【问题标题】:curl command line POST passwordcurl 命令行 POST 密码
【发布时间】:2015-12-31 19:39:21
【问题描述】:

我有以下 curl 命令可以正常工作:

curl --silent --location -cookie "$COOKIE" --cookie-jar "$COOKIE" \
--form 'username=xxx' --form 'password=yyy' 'http://example.com'

它登录到站点http://example.com 发布一个带有变量名用户名和密码的表单。

问题是:我不想明文传递密码。

我还尝试将密码保存在工作目录中名为 passwd 的文件中(chmod 600 passwd)并使用以下 curl 命令(这就是我使用 --form 而不是 --data 的原因,这本来是使用明文密码可以正常工作),但是,它不起作用:

curl --silent --location -cookie "$COOKIE" --cookie-jar "$COOKIE" \
--form 'username=xxx' --form 'password=<passwd' 'http://example.com'

关于如何解决这个问题的任何建议?

问候, 斯特凡诺

【问题讨论】:

    标签: forms post curl passwords


    【解决方案1】:

    我认为 Hans Z. 使用环境变量的答案是正确的。虽然我可能会在 bash 中添加它,但您可以使用 read 命令,它会提示输入密码并且不会使其在历史记录中可见。

    所以用法看起来像这样

    $ read -s PASSWD  # -s is there so the characters you type don't show up
    $ curl --silent --location -cookie "$COOKIE" --cookie-jar "$COOKIE" \
    --form 'username=xxx' --form "password=${PASSWD}" 'http://example.com'
    

    更新:

    在 cmets 中找到的另一个解决方案是使用 curl--data-urlencode name@filename 参数。 引用手册页:

    名称@文件名 这将使 curl 从给定文件中加载数据(包括任何换行符),对该数据进行 URL 编码并在 POST 中传递。

    最后的命令看起来像

    $ curl --silent --location -cookie "$COOKIE" --cookie-jar "$COOKIE" \
    --data 'username=xxx' --data-urlencode "password@passwd" 'http://example.com'
    

    【讨论】:

    • 感谢您的快速回复。我忘了提到我想在 cron 中安排 curl 命令,所以使用环境变量将无济于事,因为我不想将 export PASSWD=secret 放在 crontab 文件中的 cron 命令前面。
    • 为什么不把它做成这样呢? PASSWD=$(cat path-to-file-with-your-password); cron ... --form "password=$PASSWD" ...(格式不好,应该在一行中)
    • @stx73 - 你可以设置一个永久的环境变量,你不必每次都导出。
    • 它确实有效,但是,我认为--form 'password=&lt;passwd' 的解决方案更优雅,但它现在不起作用......我应该添加;type=&lt;mime type&gt; 吗?像--form 'password=&lt;passwd;type=&lt;mime type&gt;' 一样,以防万一,哪个?我想让它工作;在curl命令中添加--trace-ascii -表示密码是从passwd文件中读取的,但最终还是不起作用。
    • 奇怪的是它不能替代文件的内容。也许试试这个curl --data-urlencode "password@passwd"
    【解决方案2】:

    在环境变量中设置密码,例如export PASSWD=secret 并在 --form "password=${PASSWD}" 中使用它。

    【讨论】:

      【解决方案3】:

      在命令行中传递秘密是不安全的,因为它们在进程列表中可用。

      您应该使用--data-urlencode name@filename 方法

                       This will make curl load data from the given file
                       (including any newlines), URL-encode that data and
                       pass it on in the POST. 
      

      -K, --config &lt;file&gt;

                Specify a text file to read curl arguments from. The
                command line arguments found in the text file will be used
                as if they were provided on the command line.
      

      另见this anwser

      【讨论】:

        猜你喜欢
        • 2011-08-15
        • 1970-01-01
        • 2018-03-14
        • 2011-03-01
        • 2012-08-03
        • 1970-01-01
        • 2017-08-08
        • 2011-09-07
        相关资源
        最近更新 更多