In this post, I'm going to use Python and Telnet Library to login into a list of IP addresses and execute some commands and send the output to separated text files.
My network contains HP access switches and I need to collect some information like product number or serials from each IP in the file called (hpswitch.txt).
ok, let's start
#!/usr/bin/env python
import getpass
import telnetlib
user = ("Admin")
password = ("password")
f = open ("C:\\Users\\user.name\\Desktop\\python\\hpswitch.txt")
for line in f:
print "Getting Serials from Device " + (line)
HOST = line.strip()
tn = telnetlib.Telnet(HOST)
tn.read_until("Username:")
tn.write(user + "\n")
if password:
tn.read_until("Password:")
tn.write(password + "\n")
#this section is the switch configuration part
tn.write("super \n")
tn.write("password\n")
#tn.write("system-view \n")
tn.write("dis dev manu | in JD368B\n")
tn.write("quit\n")
#tn.write("quit\n")
readoutput = tn.read_all()
saveoutput = open("C:\\Users\\user.name\\Desktop\\serials\\switch" + HOST + ".txt", "w")
saveoutput.write(readoutput)
saveoutput.write("\n")
saveoutput.close
print tn.read_all()
=================================================================
Same code can be used for checking interfaces or uplinks states.
Samer R. Saleem
No comments:
Post a Comment