passing password to curl on command line

cod*_*der 6 shell scripting curl

I have a requirement where I am trying to write a shell script which is calling curl command internally. I have the password, username and url stored as variables in the script. However, since I want to avoid using user:password format of curl command in the script, I am just using curl --user command. My intention is to pass the password through stdin. So, I am trying something like this -

#!/bin/bash
user="abcuser"
pass="trialrun"
url="https://xyz.abc.com"
curl --user $user $url 2>&1 <<EOF
$pass
EOF
Run Code Online (Sandbox Code Playgroud)

But this is not working. I know there are variations to this question being asked, but I didn't quite get the exact answer, hence posting this question.

nba*_*ari 7

You can use:

curl -u abcuser:trialrun https://xyz.abc.comp
Run Code Online (Sandbox Code Playgroud)

In your script:

curl -u ${user}:${pass} ${url}
Run Code Online (Sandbox Code Playgroud)

To read from stdin:

curl  https://xyz.abc.com -K- <<< "-u user:password"
Run Code Online (Sandbox Code Playgroud)

When using -K, --config specify - to make curl read the file from stdin

That should work for HTTP Basic Auth, from the curl man:

-u, --user <user:password>

 Specify the user name and password to use for server authentication. 
Run Code Online (Sandbox Code Playgroud)

  • 我不想使用 user:password 格式,因为当我们使用 ps 命令时它可以以明文形式显示。因此从标准输入传递密码。 (3认同)

Ber*_*rtD 6

为了扩展@nbari的答案,如果您有一个可以在标准输出上生成密码的工具“get-password”,您可以安全地使用此调用:

user="abcuser"
url="https://xyz.abc.com"
get-password $user | sed -e "s/^/-u $user:/" | curl -K- $url
Run Code Online (Sandbox Code Playgroud)

密码将被写入管道。我们用来sed将密码修改为预期的格式。因此,该密码永远不会在ps历史记录中可见。

  • 没有 sed: `echo "-u $user:$(get-password $user)" | 卷曲-K- $url` (2认同)