When I connect to my Ubuntu server via SSH using the terminal, it sometimes return a certain string then closes the connection.
$ ssh [email protected] -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 PTYn"] []
2
Answers
First of all you can put your code inside
try
andexcept
block –And then in
ssh
command pass theT
parameter –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 ofSSHClient.exec_command
: