0

I am using Python 2.7.5, since this version is installed on the machine which I want to run script.

I have created a simple GUI in Tkinter, with button and text input. Now in one input I provide the ip, or hostname of server, in next step I read the value of input fields and send it to linux bash terminal, and here I have a problem.

Reading the value from input field(works good)

nazwa_ip = self.Input_IP_hostname.get("1.0", 'end-1c')

and next:

os.system('gnome-terminal --window-with-profile=MY_PROFILE -e "ssh -t user_name@nazwa_ip"')

and here is the problem, because it wont change "nazwa_ip" to the read value. That comand send to terminal:

ssh -t user_name@nazwa_ip

but i want to send:

ssh -t user_name@ip_adres_from_input_field

Can somebody help me to resolve the issue?

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • Appreciate your efforts, but why have you added the `bash` tag here, it doesn't look much relevant. – Inian Jan 21 '17 at 16:12
  • 1
    what about using the subprocess module to execute the command? – fedepad Jan 21 '17 at 16:20
  • Possible duplicate of [Calling an external command in Python](http://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – El Ruso Jan 21 '17 at 16:26

4 Answers4

1

according to the Python docs, it is recommended that os.system be replaced with the subprocess module .

status = os.system("mycmd" + " myarg")
# becomes
status = subprocess.call("mycmd" + " myarg", shell=True)
arde
  • 106
  • 5
0

String formatting will work here:

os.system('gnome-terminal --window-with-profile=MY_PROFILE -e "ssh -t user_name@%s"' % nazwa_ip)
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
0

Using the subprocess method might be better to do this.

import subprocess

nazwa_ip = self.Input_IP_hostname.get("1.0", 'end-1c')
ssh_param = "ssh -t user_name@{}".format(nazwa_ip)
subprocess.call(['gnome-terminal', '--window-with-profile=MY_PROFILE', '-e',  ssh_param])
SebastienPattyn
  • 393
  • 4
  • 18
0

Whilst running a subprocess is easy, starting one in a graphical terminal that behaves exactly like one the user launched is a little tricker. You could use my program interminal (link), which basically does what Stephen Rauch's answer does with gnome-terminal, but via a shell so that user environment variables and aliases etc are all available, which could be useful on the offchance that they affect how ssh runs.

You would use it from Python like this:

import subprocess
subprocess.Popen(['interminal', 'ssh', '-t', 'username@{}'.format(ip_address)])
Chris Billington
  • 855
  • 1
  • 8
  • 14