-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetmiko
51 lines (39 loc) · 1.7 KB
/
netmiko
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Netmiko - SSH Network Device Class
from netmiko import ConnectHandler
# Dictionary of device info. Port is optional if using standard 22
myrouter = {
'device_type': 'cisco_ios',
'ip': '10.11.12.55',
'username': 'myuser',
'password': 'mypass',
'port': 22,
}
# The ** passes the dictionary as arguments into the ConnectHandler class
router_conn = ConnectHandler(**myrouter)
# To see the prompt you are at
router_conn.find_prompt()
# To go into config mode
router_conn.config_mode()
# To check if you are in config mode, returns boolean
router_conn.check_config_mode()
# To exit config mode
router_conn.exit_config_mode()
# To send a command. This automatically adds the 'new line \n'
# The output removes the echo of the command and the trailing prompt of the command
# Long commands work automatically, netmiko takes care of disabling paging
# send_command() is timing based (i.e. uses a lot of sleeps)
# send_command_expect() by default will search for the router prompt at the end of the output.
# you can add an argument delay_factor to send_command to make it wait longer i.e.
# send_command(delay_factor=.5) will cause it to delay meaningfully longer.
output = router_conn.send_command("show ip int brief")
or
output = router_conn.send_command_expect("show ip int brief")
print output
# To send a set of commands you create the commands in a list
config_commands = ['logging buffered 20000', 'logging on']
output = router_conn.send_config_set(config_commands)
# To change config using a file as the source
# The commands are one per line, no commas, quotes, etc.
router_conn.send_config_from_file(config_file='config_file.txt')
# To save the configuration you have to manually do that
router_conn.send_command("wr mem")