When I connect to my Ubuntu server via SSH using the terminal, it sometimes return a certain string then closes the connection.
$ ssh hello@my.server.com -i ~/.ssh/id_ed25519
container not found
Connection to 123.10.10.231 closed.
Connection to my.server.com closed.
Using paramiko
in my Python script to connect to this server, how do I detect when this is happening?
Currently I'm doing the following to make the connection:
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
ssh.connect("my.server.com", 22, userfoo, key_filename="/home/gameveloster/.ssh/id_ed25519")
stdin, stdout, stderr = ssh.exec_command('ls')
print(stdout.readlines(), stderr.readlines())
and getting the following output even when the server is not closing the SSH connection immediately on connecting.
DEBUG:paramiko.transport:[chan 0] EOF sent (0)
["Error: Your SSH client doesn't support PTY\n"] []
The server seems to need terminal to print that message.
In general, it's not a good idea to enable terminal emulation, when automating commands. But if you need that message, you will probably have to do it. Use get_pty
parameter of SSHClient.exec_command
:
stdin, stdout, stderr = ssh.exec_command('ls', get_pty=True)