Python Paramiko send CTRL+C to an ssh shell

廉价感情. 提交于 2019-12-10 15:32:23

问题


I'm invoking a shell using Paramiko in order to use a CLI over an ssh connection. The problem with this CLI is if I do not close it specifically using CTRL+C, the program will not be able to be opened again without rebooting my system.

I've tried the below commands:

SSH.send("^C\n")
SSH.send("\x003")

is there another way to call these? Again, I've established an SSH connection using paramiko.SSHClient() and then invoked a shell using ssh.invoke_shell() and now i need to send CTRL+C to that shell to close the shell (not the ssh connection)


回答1:


You're on the right track with your second example, but it isn't quite formatted right. You're actually getting a 2 character string there.

SSH.send("\x03") should do the trick.

However, I'd probably have used this instead.

SSH.send(chr(3))




回答2:


Based on: https://stackoverflow.com/a/11190794/565212

You can -
either: pass get_pty=True when calling client.exec_command(). Then client.close() terminates the remote tail.
or: do channel.get_pty() before calling channel.exec_command(). Then channel.close() terminates the remote tail.



来源:https://stackoverflow.com/questions/33290207/python-paramiko-send-ctrlc-to-an-ssh-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!