Advertisement
jredmond

Working Force10 Paramiko example

May 24th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | None | 0 0
  1. user@host:~/$ cat test.py
  2. #!/usr/bin/env python
  3. import paramiko, time
  4.  
  5. RECEIVE_BUFFER_SIZE = 99999
  6. CUR = None
  7.  
  8. class Switch:
  9.     def __init__(self, ip, username, password):
  10.         self.ip = str(ip)
  11.         self.username = username
  12.         self.password = password
  13.  
  14.         # Establish SSH connection
  15.         try:
  16.             self.connection = paramiko.SSHClient()
  17.             self.connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  18.             self.connection.connect(self.ip, username=self.username, password=self.password)
  19.         except paramiko.SSHException, reason:
  20.             print "Could not connect to host %s, reason: %s" %(self.ip, reason)
  21.             self.enabled = False
  22.             return
  23.         except EOFError, reason:
  24.             print "Host %s disconnected, reason: %s" %(self.ip, reason)
  25.             self.enabled = False
  26.             return
  27.  
  28.         # Create channel
  29.         self.channel = self.connection.invoke_shell()
  30.         buffer = ''
  31.         while not buffer.endswith('>'):
  32.             resp = self.channel.recv(RECEIVE_BUFFER_SIZE)
  33.             buffer += resp
  34.  
  35.  
  36.     def test(self):
  37.         self.channel.send("show version")
  38.         self.channel.send("\n")
  39.         buffer = ""
  40.         while "e" not in buffer:
  41.             resp = self.channel.recv(RECEIVE_BUFFER_SIZE)
  42.             buffer += resp
  43.             time.sleep(0.1)
  44.  
  45.         print buffer
  46.  
  47.  
  48. if __name__ == "__main__":
  49.     s = Switch( "10.1.1.1", "admin", "password" )
  50.     s.test()
  51. user@host:~/$ python test.py
  52. show version
  53. Dell Force10 Networks Real Time Operating System Software
  54. Dell Force10 Operating System Version: 1.0
  55. Dell Force10 Application Software Version: 8.4
  56. Copyright (c) 1999-2011 by Dell Inc.
  57. Build Time: Tue Nov 15 09:32:07 2011
  58. E600i uptime is 3 year(s), 3 week(s), 4 day(s), 11 hour(s), 31 minute(s)
  59.  
  60. (truncated for brevity)
  61. user@host:~/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement