Difference between revisions of "Netmiko"
From HackerNet
Helikopter (talk | contribs) m |
Helikopter (talk | contribs) m |
||
Line 14: | Line 14: | ||
=Getting started= | =Getting started= | ||
− | Python | + | [[Python]] |
from netmiko import ConnectHandler | from netmiko import ConnectHandler | ||
Connect | Connect | ||
Line 40: | Line 40: | ||
===Skapa VLAN=== | ===Skapa VLAN=== | ||
from netmiko import ConnectHandler | from netmiko import ConnectHandler | ||
+ | import getpass | ||
SW1 = {'device_type': 'cisco_ios', 'ip': '10.0.0.11', 'username': 'cisco', 'password': 'cisco', 'secret': 'cisco'} | SW1 = {'device_type': 'cisco_ios', 'ip': '10.0.0.11', 'username': 'cisco', 'password': 'cisco', 'secret': 'cisco'} | ||
− | SW2 = {'device_type': 'cisco_ios', 'ip': '10.0.0.12', 'username': 'cisco' | + | SW2 = {'device_type': 'cisco_ios', 'ip': '10.0.0.12', 'username': 'cisco'} |
+ | |||
+ | pw = getpass.getpass("Enter password:") | ||
+ | SW02['password'] = pw | ||
+ | |||
all_devices = [SW1, SW2] | all_devices = [SW1, SW2] | ||
Line 49: | Line 54: | ||
for a_device in all_devices: | for a_device in all_devices: | ||
net_connect = ConnectHandler(**a_device) | net_connect = ConnectHandler(**a_device) | ||
− | net_connect.enable() | + | net_connect.enable() |
output = net_connect.send_config_set(config_commands) | output = net_connect.send_config_set(config_commands) | ||
print output | print output | ||
[[Category:Network]] | [[Category:Network]] |
Revision as of 16:53, 9 February 2016
Netmiko är ett open-source Python library som används för att SSHa till nätverksenheter. Det är baserat på paramiko och har stöd för flera olika plattformar från olika tillverkare. Målet är att förenkla användandet av show och conf-kommandon mot nätverksutrustning från script.
Exempel på device types som stöds
- cisco_ios
- cisco_xe
- cisco_asa
- cisco_nxos
- juniper
- arista_eos
- hp_procurve
Installation
sudo pip install netmiko
Getting started
from netmiko import ConnectHandler
Connect
R1 = {'device_type': 'cisco_xe', 'ip': '10.0.0.10', 'username': 'cisco', 'password': 'cisco', 'secret': 'cisco'} net_connect = ConnectHandler(**R1)
Byt mellan olika prompts
net_connect.find_prompt() net_connect.enable() net_connect.find_prompt() net_connect.config_mode() net_connect.find_prompt()
Skicka kommandon
Send command down the SSH channel, return output back
net_connect.send_command(arguments)
Send a set of configuration commands to remote device
net_connect.send_config_set(arguments)
Send a set of configuration commands loaded from a file
net_connect.send_config_from_file(arguments)
Exempel
IPintbrief = net_connect.send_command("show ip int brief") print IPintbrief
Skapa VLAN
from netmiko import ConnectHandler import getpass SW1 = {'device_type': 'cisco_ios', 'ip': '10.0.0.11', 'username': 'cisco', 'password': 'cisco', 'secret': 'cisco'} SW2 = {'device_type': 'cisco_ios', 'ip': '10.0.0.12', 'username': 'cisco'} pw = getpass.getpass("Enter password:") SW02['password'] = pw all_devices = [SW1, SW2] config_commands = [ 'vlan 100', 'name NewVLAN', 'exit' ] for a_device in all_devices: net_connect = ConnectHandler(**a_device) net_connect.enable() output = net_connect.send_config_set(config_commands) print output