Network programming is now becoming a necessary skill for Network engineers. You can no more become a good network engineer without Network programming skills. Things are getting changed on daily basis, every now and then we see new tools being introduced to automate boring and redundant things. There is a whole industry out there which is regularly working towards mechanizing everything. AI(Artificial intelligence), Machine learning, Cognitive etc are evolving like never before and hence the need for you to grow as well. We are seeing a trend in industry that your day to day job is no more important, you must keep innovating and bringing up new ideas to survive longer and that innovation cannot come without having programming skills.
Considering all these things in my mind I have decided to include python network programming in my blog. I have decided to include a small python program related to almost my every post. And following is an example.
For Network programming currently I am using paramiko library. This simple program is going to SSH into the Cisco router and fetch some output (show users in this case). I am using python 2.7. For Cisco router I am using GNS3 Router.
In the following program I am taking inputs from user for hostname, username, password and then using connect function in the paramiko library to initiate the SSH connection to the GNS3 router.
Once we have a connection we can send commands to the router with the help of exec_commands () function. Which executes the commands on router which can be saved in variables and then printed as per our needs.
import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) hostname = raw_input("Enter host IP address: ") username = raw_input("Enter SSH Username: ") password = raw_input("Enter SSH Password: ") port = 22 ssh.connect(hostname, port, username, password, look_for_keys=False) stdin,stdout,stderr = ssh.exec_command('show users') output = stdout.readlines() print '\n'.join(output)
If you have already logged into the device from the same PC on which you are running this program then the keys would be saved and you dont need look_for_keys parameter however you need to have the parameter passed with the value False, else you will see authentication errors :
error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I have seen above in my case when I did not have look_for_keys in the program.
stdin, stdout and stderr are the standard input, standard output and standard error streams which are used for passing data to program, providing outputs and errors respectively.
Following is the program output :
================ RESTART: C:/Python27/New Folder/ssh-blog.py ================ Enter host IP address: x.x.x.x Enter SSH Username: xxxx Enter SSH Password: xxxx Line User Host(s) Idle Location 0 con0 cisco idle 00:01:23 * 2 vty 0 cisco idle 00:00:02 22.214.171.124 Interface User Mode Idle Peer Address
DO NOT forget to check following blog for another cool script
Thanks and reaching out to this blog please rate the post and follow the blog for email updates about new posts.
Stay tuned and happy learning.