1
votes

I'm trying to write a script that will ssh into a box for me. I'm using Python and leveraging the paramiko library. I can successfully ssh on the box, but as soon as the script terminates, the ssh connection also terminates. I want to keep the connection open after the script has completed running.

Python:

self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh.connect(host, username=self.username, password=self.password)
stdout = execute(self.ssh, 'pwd') # test command for now to verify i'm on box
print stdout
sys.exit()

Console:

$ ssh.py

[u'/home/myuser\n']

myuser@xxxx ~

$

I haven't been able to find similar examples online, so any help would be appreciated.

1
To terminate my script - Matt Gervasio
What do you want the connection to do after the script terminates? - Robᵩ
I want the connection to remain open in the shell - Matt Gervasio
Do you mean that you want an interactive connection, that you want to be able to type commands and have them interpreted by the remote shell? - Robᵩ
Yes. All I essentially want the script to do is run 'ssh myuser@myserver'. After connecting, I want my script to exit and leave the terminal open with the ssh connection still alive, so I can do whatever manual things I need to do on that machine. Does that make sense? - Matt Gervasio

1 Answers

1
votes

Try this:

import subprocess
subprocess.call(["ssh", "myuser@myserver"])