python - 如何使用 get_pty=False 将 Ctrl+C 发送到 Python Paramiko 客户端?
问题描述
我想使用带有 get_pty=False 选项的 paramiko 客户端。我们有办法发送中断信号吗?我有一些想使用 client.close() 的想法,但我更想知道为什么发送"\0x03"
失败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
我们还有其他方法可以使用stdin.write()
for发送中断信号吗get_pty=False
?
解决方案
我相信这Ctrl+C是一个终结信号。没有终端(get_pty=false
),它没有任何特殊含义。
如果要终止远程命令,只需关闭“exec”通道即可。
stdin.channel.close()
你已经在做什么了。
推荐阅读
- c# - 带有 Web API 的角度
- mongodb - 查找和展开mongodb后如何防止重复总和
- java - POST 请求适用于 POSTMAN,但不适用于我的 Java 应用程序
- docker - 启动容器时如何获取docker容器id
- javascript - 如何从 JSON 中获取对象,然后将它们用于我的站点?
- python - 如何为模型中的特定字段创建自定义权限?
- assembly - 在指令周期内如何执行微码?
- php - 与 PHP 服务器相比,Node.js 服务器(如 Express)如何管理内存?
- java - 如何从 vert.x SqlConnection 获取 PreparedStatement?
- javascript - 如何将异步修复应用于循环?