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.
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)
为了扩展@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
历史记录中可见。