如何使用 get_pty=False 将 Ctrl+C 发送到 Python Paramiko 客户端?

How to send Ctrl+C to Python Paramiko Client with get_pty=False?

我想使用带有 get_pty=False 选项的 paramiko 客户端。我们有办法发送中断信号吗?我有一些想法喜欢使用 client.close() 但我更想知道为什么发送 "[=13=]x03" 失败 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

对于get_pty=False.

,我们有没有其他方法使用stdin.write()发送中断信号

我认为Ctrl+C是终端信号。没有终端(get_pty=false),没有什么特殊意义。

如果要终止远程命令,只需关闭“exec”通道即可。

stdin.channel.close()

你已经在做什么了。