Difference between revisions of "Netmiko"

From HackerNet
Jump to: navigation, search
(Created page with "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...")
 
m
Line 3: Line 3:
 
Exempel på device types som stöds
 
Exempel på device types som stöds
 
* cisco_ios
 
* cisco_ios
 +
* cisco_xe
 
* cisco_asa
 
* cisco_asa
 +
* cisco_nxos
 
* juniper
 
* juniper
 
* arista_eos
 
* arista_eos
Line 15: Line 17:
 
  from netmiko import ConnectHandler
 
  from netmiko import ConnectHandler
 
Connect
 
Connect
  R1 = {'device_type': 'cisco_ios', 'ip': '10.0.0.11', 'username': 'cisco', 'password': 'cisco'}   
+
  R1 = {'device_type': 'cisco_xe', 'ip': '10.0.0.10', 'username': 'cisco', 'password': 'cisco', 'secret': 'cisco'}   
 
  net_connect = ConnectHandler(**R1)
 
  net_connect = ConnectHandler(**R1)
Verify
+
Byt mellan olika prompts
 
  net_connect.find_prompt()
 
  net_connect.find_prompt()
 +
net_connect.enable()
 +
net_connect.find_prompt()
 +
net_connect.config_mode()
 +
net_connect.find_prompt()
 +
 +
'''Skicka kommandon''' <br/>
 +
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
 
Exempel
 
  IPintbrief = net_connect.send_command("show ip int brief")
 
  IPintbrief = net_connect.send_command("show ip int brief")
Line 26: Line 41:
 
  from netmiko import ConnectHandler
 
  from netmiko import ConnectHandler
 
   
 
   
  SW1 = {'device_type': 'cisco_ios', 'ip': '10.0.0.11', 'username': 'cisco', 'password': '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', 'password': 'cisco'}
+
  SW2 = {'device_type': 'cisco_ios', 'ip': '10.0.0.12', 'username': 'cisco', 'password': 'cisco', 'secret': 'cisco'}
  all_devices = [R1, R2]
+
  all_devices = [SW1, SW2]
 
   
 
   
 
  config_commands = [ 'vlan 100', 'name NewVLAN', 'exit' ]
 
  config_commands = [ 'vlan 100', 'name NewVLAN', 'exit' ]
Line 34: Line 49:
 
  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()
 
     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 21:47, 24 January 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

Python

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

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', 'password': 'cisco', 'secret': 'cisco'}
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