1

I am using spyder on ubuntu 16.04. I want to write a script that will execute several commands in the same linux terminal.

First I want to open a terminal, then ssh into another computer, then enter my password, then continuing entering commands.

I have tried os.system(command) but this does not open a new terminal for me nor run the command I want.

os.system("gnome-terminal -e 'bash -c \"ssh blah blah blah; exec bash\"'") works but I am stuck when trying to enter my password.

How can I enter commands with a Python script in this context?

Matthew Strawbridge
  • 19,940
  • 10
  • 72
  • 93
Robot12341
  • 11
  • 1
  • 2

3 Answers3

0

If you're looking to automatically connect & login to an ssh server with a password, you can use sshpass to enter the password, but it'd be better to use an SSH key. Github has a good tutorial on how to generate a key here.

If you're looking to execute commands after SSHing into your server, you can do so with: os.system("ssh user@host 'command1 && command2'")

Weston Reed
  • 187
  • 2
  • 9
0

For executing commands on a terminal you can use python's subprocess module. There's already a question here on how to execute multiple commands using a single terminal with subprocess.

0

You can use the subprocess module along with the gnome-terminal command if you're using Gnome.

The idea is to spawn a GUI terminal, and to execute a script into it through the -e option. Here is a very simple example, on how to execute a shell script in a new terminal.

myscript.sh

#!/bin/bash

pwd
ls
cat

spawn_and_run.py

import subprocess

subprocess.Popen(["gnome-terminal", "-e", "myscript.sh"])

Running python spawn_and_run.py will spawn a new terminal, print the working directory (pwd), print the content of that directory (ls), and then echo the input from stdin (cat without arguments).

Right leg
  • 16,080
  • 7
  • 48
  • 81
  • @Robot12341 Glad it helped! Don't hesitate to give a +1, to accept an answer, or simply to provide feedback, we're all learning here :) – Right leg Feb 07 '18 at 01:01