Search code examples
pythonlinuxos.system

Python, run terminal, and execute command in it


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?


Solution

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