Listing 2 Python module: automgmt.py
#!/usr/bin/env python
import re
import pexpect
from telnetlib import Telnet
import exceptions
from string import *
env = {}
pwerr = re.compile('Incorrect password')
class TelnetError(exceptions.Exception):
def __init__(self, args=None):
self.args=args
class jump:
# initiate ssh tunnel
def __init__(self, stinger, config="myisp.conf"):
self._set_env(config)
sshcmd = "ssh " + env['JMPSERVER'] + \
" -L" + env['PORT'] + ":" + stinger +":23"
self.tunnel = pexpect.spawn(sshcmd)
if env.has_key('SSHPASSWD'):
self._login()
else:
self.tunnel.expect(env['JMPPROMPT'])
# read environment variables from config file
def _set_env(self,config="myisp.conf"):
global env
try:
lines = open(config,'r').readlines()
except:
print "could no open file " + env['CONFIG'] + '\n'
for line in lines:
try:
name, value = line.split('=')
except ValueError, e:
pass
env[name]=value.strip('\n')
# if public-key is not set up, login using a password
# add line: SSHPASSWORD=<password> in config file
def _login(self):
self.tunnel.expect('sword:')
self.tunnel.send(env['SSHPASSWD'] + '\n')
self.tunnel.expect('bash-2.05')
class stcon(jump):
# initiate Telnet client (through ssh tunnel)
def __init__(self,stinger):
jump.__init__(self,stinger)
self.stinger = stinger
self.prompt = env['USER']+'>'
self.session = Telnet("localhost", env['PORT'])
# if correct response not received, then
# probably Telnet password problem. In
# which case raise TelnetError exception
try:
ret = self.session.read_until('password:',3)
except EOFError, e:
self.close()
raise TelnetError, "no password prompt"
self.session.write(env['TELPASSWD'] + '\n')
# Check for response "Incorrect password",
# if received then raise TelnetError
ret = self.session.read_until('User:', 1)
if pwerr.search(ret):
self.close()
raise TelnetError, "incorrect password"
self.session = Telnet("localhost", env['PORT'])
# if correct response not received, then
# probably Telnet password problem. In
# which case raise TelnetError exception
try:
ret = self.session.read_until('password:',3)
except EOFError, e:
self.close()
raise TelnetError, "no password prompt"
self.session.write(env['TELPASSWD'] + '\n')
# Check for response "Incorrect password",
# if received then raise TelnetError
ret = self.session.read_until('User:', 1)
if pwerr.search(ret):
self.close()
raise TelnetError, "incorrect password"
self.session = Telnet("localhost", env['PORT'])
# if correct response not received, then
# probably Telnet password problem. In
# which case raise TelnetError exception
try:
ret = self.session.read_until('password:',3)
except EOFError, e:
self.close()
raise TelnetError, "no password prompt"
self.session.write(env['TELPASSWD'] + '\n')
# Check for response "Incorrect password",
# if received then raise TelnetError
ret = self.session.read_until('User:', 1)
if pwerr.search(ret):
self.close()
raise TelnetError, "incorrect password"
self.session.write(env['USER'] + '\n')
ret = self.session.read_until('Password:')
self.session.write(env['USRPASSWD'] + '\n')
ret = self.session.read_until(self.prompt)
# issue a commad to the network device and
# grab ouput
def do_cmd(self,cmd):
self.session.write(cmd+'\n')
output = self.session.read_until(self.prompt).split('\n')
# clean up
output = map(lambda i: strip(i,'\r'), output)
output = map(lambda i: strip(i), output)
try:
# remove the line with the command name in it
output.remove(cmd)
except:
pass
try:
# remove the line with the prompt in it
output.remove(self.prompt)
except:
pass
try:
# remove blank lines
output.remove('')
except:
pass
return output
def close(self):
# close Telnet session then ssh tunnel
self.session.close()
self.tunnel.close()
|