Execute bash commands on remote machine using python script

时间:2018-03-23 00:38:59

标签: python ssh

I have a python script that establishes ssh connected using os.system() function :

def livestream():
   os.system('ssh -X ubuntu@192.168.0.2')
   os.system('cd Desktop') #doesn't get executed on remote machine

I know that os.system() executes commands on the same machine.

Further, I need to execute a program present on the remote machine.

My question is how do I execute bash commands in python once ssh connection is established?

2 个答案:

答案 0 :(得分:2)

def livestream():
   os.system('ssh -X ubuntu@192.168.0.2 "cd Desktop"')

答案 1 :(得分:1)

You can use the call method from the subprocess module.

https://docs.python.org/2/library/subprocess.html