[Tutor] Python on Windows with SSH for Cisco devices

Chris Hallman challman at gmail.com
Fri Jul 13 21:50:49 CEST 2007


Has anyone successfully used Python on Windows with SSH connections to Cisco
devices? I'm referring to using a Python module (Paramiko, pyssh though not
actively developed, Twisted.conch, etc.) and not shelling out via Pexpect
(doesn't work on Windows) or Plink. I need to connect to hundreds of Cisco
routers/switches so that I can run numerous commands to gather data. Here is
a snippet of my Telnet commands that I need working via SSH:

self.tn.write("wr\n")
(index, match, read) = self.tn.expect(["OK"], 15)
if not match:
    self.tn.write("yes\n")
time.sleep(random.uniform(0,2))
self.tn.write("copy tf runn\n")
self.tn.read_until("host []?", 7)
time.sleep(random.uniform(0,2))
self.tn.write("192.168.136.51\n")
self.tn.read_until("filename []?", 7)
time.sleep(random.uniform(0,2))
self.tn.write(self.filename +"\n")
time.sleep(random.uniform(0,2))
self.tn.read_until("[running-config]?", 7)
time.sleep(random.uniform(0,2))
self.tn.write("\n")


I've been able to get the following to work, but the TCP connection closes
after each command:

import paramiko

client = paramiko.SSHClient()

# ignore host keys for the test
client.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy())

client.connect('host####', 22, 'user####', 'passw####')
(stdin, stdout, stderr) = client.exec_command('sh ver | i IOS')
print stdout.read()


I've read that I can use a dummy channel (not sure the difference between a
channel and a TCP connection) to keep the connection active. I've read the
docs and I've searched the net, but I can't seem to find a good example that
helps. Programming isn't my day job so I'm not that great at it. Any help
would be great.


Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070713/e8f32cfb/attachment-0001.html 


More information about the Tutor mailing list