繁体   English   中英

Python子流程AWS凭证Shell脚本导致目录错误

[英]Python subprocess AWS credentials shell script causing a directory error

我必须运行以下命令以获得所有必需的“凭据”,才能在EC2中运行我的Python脚本。 因此,我决定使用子流程来简化此过程。

subprocess.call(["export instance_profile=`curl 
http://169.254.169.254/latest/meta-data/iam/security-credentials",
"export AWS_ACCESS_KEY_ID=`curl http://169.254.169.254/latest/meta-
data/iam/security-credentials/${instance_profile} | grep AccessKeyId 
| cut -d':' -f2 | sed 's/[^0-9A-Z]*//g'`",
"export AWS_SECRET_ACCESS_KEY=`curl 
http://169.254.169.254/latest/meta-data/iam/security-
credentials/${instance_profile} | grep SecretAccessKey | cut -d':' -
f2 | sed 's/[^0-9A-Za-z/+=]*//g'`",
"export AWS_SECURITY_TOKEN=`curl http://169.254.169.254/latest/meta-
data/iam/security-credentials/${instance_profile} | grep Token | cut 
-d':' -f2 | sed 's/[^0-9A-Za-z/+=]*//g'`",
"export http_proxy=proxy.xxx.xxxxxxxxx.com:8099",
"export https_proxy=${http_proxy}"])

我得到一个错误:

File "funtest.py", line 25, in <module>
"export https_proxy=${http_proxy}"])
File "/usr/lib64/python2.7/subprocess.py", line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1327, in 
_execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

我是bash和subprocess的新手,所以如果我的错误有些琐碎,请原谅我。 我尝试运行python ./script.py,但出现了相同的错误。 我想为此使用子流程,因为它据说是最安全的方式。 一些指导将不胜感激。

subprocess.call的第一个参数必须是程序或可执行文件。 就您而言,事实并非如此。 看起来您想在shell中执行调用,因此设置此参数shell=True 注意:使用shell=True是安全隐患。

警告执行包含来自不受信任源的未经处理的输入的Shell命令会使程序容易受到Shell注入的攻击,这是一个严重的安全漏洞,可能导致任意命令执行。 出于这个原因,在命令字符串是由外部输入构造的情况下,强烈建议不要使用shell = True。

subprocess.call(["export instance_profile=`curl http://169.254.169.254/latest/meta-data/iam/security-credentials`",
"export AWS_ACCESS_KEY_ID=`curl http://169.254.169.254/latest/meta- data/iam/security-credentials/${instance_profile} | grep AccessKeyId | cut -d':' -f2 | sed 's/[^0-9A-Z]*//g'`",
"export AWS_SECRET_ACCESS_KEY=`curl http://169.254.169.254/latest/meta-data/iam/security- credentials/${instance_profile} | grep SecretAccessKey | cut -d':' - f2 | sed 's/[^0-9A-Za-z/+=]*//g'`",
"export AWS_SECURITY_TOKEN=`curl http://169.254.169.254/latest/meta-data/iam/security-credentials/${instance_profile} | grep Token | cut -d':' -f2 | sed 's/[^0-9A-Za-z/+=]*//g'`",
"export http_proxy=proxy.xxx.xxxxxxxxx.com:8099",
"export https_proxy=${http_proxy}"], shell=True)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM