Basic Netmiko

Netmiko adalah sebuah library python yang meringkas penggunaan dari paramiko (high level than paramiko) dan menggunakan paramiko sebagai base untuk melakukan SSH. Netmiko dibuild khusus untuk perangkat jaringan, berikut beberapa perangkat yang sudah ditest :

Supports:

Arista vEOS
Cisco ASA
Cisco IOS
Cisco IOS-XE
Cisco IOS-XR
Cisco NX-OS
Cisco SG300
Dell OS10
HP Comware7
HP ProCurve
Juniper Junos
Linux

Limited testing

Alcatel AOS6/AOS8
Avaya ERS
Avaya VSP
Brocade VDX
Brocade MLX/NetIron
Calix B6
Cisco WLC
Dell-Force10
Dell PowerConnect
Huawei
Mellanox
NetApp cDOT
Palo Alto PAN-OS
Pluribus
Ruckus ICX/FastIron
Ubiquiti EdgeSwitch
Vyatta VyOS

konfigurasi perangkat cisco ios dan vyos

R1(config)#hostname R1
R1(config)#ip domain-name btech.id        
R1(config)#crypto key generate rsa 
The name for the keys will be: R1.btech.id
Choose the size of the key modulus in the range of 360 to 2048 for your
  General Purpose Keys. Choosing a key modulus greater than 512 may take
  a few minutes.

How many bits in the modulus [512]: 2048
% Generating 2048 bit RSA keys, keys will be non-exportable...[OK]

*Mar  1 00:02:27.167: %SSH-5-ENABLED: SSH 1.99 has been enabled
R1(config)#ip ssh version 2
R1(config)#username btech privilege 15 secret btech
R1(config)#line vty 0 4
R1(config-line)#transport input ssh 
R1(config-line)#login local

[edit]
vyos@vyos# set interfaces ethernet eth1 address dhcp
[edit]
vyos@vyos# set service ssh port 22
[edit]
vyos@vyos# commit
[ interfaces ethernet eth1 address dhcp ]
Starting DHCP client on eth1 ...

[edit]
vyos@vyos# save
Saving configuration to '/config/config.boot'...
Done

install library netmiko :

ubuntu@ubuntu:~$ sudo pip install netmiko
[sudo] password for ubuntu: 
The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.                                                                                                   
The directory '/home/ubuntu/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.                                                                                                          
Collecting netmiko
  Downloading https://files.pythonhosted.org/packages/96/f8/4ecb91398a14bc1942322e87203859b04421cdb2b8b5c7d9b3fc9a168e21/netmiko-2.1.1.tar.gz (71kB)
    100% |████████████████████████████████| 71kB 713kB/s 
Requirement already satisfied (use --upgrade to upgrade): paramiko>=2.0.0 in /usr/local/lib/python2.7/dist-packages (from netmiko)
Collecting scp>=0.10.0 (from netmiko)
  Downloading https://files.pythonhosted.org/packages/ef/8c/399c5ddffc8c1bd2593ee6c8937ed31f005dcfa0c591da149f7b4b993f14/scp-0.11.0-py2.py3-none-any.whl
Collecting pyyaml (from netmiko)
  Downloading https://files.pythonhosted.org/packages/9e/a3/1d13970c3f36777c583f136c136f804d70f500168edc1edea6daa7200769/PyYAML-3.13.tar.gz (270kB)
    100% |████████████████████████████████| 276kB 549kB/s 
Collecting pyserial (from netmiko)
  Downloading https://files.pythonhosted.org/packages/0d/e4/2a744dd9e3be04a0c0907414e2a01a7c88bb3915cbe3c8cc06e209f59c30/pyserial-3.4-py2.py3-none-any.whl (193kB)
    100% |████████████████████████████████| 194kB 562kB/s 
Collecting textfsm (from netmiko)
  Downloading https://files.pythonhosted.org/packages/a1/0d/a1b490503545b3b4600b965eae5d44cc2b6ce27cfb44f4debc563dbb56d3/textfsm-0.4.1.tar.gz
