It looks like you are really asking for the inverse of the old nohup command; this means that the remote command responds to the HUP (hang) signal that is generated by the TTY driver (PTY or psuedo-tty) when the basic connection to it is closed.
Personally, I suspect that Paramiko will be much better than trying to manage this with the subprocess module. You can make Paramiko open the connection, select pty and execute its commands without any problems; and the code for using existing files of known_files and identities (private keys) is relatively simple.
This recipe Copy files via SSH using paramiko in ActiveState shows the basics of ssh known_hosts downloads using identifier files and interacting with any ssh agent you can work with. From there, what you are asking for should be simple:
You can also use the TwistedConch implementation of the SSH protocols. However, wrapping your head around a Twisted frame can be quite complicated.
Here is an example of a simple ssh server in Twisted with pty support: Twisted Conch in 60 seconds: PTY requests Here, the SO stream was sent a week before yours. The best way to run remote commands via ssh in Twisted? but that didn’t deal with pty issues. Looking at the docs for twisted.conch.ssh.session suggests that it has pty support; but lists it as "Undocumented."
Of course, you can even go with Pexpect , run the shell on your local ssh client and send a remote command through it. This most accurately reflects how you run the command from your own shell.
source share