0

I am using the following python code to get a continuous log from a linux server:

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.168.1', 22, username="root", password="", timeout=5)
stdin, stdout, stderror = ssh.exec_command('journalctl -f', get_pty=True)
for log in iter(stdout.readline, ""):
    print(log, end="")

I would like to break this feed by any key press, but once this is running, there is no other way to stop it other than exiting the program.

Any ideas would be greatly appreciated.

1 Answers1

0

To close only the process, use Channel.close():

stdin.channel.close()

Or you can close whole connection:

ssh.close()
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • How would I get stdin.channel.close() to invoke from a key press of some kind? I just want to be able to kill the log output in some way. – Oliver Feb 09 '22 at 06:13
  • See [How to kill a while loop with a keystroke?](https://stackoverflow.com/q/13180941/850848) – Martin Prikryl Feb 09 '22 at 08:42