Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Paramiko SSH session not active after being idle for many hours

I am using Python Paramiko ssh.exec_command to send commands to a slave machine from the master machine (from CentOS to CentOS). The master sends the command to the slave and waits (sleeps or does something meaningful) for it to be completed by the slave. The slave takes approximately 10 hours to complete the command. After 10 hours if the master sends the next command to the slave using ssh.exec_command, I get an error message saying SSH session is not active. I tried setting ServerAliveinterval, TCPKeepAlive in ssh_config file on both master and slave side. But nothing worked. What are all the other possibilities that I need to check to keep the SSH session active forever.

like image 672
S_Ananth Avatar asked May 19 '26 14:05

S_Ananth


1 Answers

ServerAliveInterval has no effect on Paramiko.

Use Transport.set_keepalive:

transport = client.get_transport()
transport.set_keepalive(60)

If that does not help (typically because the server ignores keepalives), you have to keep the session alive yourself. E.g. by making the command produce an output continuously.

like image 179
Martin Prikryl Avatar answered May 21 '26 06:05

Martin Prikryl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!