Requirement already satisfied (use --upgrade to upgrade): pyasn1>=0.1.7 in /usr/local/lib/python2.7/dist-packages (from paramiko>=2.0.0->netmiko)
Requirement already satisfied (use --upgrade to upgrade): bcrypt>=3.1.3 in /usr/local/lib/python2.7/dist-packages (from paramiko>=2.0.0->netmiko)
Requirement already satisfied (use --upgrade to upgrade): cryptography>=1.5 in /usr/local/lib/python2.7/dist-packages (from paramiko>=2.0.0->netmiko)
Requirement already satisfied (use --upgrade to upgrade): pynacl>=1.0.1 in /usr/local/lib/python2.7/dist-packages (from paramiko>=2.0.0->netmiko)
Requirement already satisfied (use --upgrade to upgrade): six>=1.4.1 in /usr/local/lib/python2.7/dist-packages (from bcrypt>=3.1.3->paramiko>=2.0.0->netmiko)
Requirement already satisfied (use --upgrade to upgrade): cffi>=1.1 in /usr/local/lib/python2.7/dist-packages (from bcrypt>=3.1.3->paramiko>=2.0.0->netmiko)
Requirement already satisfied (use --upgrade to upgrade): enum34; python_version < "3" in /usr/local/lib/python2.7/dist-packages (from cryptography>=1.5->paramiko>=2.0.0->netmiko)
Requirement already satisfied (use --upgrade to upgrade): asn1crypto>=0.21.0 in /usr/local/lib/python2.7/dist-packages (from cryptography>=1.5->paramiko>=2.0.0->netmiko)
Requirement already satisfied (use --upgrade to upgrade): idna>=2.1 in /usr/local/lib/python2.7/dist-packages (from cryptography>=1.5->paramiko>=2.0.0->netmiko)
Requirement already satisfied (use --upgrade to upgrade): ipaddress; python_version < "3" in /usr/local/lib/python2.7/dist-packages (from cryptography>=1.5->paramiko>=2.0.0->netmiko)
Requirement already satisfied (use --upgrade to upgrade): pycparser in /usr/local/lib/python2.7/dist-packages (from cffi>=1.1->bcrypt>=3.1.3->paramiko>=2.0.0->netmiko)
Installing collected packages: scp, pyyaml, pyserial, textfsm, netmiko
  Running setup.py install for pyyaml ... done
  Running setup.py install for textfsm ... done
  Running setup.py install for netmiko ... done
Successfully installed netmiko-2.1.1 pyserial-3.4 pyyaml-3.13 scp-0.11.0 textfsm-0.4.1
You are using pip version 8.1.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

buat programnya :

# import library netmiko
from netmiko import ConnectHandler

# Define cisco ios
# device_type list https://github.com/ktbyers/netmiko/blob/master/netmiko/ssh_dispatcher.py
cisco_ios = {
    'device_type':'cisco_ios',
    'ip':'192.168.122.250',
    'username':'btech',
    'password':'btech',
    'port' : 22
}

vyos = {
    'device_type':'vyos',
    'ip':'192.168.122.111',
    'username':'vyos',
    'password':'vyos',
    'port' : 22
}

# add all device into list
device = [cisco_ios,vyos]

# use looping
for node in device:
    connect = ConnectHandler(**node)
    output = connect.send_command('show arp')
    print "\n\n##### Device {0} #####".format(node['device_type'])
    print (output)

jalankan dan lihat hasilnya :

ubuntu@ubuntu:~/netmiko$ python program.py 


##### Device cisco_ios #####
Protocol  Address          Age (min)  Hardware Addr   Type   Interface
Internet  192.168.122.129         0   0c9e.2e63.4800  ARPA   FastEthernet0/0
Internet  192.168.122.250         -   c201.0a97.0000  ARPA   FastEthernet0/0


##### Device vyos #####
Address                  HWtype  HWaddress           Flags Mask            Iface
192.168.122.1            ether   52:54:00:39:6a:c8   C                     eth1
192.168.122.129          ether   0c:9e:2e:63:48:00   C                     eth1

Comments are closed.