Open SSH connection on exit Python
Question: I am writing a little script which picks the best machine out of a few dozen
to connect to. It gets a users name and password, and then picks the best
machine and gets a hostname. Right now all the script does is print the
hostname. What I want is for the script to find a good machine, and open an
ssh connection to it with the users provided credentials.

So my question is how do I get the script to open the connection when it
exits, so that when the user runs the script, it ends with an open ssh
connection.

I am using sshpass.


Answer: You can use the os.exec* function to replace the Python process with the
callee:

    
    
    import os
    os.execl("/usr/bin/ssh", "user@host", ...)
    

<https://docs.python.org/2/library/os.html#os.execl>

