Python Paramiko send CTRL+C to an ssh shell

11,617

Solution 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))

Solution 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.

Share:
11,617
bladexeon
Author by

bladexeon

Typically I tend to do coding in Python and occasionally Perl. I also have some experience in C. College Junior pursuing a Bachelors Degree in Electrical Engineering.

Updated on June 22, 2022

Comments

  • bladexeon
    bladexeon almost 2 years

    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)

  • bladexeon
    bladexeon over 8 years
    That did it my friend thanks for explaining my error here
  • Samuel Newport
    Samuel Newport almost 4 years
    How would one send CTRL+A for example would it just be "\x01" instead? if so what encoding scheme is this? Might be worth adding to the answer for completeness? (still upvoted though was very helpful)