Paramiko Question

MRAB python at mrabarnett.plus.com
Thu Nov 3 11:38:33 EDT 2011


On 03/11/2011 09:22, Jacob Abraham wrote:
> Hi All,
>
>        I need help with the below mentioned script. The second time I
> call a.execute, self.transport.open_session() fails with an EOF error.
> Is this a paramiko bug or am I doing something wrong?
>
>
> import paramiko
>
> class Connection(object):
>      def __init__(self, host, username = None, password = None,
> private_key = None, \
>                   port = 22):
>          self.transport = paramiko.Transport((host, port))
>          self.live = True
>          self.transport.connect(username = username, password = password)
>
>      def execute(self, command):
>          channel = self.transport.open_session()
>          channel.exec_command(command)
>          output = channel.makefile('rb', -1).readlines()
>          if output:
>              print output
>          else:
>              print channel.makefile_stderr('rb', -1).readlines()
>
>      def close(self):
>          if self.live:
>              self.transport.close()
>              self.live = False
>
>      def __del__(self):
>          self.close()
>
> if __name__ == '__main__':
>      a= Connection('ip_or_hostname', 'root', '')
>      print a.execute('version')
>      print a.execute('version')
>      print a.execute('version')
>      a.close()
>
I notice that in the 'execute' method you're opening the session, but 
not closing it.
Could that be the cause of the problem?

I also notice that in that method you're printing the output, not 
returning it, but when you call the execute method you're trying to
print the result, which will be None.



More information about the Python-list mailing list