为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

哈士奇WWW 2023-03-08 15:51:07
我想使用带有 get_pty=False 选项的 paramiko 客户端。我们有办法发送中断信号吗?我有一些想法喜欢使用 client.close() 但我更想知道为什么发送"\0x03"失败get_pty=False。import paramikoclient = 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())  # Falsestdin.write("\x03".encode())stdin.channel.close()print(stdout.read().decode(errors='ignore')) # File outputprint(stdin.channel.exit_status_ready())  # Truestdin, 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())  # Falsestdin.channel.close()   # hangs in next step for stdin.write("\x03".encode())print(stdout.read().decode(errors='ignore')) # File outputprint(stdin.channel.exit_status_ready())  # True我们还有其他方法可以使用stdin.write()for发送中断信号吗get_pty=False?
查看完整描述

1 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

我相信这Ctrl+C是一个终止信号。如果没有终结符 ( get_pty=false),则没有任何特殊含义。

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

stdin.channel.close()

你已经在做什么了。


查看完整回答
反对 回复 2023-03-08
  • 1 回答
  • 0 关注
  • 311 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号