Blogs
- Mastering DHCP Snooping: Enhance Your Network Security
- Automate Meraki Device Renaming
- Securing Your Network Access with 802.1X
- OpenSSL cheatsheet
- 802.1x EAP peap and EAP tls
- BGP Internet Edge
- Sumologic Troubleshooting
- Firewall Benefits
- Meraki
- Napalm Python
- SumoLogic SEIM
- Layer 1 and 2 checklist
- Automating OS Upgrade
- Netmiko
- TCPDUMP
- Multicast Notes
- MPLS Notes
- BGP Notes
- OSPF Notes
- Linux cheat sheet
- ISIS Notes
- TCP IP
Netmiko
Introduction
If you're like most network professionals, you're always looking for ways to speed up your network management tasks. Well, look no further! Python Netmiko can help you automate your networking tasks and save you a lot of time. In this blog and video series, we'll show you how to use Python Netmiko to manage your networks faster and more efficiently. So what are you waiting for? Start using Python Netmiko today!
Pre-requisite
- Python
Video training
- : Goes over this blog that explains the most used netmiko functions.
- : Covers examples of the functions send_command, send_command_timing and send_config_set.
- : Will be covering the functions write_channel and read_channel by creating our own sendCommandTiming function.
- : we will update the above script to create our own sendcommand function.
Functions
send_command_timing()
last_read: will take effect when we are receiving data, Netmiko will keep reading the data every 0.1 seconds. Once Netmiko detects no more NEW data, it switches to last_read mode. It will wait until the last_read before returning all data or looping again. If you are not capturing all the data you want, then you should increase the last_read time.
output = netCom.send_command_timing(
self,
command_string: str,
last_read: float = 2.0,
read_timeout: float = 120.0,
# other arguments
)
print(output)
send_command()
By default send_command() will try to find the current prompt using the find_prompt() method. In other words, Netmiko will attempt to find the existing prompt before the command is sent.
expect_string: tells Netmiko what to look for in the output
delay_factor: Its effect is to modify all of the delays embedded in send_command (for example, a delay_factor=2 will double all of the delays; a delay_factor=4 will quadruple all of the delays).
output = netCom.send_command(
cmd,
expect_string=r'something'
)
print(output)
send_config_from_file(str: pathToFile)
send commands from file
result = netCom.send_config_from_file('running.cfg')
send_config_set()
Execute multiple configuration commands (will automatically enter into config mode)
config_commands = [ 'logging buffered 20000',
'logging buffered 20010',
'no logging console' ]
output = netCom.send_config_set(config_commands)
print(output)
redispatch(obj, device_type: str)
Change Netmiko object's class to proper class. if you need to redispatch after interacting with a jump host.
#!/usr/bin/env python
def showVersionThroughtJumpHost(jump_host,device):
netCom = Netmiko(**jump_host)
out = netCom.send_command_timing('ssh {}@{}\n'.format(device['username'],device['host']))
if 'assword' in out:
out = netCom.send_command_timing(device['password']+'\n')
elif 'The authenticity of host ':
out = netCom.send_command_timing('yes\n')
out = netCom.send_command_timing(device['password']+'\n')
else:
print('-----------Unknown return from ssh session')
redispatch(netCom,device_type=device['device_type'])
netCom.send_command_timing('enable')
netCom.find_prompt()
out = netCom.send_command('show version')
print(out)
read_channel() and write_channel(str:'cmd')
commands are useful when you want to connect through a jump host and don't want netmiko to deal with the prompt or timing to show the return outputs from the device. you will have to write your own delay and expect conditions
read_channel(): you can read everything in the ssh session at the time of executing the function
write_channel(str:'cmd'): send the command to the ssh session
device ={
'host':'192.168.0.201',
'username':'cisco','password':'cisco','secret':'enable_password',
'device_type':'cisco_ios'
}
jump_host = {'host':'192.168.0.14',
'username':'sftpuser','password':'password123',
'device_type':'linux_ssh',
}
netCom = Netmiko(**jump_host)
netCom.write_channel('ssh {}@{}\n'.format(device.get('username'),device.get('host')))
time.sleep(1)
out = netCom.read_channel()
if 'assword' in out :
print('----------------- assword -----------------')
netCom.write_channel(device.get('password')+'\n')
elif 'The authenticity of host ' in out:
print('----------------- authenticity-----------------')
netCom.write_channel('yes\n')
time.sleep(1)
netCom.write_channel(device.get('password')+'\n')
time.sleep(1)
else:
print('----------------- script unknow reply -----------------')
print(out)
redispatch(netCom,device_type=device.get('device_type'))
out = netCom.send_command('show verion\n')
print(out)
Logging
Enable Logging to see all communication with the device
import logging
logging.basicConfig(
format='%(asctime)s :%(levelname)s:%(message)s',
filename='./CombinationLogging.log',
level=logging.DEBUG,
datefmt='%m/%d/%Y %I:%M:%S %p'
)
enable()
Go into enable mode
check_enable_mode()
check if the devices is in enable mode
exist_enable_mode()
exit enable mode
find_prompt()
Find the current prompt
disconnect()
Disconnect from the device
Good Doc
YouTube
Doc #1
Doc #2
GitRepo Netmiko Exercise
GitRepo OsUpgrade
If you need any help, don't hesitate to reach out. We are more than happy to help you in any way we can.
Talk to an expert