Google
 
Web pyadmin.blogspot.com

Thursday, May 04, 2006

Problem faced with Nortel routers

As I am doing my work, I had came to one situation that, in evrymonth beginning I hav to change the password of 400 nortel routers within 2 days. Offcourse it's a very tedious task even with a 4 memberd team. Earlier it was very simple with cisco and juniper routers that a command like

for i in `cat file_containing_list_of_routers`

do
{
(echo "username" ; echo "password" ;
echo "term length 0";
cat commands_cisco.txt;
echo "exit";
echo "exit";
sleep 2 ) | telnet $i >> /verify.out

}
done
where ommands_cisco.txt contains the commads for changing password in a cisco/juniper router



will do the work. But when it's come to nortel routers, alas this thing is not working, I heard that this is bcos of some security mechanism implementd on nortel routers.There comes my shy friend

I am using the telnetlib module. The script reads the ip addresses from a file and changes the password.

#!/usr/bin/python
__author__ = 'Vineesh Kumar : vinu@hcl.in'
__version__ = '0.2'
__date__ = '7/04/06'

import getpass
import sys
import telnetlib
import time

sleeptime=15 """The time to wait for getting a response from the router"""

"""
Ok I will extract the input arguments
"""
def extractArgs(listofargs):
for arg in range(0,len(listofargs),2):
if listofargs[arg] == '-f':
file=listofargs[arg+1]
elif listofargs[arg]=='-u':
user=listofargs[arg+1]
elif listofargs[arg]=='-p':
password=listofargs[arg+1]
elif listofargs[arg] == '-n':
newpwd=listofargs[arg+1]
return [file,user,password,newpwd]

"""
It's my duty to read the IPS from a file specified by the -f switch
"""
def readIpsFromFile(filename):
fileHandle = open ( filename )
fileList = fileHandle.readlines()
fileHandle.close()
return fileList

"""
reads the ip from the list and change the password
"""
def changeAll(hostlist,user,passwd,newpasswd):
for host in hostlist:
if host.strip():
changePassword(host,user,passwd,newpasswd)

"""
The actual worker who changes the password by telnetting to
a specified ip
"""
def changePassword(host,user,passwd,newpasswd):
HOST=host
user=user
password=oldpwd=passwd
newpwd=newpasswd
try:
if HOST.strip():
tn = telnetlib.Telnet(HOST)
print tn.read_until("Login:")
tn.write(user + "\n")
if password:
print tn.read_until("Password:",120)
tn.write(password + "\n")
if tn.read_until(">",120):
tn.write("bcc\n")
print tn.read_until("bcc>",120)
tn.write("password\n")
print tn.read_until("password:",120)
time.sleep(sleeptime)
tn.write(oldpwd+ "\n")
time.sleep(sleeptime)
tn.write(newpwd + "\n")
time.sleep(sleeptime)
tn.write(newpwd + "\n")
tn.write("exit")
tn.write("logout")
tn.close()
print "Password changed successfully for device: " + host +"\n"
else:
print "Password changing failed for device :"+ host + "\n"
except EOFError:
print "Username/Password mismatch for device :"+ host + "\n"

"""
I am the master I will call others to do the job
"""
def doTheJob():
try:
list=extractArgs(sys.argv[1:])
ips=readIpsFromFile(list[0])
print ips
changeAll(ips,list[1],list[2],list[3])
except UnboundLocalError:
print "usage: chpwd -f -u -p -n "

"""
I am the admin and ultimately I am executing this program
"""

doTheJob()




and
That may not be the end of the story.........
The hunt begins

1 Comments:

At 7:37 PM, Blogger Unknown said...

Any advice when trying to pull a config of a Nortel Passport switch using pexpect? All I get is approx 366 bytes of data in my file. No errors nothing...

While I'm here: I'm using ciscoconfparser to produce csv files with the info I need from multiple Cisco config files. However there doesn't seem to be a similar thing for Nortel configurations. Any advice?

 

Post a Comment

<< Home