Thursday, 14 September 2017

Python for network engineer :- RUN ping test to a destination from multiple routers using Python2.7




Below script is used to run ping test to a destination from all routers.

e.g ip.txt must in same folder where you are storing you python program.

Note:- Make sure router IP must be reachable from the machine where python script is configured.

File used in the scripts are :-
import telnetlib
import time

user = "test"
password = "test"

ips = [i.strip() for i in open("ip.txt")]

for ip in ips:
     telnet = telnetlib.Telnet(ip)
     telnet.read_until("Username: ")
     telnet.write(user + "\n")
     telnet.read_until("Password: ")
     telnet.write(password + "\n")
     telnet.write("ping 192.168.10.100\n") #< 192.168.10.100 is the destination
     time.sleep(1)
     telnet.write("exit\n")
     print telnet.read_all()

Python for network engineer :- Cisco router config backup using Python2.7



Below script is used to take backup of multiple devices. Make sure when you call a file in python then that file must be stored in same folder where you are storing your python script.

e.g both ip.txt must in same folder where you are storing you python program.

Note:- Make sure router IP must be reachable from the machine where python script is configured.



File used in the scripts are :-

ip.txt :- A notepad file contains below router IP address.

192.168.10.1
192.168.10.2
192.168.10.3


import telnetlib
import datetime

now = datetime.datetime.now()
ips = [i.strip() for i in open("ip.txt")]
username = "test"
password = "test"

for ip in ips:
    filename_prefix = (ip)
    tn = telnetlib.Telnet(ip)
    tn.read_until("Username:")
    tn.write(username+"\n")
    tn.read_until("Password:")
    tn.write(password+"\n")
    tn.write("terminal length 0"+"\n")
    tn.write("sh run"+"\n")
    tn.write("exit"+"\n")
    output=tn.read_all()

    filename = "%s_%.2i-%.2i-%i_%.2i-%.2i-%.2i" % (filename_prefix,now.day,now.month,now.year,now.hour,now.minute,now.second)

    fp=open(filename,"w")
    fp.write(output)
    fp.close()

Python for network engineer :- Configure multiple router using telnet :-python 2.7


Below script is used to configure multiple devices. In below script ACL needs to be applied to multiple routers. Both Router IP and configuration are stored in different files Make sure when you call a file in python then that file must be stored in same folder where you are storing your python script.

e.g both config.txt must in same folder where you are storing you python program.

Note:- Make sure router IP must be reachable from the machine where python script is configured.

File used in the scripts are :-

Config.txt :- A notepad file contains below ACL configuration

conf t

ip access-list extended Restrict_Outlets
Permit ip any 10.130.127.0 0.0.0.255
deny ip any 10.126.0.0 0.1.255.255
deny ip any 10.128.0.0 0.3.255.255
deny ip any 10.175.0.0 0.0.255.255
deny ip any 10.176.0.0 0.7.255.255
deny ip any 10.190.0.0 0.1.255.255
deny ip any 10.192.0.0 0.15.255.255
deny ip any 10.208.0.0 0.1.255.255
deny ip any 10.210.0.0 0.0.255.255
deny ip any 10.60.0.0 0.0.255.255
permit ip any any

int fa3/0
ip access-group Restrict_Outlets out
no shut

ip.txt :- A notepad file contains below router IP address.

192.168.10.1
192.168.10.2
192.168.10.3
====================Program============

import time
import telnetlib
import sys

#=====Credential for the devices
user = "test"
password = "test"

#=====config.txt contains the ACL

g = open('config.txt', 'r+')   
str = g.read()
g.close

#=====Extracting routers IP from ip.txt file and storing it in variable ips.

ips = [i.strip() for i in open("ip.txt")]

#=====Below FOR loop is  configuring ACL to one router at a time.

for ip in ips:
     telnet = telnetlib.Telnet(ip)
     telnet.read_until("Username: ")
     telnet.write(user + "\n")
     telnet.read_until("Password: ")
     telnet.write(password + "\n")
     telnet.write(str)
     telnet.write("end\n")
     telnet.write("exit\n")
     time.sleep(5)
     print ip
     print telnet.read_all()

====================END============

Router config example:-

hostname R1

interface FastEthernet0/0
 ip address 192.168.10.1 255.255.255.0
 duplex full
 no shut

interface FastEthernet3/0
 no ip address
 ip access-group Restrict_Outlets out ----<<<<<
 shutdown
 duplex auto
 speed auto

line vty 0 4
 login local
line vty 5 16
 login local
!
!
end






Python file modes


The modes are: 
  • r’ – Only to read a file.
  • w’ – Used to edit and write new information to the file.
  • a’ – Appending mode:- Used to add new data in existing file.  
  • r+’ – Special read and write mode, When need to perform both read and write functions to the file.

Wednesday, 13 September 2017

Python for network engineer:- Configure Cisco router using Python2.7

We tried to configure loopback0 on the router using python. Same script can be used to configure anything on the router. We just need to add telnet.write command to configure the cisco router.

import telnetlib

user = "test"
password = "test"

HOST = "192.168.1.100"

telnet = telnetlib.Telnet(HOST)

telnet.read_until("Username: ")
telnet.write(user + "\n")
telnet.read_until("Password: ")
telnet.write(password + "\n")
telnet.write("conf t\n")
telnet.write("int lo0\n")
telnet.write("ip address 1.1.1.1 255.255.255.0\n")
telnet.write("end\n")
telnet.write("exit\n")
print telnet.read_all()

=== output===

R1#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)#int lo0
R1(config-if)#ip address 1.1.1.1 255.255.255.0
R1(config-if)#end
R1#exit

Python for network engineer :- SSH to Cisco router using Python2.7

Below script can be used to access the device using SSH and capture show ip int brief command output.

import paramiko  #--<< import Paramiko library
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # --<< to add host key
ssh.connect('192.168.1.100', port=22, username='test', password='test')--<< Device IP address, ssh port , username and password of the device)
stdin,stdout,stderr=ssh.exec_command('show ip int brief')
output=stdout.readlines()
type(output)
print '\n'.join(output)


Output:-

Interface                  IP-Address      OK? Method Status                Protocol

FastEthernet0/0            192.168.1.100   YES manual up                    up      

FastEthernet1/0            unassigned      YES unset  administratively down down    

FastEthernet1/1            unassigned      YES unset  administratively down down    

FastEthernet2/0            unassigned      YES unset  administratively down down    

FastEthernet2/1            unassigned      YES unset  administratively down down    

FastEthernet3/0            unassigned      YES unset  administratively down down    

FastEthernet3/1            unassigned      YES unset  administratively down down    

>>> 

Python for network engineer:- Telnet to Cisco router using Python

Below script can be used to telnet Cisco router using Python script and can capture show version output.


import telnetlib # --<< importing telnet library

user = "test"   #--<< username configured in router
password = "test" #--<<Password configured in router


HOST = "192.168.1.100" #--<<IP address of the router

telnet = telnetlib.Telnet(HOST)

telnet.read_until("Username: ") # --<<Reading username from the router CLI screen
telnet.write(user + "\n") #--<< Configure username which is configured above

telnet.read_until("Password: ") #--<<Reading Password from the router CLI screen
telnet.write(password + "\n") #--<< Configure password which is configured above

telnet.write("terminal length 0\n") #--<< Configure command to capture the complete output
telnet.write("show version \n")  #--<<configure  show run

telnet.write("exit\n")  # --<<exit from the router


print telnet.read_all()  #-<<Read output from above show version command