shell - 在命令行上将密码传递给 curl

标签 shell curl scripting

我有一个要求,我试图编写一个在内部调用 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/

相关文章:

linux - 如何使用eval读取变量

c - 在c中读取shell的输入

shell - 用于将 IPv6 地址转换为数字(或字符串)的大型 CSV 文件的脚本

php - 为什么 CURL 在我设置了 3000 毫秒超时后会在 1000 毫秒内超时?

python - 在终端中使用 Curl 和 Django Rest Framework

windows - 通过脚本/命令行从 PE 文件中提取资源

python - 在 python 中执行 ls 输出魔术

c++ - 身份验证后从 curl 获取安全 cookie (c++)

linux - 根据文件名创建目录名

vim - 删除 Vim 中的行并将其重定向到另一个文件