Wednesday, 27 September 2017

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

import paramiko
import time
import sys
import logging
import socket

remote_conn_pre = paramiko.SSHClient()
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# store router ip in ip.txt file in same folder where script is.
ips = [i.strip() for i in open("ip.txt")]

g = open('config.txt', 'r+')# store configuration in config.txt file in same folder where script is.
str = g.read()
g.close

for ip in ips:
    try:
        remote_conn_pre.connect(ip, username='test', password='test', timeout=4, look_for_keys=False, allow_agent=False)

        remote_conn = remote_conn_pre.invoke_shell()
        print (ip + ' === Device Reachable')
        remote_conn.send("\n")
        remote_conn.send((str))
        time.sleep(2)
        #output = remote_conn.recv(5000)
        #print (output)
    except paramiko.AuthenticationException:
        print ip + ' === Bad credentials'
    except paramiko.SSHException:
        print ip + ' === Issues with ssh service'
    except socket.error:
        print ip + ' === Device unreachable'

Saturday, 23 September 2017

Python for network engineer: cisco device backup using SSH : python2.7

Below script will take the backup of cisco devices in the folder where you have stored your python script. Please make sure ip.txt file(which contains router IP address) is also in same folder.

Below is the sample ip.txt file.


import paramiko
import time
import datetime

ssh1 = paramiko.SSHClient()
ssh1.set_missing_host_key_policy(paramiko.AutoAddPolicy())

now = datetime.datetime.now()

#open file in read mode
#ip.txt is a text file which contains routers IP address. Make sure it is stored in same folder #where the python script is. 
f = open("ip.txt","r")
lines = f.readlines()
f.close()

#open file in write mode and remove spaces between rows.

f = open("ip.txt","w")
for line in lines:
    if line!=""+"\n":
        f.write(line)
f.close()

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

for ip in ips:
    try:
        filename_prefix = (ip)
        ssh1.connect(ip,username='test', password='test')
        ssh = ssh1.invoke_shell()
        time.sleep(5)
        print ip
        ssh.send("\nterminal length 0\n")
        ssh.send("show run\n")
        time.sleep(5)
        output = ssh.recv(65535)
        print output
        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()
       
    except:
        response = 'Failed'
        print (ip + " is not reachable")




Friday, 22 September 2017

Python for network Engineer:- SSH to cisco router and run multiple commands :- python2.7

import paramiko
import time
#from getpass import getpass

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.10.1', username='test', password='test')


ssh = ssh.invoke_shell()

ssh.send("show ip int brief\n")
time.sleep(1)
output = ssh.recv(65535)
print output
time.sleep(5)
ssh.send("sh ver\n")
time.sleep(1)
output = ssh.recv(65535)
print output


Monday, 18 September 2017

How to remove rows with specific text from the notepad using python 2.7

Below script is used to remove specific text from notepad.

Below script is used to remove specific text from the notepad using python. To demonstrate the same we have used a file which has “networking” word in the file.

#open the file in read mode which you need to modify, we have used ip.txt file.
file= open("ip.txt","r")
# Read the file
lines = file.readlines()
# close the file
file.close()

# Read the file in write mode
file = open("ip.txt","w")
# Reading each line and writing text, which is not a "networking" word, back to the file.
for line in lines:
    if line!="networking"+"\n":
        file.write(line)
file.close()

Before the script execution, ip.txt contains below content. You can see that below file contains word "networking" in many rows and our onjective is to remove all the rows which contains word "networking".






















After script execution, all rows containing  word "networking" has been deleted.

  

How to remove empty rows from notepad using Python 2.7

Below script is used to remove empty rows from notepad.

#open the file in read mode which you need to modify, we  have used ip.txt file.
file= open("ip.txt","r")
# Read the file
lines = file.readlines()
# close the file
file.close()

# Read the file in write mode
file = open("ip.txt","w")
# Reading each line and writing text which is not a space back to the file.
for line in lines:
    if line!=""+"\n":
        file.write(line)
file.close()

Before the script execution, ip.txt contains below content. You can see there are various spaces in it.




















After script execution, All empty rows has been deleting from the file.











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()