Difference between revisions of "Netmiko"

From HackerNet
Jump to: navigation, search
m
m
Line 25: Line 25:
 
  net_connect.config_mode()
 
  net_connect.config_mode()
 
  net_connect.find_prompt()
 
  net_connect.find_prompt()
 +
net_connect.disconnect()
  
 
'''Skicka kommandon''' <br/>
 
'''Skicka kommandon''' <br/>
Line 57: Line 58:
 
     output = net_connect.send_config_set(config_commands)
 
     output = net_connect.send_config_set(config_commands)
 
     print output
 
     print output
 +
    net_connect.disconnect()
  
 
[[Category:Network]]
 
[[Category:Network]]

Revision as of 16:20, 26 March 2019

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()
net_connect.disconnect()

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
    net_connect.disconnect()