1

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.

8
  • To terminate my script Commented Aug 14, 2015 at 18:05
  • What do you want the connection to do after the script terminates? Commented Aug 14, 2015 at 18:07
  • I want the connection to remain open in the shell Commented Aug 14, 2015 at 18:08
  • 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? Commented Aug 14, 2015 at 18:10
  • 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? Commented Aug 14, 2015 at 18:18

1 Answer 1

1

Try this:

import subprocess
subprocess.call(["ssh", "myuser@myserver"])
Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible to include the password in the command so I don't have to re-type it in every time? Thanks
as for security reasons it is not possible to use a password within the ssh command, use ssh-keys instead (much more comfortable and secure)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.