Search code examples
pythonsshoperating-systemgnu-screen

SSH and Screen with a Python script


I'm trying to do the following from a python script:

  1. connect to remote server
  2. in that remote server create a Screen
  3. in that screen run a few commands
  4. exit

So I try the following:

import os
os.system('ssh -t -t myname@server')
os.system('nice -11 screen')

However, the last command does not execute on the server. I get into the server, but have no connection to it from the python script anymore. What I want to do now is create a screen session.


Solution

  • How about this:

    import os
    os.system('ssh -t -t myname@myserver "nice -11 screen"')
    

    This leaves you at the screen.

    Or if you just want to run a few commands:

    import os
    os.system('ssh -t -t myname@myserver "ls && pwd"')
    

    This runs both these commands, then exits.

    Edit:

    The following will leave you at a shell prompt:

    os.system('ssh -t -t myname@myserver "nice -11 screen -U"')
    

    The following will run a command, then leave you at a shell prompt:

    os.system('ssh -t -t myname@myserver "ls > ~/x.txt && nice -11 screen -U"')