0

I want to use paramiko client with get_pty=False option. Do we have a way to send interrupt signal? I got some idea like to use client.close() but I am more interested to know why sending "\0x03" fails with get_pty=False.

import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
connection_param= {'hostname': '192.168.255.1',
            'username': 'username',
            'password': 'password',
            'port': '22',
            'timeout': 15,
            'key_filename': None,
            'pkey': None}
client.connect(**connection_param)
stdin, stdout, stderr = client.exec_command("tail -f /tmp/test.log", bufsize=-1, timeout=None, get_pty=True)
time.sleep(1)
print(stdin.channel.exit_status_ready())  # False
stdin.write("\x03".encode())
stdin.channel.close()
print(stdout.read().decode(errors='ignore')) # File output
print(stdin.channel.exit_status_ready())  # True
stdin, stdout, stderr = client.exec_command("tail -f /tmp/test.log", bufsize=-1, timeout=None, get_pty=False)
time.sleep(1)
print(stdin.channel.exit_status_ready())  # False
stdin.channel.close()   # hangs in next step for stdin.write("\x03".encode())
print(stdout.read().decode(errors='ignore')) # File output
print(stdin.channel.exit_status_ready())  # True

Do we have any other way to send interrupt signal using stdin.write() for get_pty=False.

Osadhi Virochana
  • 1,294
  • 2
  • 11
  • 21
  • [Python Paramiko send CTRL+C to an ssh shell](https://stackoverflow.com/questions/33290207/python-paramiko-send-ctrlc-to-an-ssh-shell) – furas Jul 15 '20 at 13:39

1 Answers1

2

I believe that Ctrl+C is a terminal signal. Without the terminal (get_pty=false), it does not have any special meaning.

If you want to terminate the remote command, just close the "exec" channel.

stdin.channel.close()

What you are doing already.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992