Friday, 15 January 2010

Python interactive ssh client using paramiko -


I'm not a programmer, but I want to use Python for automation of some administrative purposes. After "Hello World" I have tried to create the first interactive SSH client. I've read some documentation and articles and have decided that this would be the easiest way to use Paramio module, but unfortunately I have to face a problem: My application gives you some essential information like server IP, username, password Asks to enter. After that it establishes a connection to the defined server and gives you clear on your screen. I use Loops to copy the process of registering. Unfortunately my application works well only with the first command entered by you. An error appears when trying to type the second command:

  Traceback (the most recent call last): The file "C: \ Python 27 \ test \ ssh_client.py", line 53, & Lt; Module & gt; Client.execute_command (command) file "C: \ Python27 \ test \ ssh_client.py", line 26, execute_command stdin, stdout, stderr = self.connection.exec_command (command) file "C: \ Python27 \ lib \ site-package \ Paramiko \ client.py ", line 343, exec_command chan.exec_command (command) in AttributeError: The 'NoneType' object is an attribute 'exec_command'   

programm code (Windows 7): < / P>

  import paramiko SERVER = raw_input ('Please enter an IP address of the remote host:') USER = raw_input ('Please enter your username:') PASSWORD = raw_input ('Please enter password: ') Class MYSSHClient (): def __init __ (auto, server = serv R, user name = user, password = password): self.server = server self.username = user name self.password = password self.connection = none self.result = '' self.is_error = false def do_connect (self) : Self.connection = paramiko.SSHClient () self.connection.set_missing_host_key_policy (paramiko.AutoAddPolicy ()) self.connection.connect (self.server, user name = self.username, password = self.password) def execute_command (auto, Order): If order: print command stdin, stdout, stderr = self.connection.exec_command (command) stdin.close () error = str (stderr.read ()) Error: self.is_error = True self.result = error Print 'error' else: self.is_error = Fals E self.result = str (stdout.read ()) print 'no error' print self.result Other: print "no command was entered" def do_close (self): self.connection.close () if __name__ = = '__main__': client = MYSSHClient () client.do_connect (), while 1: command = raw_input ('CLI:') if command == 'Q': break client.doecute_command (command) client.do_close ()   

I tried to delete the loop and tried to make a right call in the code, but there is only one problem (see the same error while typing second command). It seems that I do not fully understand how the Paramio module works. I tried to find information on the web, but unfortunately no solution was found.

I would greatly appreciate if someone told me what I did wrong or give me a link on this issue where I can find a solution.

Thanks in advance for any help. Please use Pxssh module for this. It is very useful for your application if it works for windows. The example is very useful for you

I think you should check your server settings in the remote server machine

No comments:

Post a Comment