0

I am trying to use subprocess module for executing remote command. I need to execute program called 'upgrade' that is located on remote server. Also, I need to use an argument in that call. Here is how I tried to do that:

import subprocess
p=subprocess.Popen(['ssh', '15.36.77.1', './home/upgrade -u' ],stdin=subprocess.PIPE,
stdout=subprocess.PIPE,stderr=subprocess.PIPE, shell=True)
stdout,stderr=p.communicate(input='\n')

When I try to execute this command and get return code form it, I get 255, which means that command failed. Could you help me with this issuse? No third part libraries should be used. Sorry for bad english.

user5142625
  • 85
  • 1
  • 1
  • 9
  • Possible duplicate of: http://stackoverflow.com/questions/31558225/subprocess-remote-command-execution-in-python – LRP Jul 30 '15 at 08:46

1 Answers1

0

I think you're only problem is that when using shell=True you execute everything which you give to subprocess.Popen as a string. The two ways to do this with and without shell=True are as follows:

subprocess.call(["ls", "-l"])

subprocess.call("exit 1", shell=True)

So in your case the string which you need to give to Popen is:

['ssh 15.36.77.1 ./home/upgrade -u']

or you can remove shell=True to make the command:

p=subprocess.Popen(['ssh', '15.36.77.1', './home/upgrade -u' ],
                   stdin=subprocess.PIPE, subprocess.PIPE,stderr=subprocess.PIPE)

Note that using shell=True is not recommended as this can be a potential security hazard if asking for intput from the command line. See the example given in the documentation:

https://docs.python.org/2/library/subprocess.html#frequently-used-arguments

P.S. Your English is great!

LRP
  • 118
  • 8
  • Unfortunately, I get error:"No such file or folder", althought there is file 'upgrade' located in /home. Why is that so? – user5142625 Jul 30 '15 at 11:01