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

Tuesday, May 02, 2006

An admin friendly ping

When i was working, I just want to ping some machines.And offcourse the machines are from other networks(otherwise i can use broadcast).So one way i found is that i can write a simple shell script which does the ping to the individual systems, but what about if u are having some 100 systems and all takes time more that 2 seconds to get a response?
here comes an alternative!!!!! a threaded ping, offcourse using python
#!/usr/bin/python
import os
import re
import time
import sys
from threading import Thread

class ping(Thread):
def __init__ (self,ip):
Thread.__init__(self)
self.ip = ip
self.status = -1
def run(self):
pingseq = os.popen("ping -q -c2 "+self.ip,"r")
while 1:
line = pingseq.readline()
if not line: break
result = re.findall(ping.getstatus,line)
if result:
self.status = int(result[0])

ping.getstatus = re.compile(r"(\d) received")
output = ("Not responding","Partial Response","Responding")

print time.ctime()

pinglist = []

for host in range(100,120):
ip = "10.200.9."+str(host)
current = ping(ip)
pinglist.append(current)
current.start()

for pingle in pinglist:
pingle.join()
print "Status from ",pingle.ip,"is",output[pingle.status]

print time.ctime()

some fun with python

Here is a python script for sending email.U may wonder y i need another mail sender. No u don't need one.
but if u just want to play on ur friend by shooting some funny emails without revealing ur identity, u need this one. say if u want to play around a friend pretending as a (fe)male, u can use this script to send him/her an email as sexy(fe)male@heaven.com

Guyz!!!(who thinks pyton in for serious tasks),plz dont scold me. This is meant for newbies.This script reveals the power of python. A newbe can understand this script.

This is a command line tool, and moreover u dont need an email account to send mail to someone. But offcourse u need a mail server,and u cannot recieve get any reply in this (noexistent)email id. This is not a really useful tool. This is for those who are just into python and also to demonstrate the power of python.

#!/usr/bin/python

import smtplib, string, sys, time
"""IP/hostname of ur mail server"""
mailserver = "10.200.9.99"

From = string.strip(raw_input("From: "))
To = string.strip(raw_input("To: "))
Subject = string.strip(raw_input("Subject: "))

Date = time.ctime(time.time())
Header = ("From: %s\nTo: %s\nDate: %s\nSubject: %s\n\n"
% (From, To, Date, Subject))


Text = "your message here"
server = smtplib.SMTP(mailserver)
failed = server.sendmail(From, To, Header + Text)
server.quit()
if failed:
print "failed to send mail"
else:
print "all done.."

First words

Here is the first posting of mine. this is just for testing the look and feel