Matt Gervasio
Matt Gervasio

Reputation: 55

Python ssh - keep connection open after script terminates

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.

Upvotes: 1

Views: 2628

Answers (1)

Robᵩ
Robᵩ

Reputation: 168836

Try this:

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

Upvotes: 1

Related Questions