1

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?

3
  • Appreciate your efforts, but why have you added the bash tag here, it doesn't look much relevant. Commented Jan 21, 2017 at 16:12
  • 1
    what about using the subprocess module to execute the command? Commented Jan 21, 2017 at 16:20
  • Possible duplicate of Calling an external command in Python Commented Jan 21, 2017 at 16:26

4 Answers 4

2

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)
Sign up to request clarification or add additional context in comments.

Comments

0

String formatting will work here:

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

1 Comment

Is this a string comprehension? Looks like string formatting to me.
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])

Comments

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)])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.