【问题标题】:Python subprocess AWS credentials shell script causing a directory errorPython 子进程 AWS 凭证 shell 脚本导致目录错误
【发布时间】:2017-11-24 17:39:13
【问题描述】:

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

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 但我有同样的错误。我想为此使用子进程,因为它被认为是最安全的方法。一些指导将不胜感激。

【问题讨论】:

    标签: python bash shell amazon-web-services amazon-ec2


    【解决方案1】:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-30
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多