Netmiko
library to simplify CLI connections to diffèrent network devices on Multi-vendor
Installation
To install netmiko in linux ubuntu use command bellow:
$ pip install netmiko
Exemple connect to devices :
from netmiko import ConnectHandler
from getpass import getpass
password = getpass()
secret = getpass("Enter secret: ")
cisco1 = {
"device_type": "cisco_ios",
"host": "cisco1.lasthop.io",
"username": "pyclass",
"password": password,
"secret": secret,
}
net_connect = ConnectHandler(**cisco1)
# Call 'enable()' method to elevate privileges
net_connect.enable()
print(net_connect.find_prompt())
net_connect.disconnect()
Exemple Connecting to multiple devices:
from netmiko import ConnectHandler
from getpass import getpass
password = getpass()
cisco1 = {
"device_type": "cisco_ios",
"host": "cisco1.com",
"username": "admin",
"password": password,
}
cisco2 = {
"device_type": "cisco_ios",
"host": "cisco2.com",
"username": "admin",
"password": password,
}
nxos1 = {
"device_type": "cisco_nxos",
"host": "nxos.com",
"username": "admin",
"password": password,
}
srx1 = {
"device_type": "juniper_junos",
"host": "srx1.com",
"username": "admin",
"password": password,
}
for device in (cisco1, cisco2, nxos1, srx1):
net_connect = ConnectHandler(**device)
print(net_connect.find_prompt())
net_connect.disconnect()
Exemple executing show command:
from netmiko import ConnectHandler
from getpass import getpass
cisco1 = {
"device_type": "cisco_ios",
"host": "cisco1.com",
"username": "admin",
"password": getpass(),
}
# Show command that we execute.
command = "show ip int brief"
with ConnectHandler(**cisco1) as net_connect:
output = net_connect.send_command(command)
# Automatically cleans-up the output so that only the show output is returned
print()
print(output)
print()
Exemple log file:
#!/usr/bin/env python
from netmiko import ConnectHandler
from getpass import getpass
cisco1 = {
"device_type": "cisco_ios",
"host": "cisco1.lasthop.io",
"username": "pyclass",
"password": getpass(),
# File name to save the 'session_log' to
"session_log": "output.txt"
}
# Show command that we execute
command = "show ip int brief"
with ConnectHandler(**cisco1) as net_connect:
output = net_connect.send_command(command)
Exemple lab configuration :
In this lab we configure device sw1 and sw2 using network automation machine.
Switch 10 configuration :
hostname sw10
!
boot-start-marker
boot-end-marker
enable password admin
username admin password 0 admin
no aaa new-model
ip domain-name dtn.dz
ip cef
no ipv6 cef
spanning-tree mode rapid-pvst
spanning-tree extend system-id
!
vlan internal allocation policy ascending
!
interface Vlan1
ip address 192.168.122.10 255.255.255.0
!
ip forward-protocol nd
!
no ip http server
no ip http secure-server
!
ip ssh version 2
!
control-plane
line con 0
line aux 0
line vty 0 4
login local
!
!
end
Switch 20 configuration :
hostname sw20
!
enable password admin
!
username admin password 0 admin
no aaa new-model
ip domain-name dtn.dz
ip cef
no ipv6 cef
ing-tree mode rapid-pvst
spanning-tree extend system-id
!
vlan internal allocation policy ascending
!
interface Vlan1
ip address 192.168.122.20 255.255.255.0
!
ip forward-protocol nd
!
no ip http server
no ip http secure-server
!
ip ssh version 2
line con 0
line aux 0
line vty 0 4
login local
!
!
end
No comments:
Post a Comment