skip to Main Content

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


  1. First of all you can put your code inside try and except block –

    try:
        ssh.connect("my.server.com", 22, userfoo, key_filename="/home/gameveloster/.ssh/id_ed25519")
    except paramiko.ssh_exception.SSHException:
        # catch the exception
    

    And then in ssh command pass the T parameter –

    ssh -T [email protected] -i ~/.ssh/id_ed25519
    
    Login or Signup to reply.
  2. 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)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search