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)
asked Feb 24, 2014 at 22:52
4
  • try sudo python test.py (and remove sudo from the script) Commented Feb 24, 2014 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. Commented Feb 24, 2014 at 23:00
  • related: Understanding python subprocess.check_output's first argument and shell=True Commented Feb 28, 2014 at 2:10
  • unrelated: don't use subprocess.PIPE unless you consume the pipes. It may stall the child process. Commented Feb 28, 2014 at 2:11

1 Answer 1

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)
answered Feb 24, 2014 at 22:55
Sign up to request clarification or add additional context in comments.

3 Comments

You mean none of them are? What are the problems you encounter?
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
This usually means the executable was not found. Try /usr/bin/sudo instead of just sudo (in case sudo is not in your path).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.