我有一个要求,我试图编写一个在内部调用 curl 命令的 shell 脚本。我将密码、用户名和 url 作为变量存储在脚本中。但是,由于我想避免在脚本中使用 curl 命令的 user:password 格式,因此我只是使用 curl --user 命令。我的目的是通过标准输入传递密码。所以,我正在尝试这样的事情 -
#!/bin/bash
user="abcuser"
pass="trialrun"
url="https://xyz.abc.com"
curl --user $user $url 2>&1 <<EOF
$pass
EOF
但这行不通。我知道所问的这个问题有多种变化,但我并没有得到确切的答案,因此发布了这个问题。
最佳答案
您可以使用:
curl -u abcuser:trialrun https://xyz.abc.comp
在你的脚本中:
curl -u ${user}:${pass} ${url}
从标准输入读取:
curl https://xyz.abc.com -K- <<< "-u user:password"
使用时
-K, --config
指定 -
使 curl 从标准输入读取文件这应该适用于 HTTP Basic Auth ,来自 curl man:
-u, --user <user:password>
Specify the user name and password to use for server authentication.
关于shell - 在命令行上将密码传递给 curl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46540047/