Start interactive SSH session from Python script

11,733

I get an interactive terminal if I use os.system('ssh [...]')

Share:
11,733
sholsapp
Author by

sholsapp

SOreadytohelp

Updated on July 25, 2022

Comments

  • sholsapp
    sholsapp almost 2 years

    I'd like to start an interactive SSH terminal from a Python script without using modules like pexpect or paramiko - I want to stick with what CentOS pre-installed Python provides me (to ease compatibility and deployment issues).

    I can run commands fine using the subprocess module, but cannot get an interactive terminal. In Perl, I would just use backticks to achieve this, but am looking for the pythonic way of doing this.

    Can someone point me in the right direction?

    UPDATE - based on @leoluk's answer, I used the instructions from docs.python.org to come up with: subprocess.call("ssh ...", shell=True)

  • sholsapp
    sholsapp over 13 years
    Using the instructions to convert os.system to Popen, I was able to make it work.
  • snim2
    snim2 about 11 years
    The OP did say s/he didn't want to use pexpect!
  • mart1n
    mart1n about 8 years
    @sholsapp Can you please share your solution using Popen? Thank you!
  • sholsapp
    sholsapp about 8 years
    @mart1n I've updated the question to use the code that I came up with based on the docs.python.org/2/library/subprocess.html#replacing-os-syste‌​m instructions for replacing os.system.