For convenience I have written a small wrapper class to login on a remote host, execute a command, end retrieve the data:
def MySSHClient:
def connect(self, remoteHost, remotePort, userName, password):
self.__s = paramiko.SSHClient()
self.__s.load_system_host_keys()
self.__s.connect(remoteHost, remotePort, userName, password)
def exec_command(self, command):
bufsize = -1
chan = self.__s.get_transport().open_session()
chan.exec_command(command)
stdin = chan.makefile('wb', bufsize)
stdout = chan.makefile('r', bufsize)
stderr = chan.makefile_stderr('r', bufsize)
stdin.close()
exitcode = chan.recv_exit_status()
r = MySSHCommandResult(command, stdin, stdout, stderr, exitcode)
chan.close()
return r
def close(self):
self.__s.close()
This code is adapted from the original paramiko python implementation. I just added the last 5 lines.
(FYI: MySSHCommandResult reads all data from stdout and strerr during construction and stores it for further use.)
The class MySSHClient is used within a simple python program:
....
exitCode = 0
s = None
try:
....
exitCode = 3
s = MySSHClient()
s.connect(host, port, login, password)
exitCode = 4
result = s.exec_command(myCommand)
exitCode = 5
if not result.isSuccess():
raise Exception("Failed to execute command!")
result.dump() # for current debugging purposes
exitCode = 0
except:
pass
if s is not None:
s.close()
sys.exit(exitCode)
(Through these exit codes the python program tells the caller if everything succeeded. As you can see a variety of exit codes is used in order to allow a bit of error diagnosis on failure.)
So far so good. Basically this works. But what I do not understand is that sometimes my python program gives additional ouput like this:
Exception ignored in: <bound method BufferedFile.__del__ of <paramiko.ChannelFile from <paramiko.Channel 0 (closed) -> <paramiko.Transport at 0x74300588 (unconnected)>>>>
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/paramiko/file.py", line 61, in __del__
File "/usr/local/lib/python3.5/dist-packages/paramiko/file.py", line 79, in close
File "/usr/local/lib/python3.5/dist-packages/paramiko/file.py", line 88, in flush
TypeError: 'NoneType' object is not callable
Or like this:
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/paramiko/file.py", line 61, in __del__
File "/usr/local/lib/python3.5/dist-packages/paramiko/file.py", line 79, in close
File "/usr/local/lib/python3.5/dist-packages/paramiko/file.py", line 88, in flush
TypeError: 'NoneType' object is not callable
Everything works fine all the time, but at about 10% to 20% of the time I see these error messages. Has anyone an idea why sometimes the cleanup fails on program termination? How can I avoid these error messages?