I'm a newbie to python and have been reading and surfing the net to accomplish my task.
I'm writing a function which will do a ssh
to my device, execute few commands and display the result both in terminal and into a log file.
i have written something like this:
class logger(object):
def __init__(self, filename="Default.log"):
print 'Inside Logger Class'
self.terminal = sys.stdout
self.log = open(filename, "a")
class simpleTelnet(logger):
def __init__(self):
print 'Inside simpleTelnt Constructor'
logger.__init__(self,"myfilename.txt")
self.log.write = 'Writing into the log file'
def telnetSession(self):
p=pexpect.spawn('ssh admin@<ip address>')
p.logfile = sys.stdout
p.expect('Password:')
p.sendline('password')
time.sleep(2)
p.sendline('show version | no-more')
expect(pexpect.EOF, timeout = None)
out = p.before()
self.log.write(p.logfile)
p.close()
return out
if __name__ == "__main__":
output = simpleTelnet()
cmd = output.telnetSession()
Here i'm trying to login to a device and print the output on both stdout and also write to a file. I'm able to print in the stdout and log to a file but after executing the command, although i close the spawn class with p.close(), it does not close and end the script execution. the program stays there for ever. How do i close the program after executing these commands.