1

I'm trying to execute a shellscript from Python that takes ipaddress as a parameter,

I'm using the below command but get an error, I need to execute this shellscript as a Sudo user..

Error:-

[root@linuxhost web]# python test.py
29575
usage: sudo [-D level] -h | -K | -k | -V

usage: sudo -v [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-u user

            name|#uid]



process = subprocess.Popen(['sudo','/usr/local/bin/test.sh','127.0.0.1'],stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
process.wait()

I tried calling the shellscript by directly calling using sudo and still it fails..

process = subprocess.Popen(['sudo /usr/local/bin/test.sh','127.0.0.1'],stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
user1050619
  • 19,822
  • 85
  • 237
  • 413
  • try sudo python test.py (and remove sudo from the script) – eugecm Feb 24 '14 at 22:55
  • Sometimes it is handy for scripts to ask for root password instead of failing and requiring the user to re-run the script. – Krumelur Feb 24 '14 at 23:00
  • related: [Understanding python subprocess.check_output's first argument and shell=True](http://stackoverflow.com/q/21029154/4279) – jfs Feb 28 '14 at 02:10
  • unrelated: don't use `subprocess.PIPE` unless you consume the pipes. It may stall the child process. – jfs Feb 28 '14 at 02:11

1 Answers1

2

You can't combine list args with shell=True. Use something like

process = subprocess.Popen('sudo /usr/local/bin/test.sh 127.0.0.1',
    stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)

or do not use shell=True

process = subprocess.Popen(['sudo','/usr/local/bin/test.sh','127.0.0.1'],
    stdout=subprocess.PIPE,stderr=subprocess.PIPE)
Krumelur
  • 31,081
  • 7
  • 77
  • 119
  • You mean none of them are? What are the problems you encounter? – Krumelur Feb 24 '14 at 23:54
  • with list args and no shell=True, I get this error------ File "/usr/lib/python2.7/subprocess.py", line 679, in __init__ errread, errwrite) File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory – user1050619 Feb 25 '14 at 00:56
  • This usually means the executable was not found. Try `/usr/bin/sudo` instead of just `sudo` (in case sudo is not in your path). – Krumelur Feb 25 '14 at 06:52