我正在尝试从Python执行一个以ipaddress作为参数的shellcript,
我正在使用下面的命令但是出错了,我需要以Sudo用户身份执行这个shellcript ..
错误: -
[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()
我尝试通过直接使用sudo调用来调用shellcript但仍然失败..
process = subprocess.Popen(['sudo /usr/local/bin/test.sh','127.0.0.1'],stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
答案 0 :(得分:2)
您无法将列表参数与shell=True
组合在一起。使用像
process = subprocess.Popen('sudo /usr/local/bin/test.sh 127.0.0.1',
stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
或不要使用shell=True
process = subprocess.Popen(['sudo','/usr/local/bin/test.sh','127.0.0.1'],
stdout=subprocess.PIPE,stderr=subprocess.PIPE)