I'm using Paramiko to tail -f
a file on a remote server.
Previously, we were running this via ssh -t
, but that proved flaky, and the -t
caused issues with our remote scheduling system.
My question is how to kill tail when the script catches a SIGINT?
My script (based on Long-running ssh commands in python paramiko module (and how to end them))
#!/usr/bin/env python2
import paramiko
import select
client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect('someserver', username='victorhooi', password='blahblah')
transport = client.get_transport()
channel = transport.open_session()
channel.exec_command("tail -f /home/victorhooi/macbeth.txt")
while True:
try:
rl, wl, xl = select.select([channel],[],[],0.0)
if len(rl) > 0:
# Must be stdout
print channel.recv(1024)
except KeyboardInterrupt:
print("Caught control-C")
client.close()
channel.close()
exit(0)
The script catches my Ctrl-C successfully, and ends. However, it leaves the tail -f
process running on the remote system,.
Neither client.close() nor channel.close() seem to terminate it.
What command can I issue in the except block to kill it?
The remote server is running Solaris 10.
To install paramiko library, run the subsequent command in the command prompt. paramiko needs cryptography as a dependency module. So run both commands in command prompt : After installation is completed, now we’ll hook up with a remote SSH server using paramiko library. Code snippet for an equivalent is given below:
Using exec_command () in Paramiko returns a tuple (stdin, stdout, stderr). Most of the time, you just want to read stdout and ignore stdin and stderr. You can get the output the command by using stdout.read () (returns a string) or stdout.readlines () (returns a list of lines). This website uses cookies to improve your experience.
Since exec_command is going to spawn a new thread for your command, you can just leave it open and proceed with other tasks. But make sure to empty buffers every now and then (for verbose remote commands) to prevent paramiko from stalling.
It allows you to connect to a server, or multiple servers, without having you remember or enter your password for each system that is to log in remotely from one system into another. To install paramiko library, run the subsequent command in the command prompt. paramiko needs cryptography as a dependency module.
You should use ssh keepalives... the problem you have is that the remote shell has no way of knowing (by default) that your ssh session was killed. Keepalives will enable the remote shell to detect that you killed the session
client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect('someserver', username='victorhooi', password='blahblah')
transport = client.get_transport()
transport.set_keepalive(1) # <------------------------------
# ... carry on as usual...
Set the keepalive value as low as you like (even 1 second)... after several seconds, the remote shell will see that the ssh login died, and it will terminate any processes that were spawned by it.
There is one way to do this. It works like on the shell
ssh -t commandname
The option -t is opening a pseudo pty to help ssh to track how long this process should last. the same can be done via pormiko via
channel.get_pty()
prior to execute_command(...). This will not open a shell like it does with channel.invoke_shell(), it just requests such a pseudo interface to tie all processes to. The effect can also be seen if ps aux is issued on the remote machine, the process is now attached to the sshd with a ptxXY interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With