I am trying to manage several cisco switches in a network I control. I simply want to use pexpect to log-into each one to check uptime, just to keep an eye each morning that the devices have not rebooted.
I have the pexpect portion working, but I want to list all the IP’s of the devices in a text file, and then using the python script, have it ingest these ip’s, and one by one, log into the device and do a show version command. Perhaps, if I define the pexpect commands as a module this would work, but not sure how to do that. I am new to python and tried to find the answer, but after many google searches, came up empty so wanted to try here.
Below is my attempt in automating this, but it does not work.
Here is what I have thus far.
import pexexpect
list=raw_input('Type in name of text file:' )
fo=file(list, 'r')
tt=fo.readlines()
fo.close()
for ip in tt:
load(ip)
child=pexpect.spawn("telnet ip")
child.expect("password:*")
child.sendline("mypassword")
Apart from that, use
opennotfile, and you don’t need to read the lines, you can simply iterate the file object. Nice would be to extract the pexpect stuff into a function: