简体   繁体   English

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

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

I have to run the following commands in order to obtain all the required "credentials" to run my Python scripts within EC2. 我必须运行以下命令以获得所有必需的“凭据”,才能在EC2中运行我的Python脚本。 So I decided to use subprocess in order to streamline this procedure. 因此,我决定使用子流程来简化此过程。

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}"])

and I got an error: 我得到一个错误:

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

I am new to bash and subprocess so forgive me if my mistake something trivial. 我是bash和subprocess的新手,所以如果我的错误有些琐碎,请原谅我。 I tried running python ./script.py but I have the same error. 我尝试运行python ./script.py,但出现了相同的错误。 I would like to use subprocess for this as it is supposedly the safest way of doing this. 我想为此使用子流程,因为它据说是最安全的方式。 Some guidance would be really appreciated. 一些指导将不胜感激。

The first argument to subprocess.call has to be a program or executable. subprocess.call的第一个参数必须是程序或可执行文件。 In your case, it is not. 就您而言,事实并非如此。 Looks like you want to execute the call in a shell, so set this argument shell=True . 看起来您想在shell中执行调用,因此设置此参数shell=True Note: using shell=True is a security hazard. 注意:使用shell=True是安全隐患。

Warning Executing shell commands that incorporate unsanitized input from an untrusted source makes a program vulnerable to shell injection, a serious security flaw which can result in arbitrary command execution. 警告执行包含来自不受信任源的未经处理的输入的Shell命令会使程序容易受到Shell注入的攻击,这是一个严重的安全漏洞,可能导致任意命令执行。 For this reason, the use of shell=True is strongly discouraged in cases where the command string is constructed from external input. 出于这个原因,在命令字符串是由外部输入构造的情况下,强烈建议不要使用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