Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing password to curl on command line

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

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.

like image 247
coder Avatar asked Oct 03 '17 08:10

coder


People also ask

How do you pass credentials in curl command?

For example, if a website has protected content curl allows you to pass authentication credentials. To do so use the following syntax: curl --user "USERNAME:PASSWORD" https://www.domain.com . “USERNAME” must be replaced with your actual username in quotes.

How do I curl using CMD?

cURL makes HTTP requests just like a web browser. To request a web page from the command line, type curl followed by the site's URL: The web server's response is displayed directly in your command-line interface. If you requested an HTML page, you get the page source -- which is what a browser normally sees.

How do I pass login credentials through URL?

We can do HTTP basic authentication URL with @ in password. We have to pass the credentials appended with the URL. The username and password must be added with the format − https://username:password@URL. Let us make an attempt to handle the below browser authentication.

What is user in curl request?

--user (or -u ) in curl provides a basic auth to your request. In Postman you can achieve the same result with a choice in Authorization tab.


2 Answers

You can use:

curl -u abcuser:trialrun https://xyz.abc.comp

In your script:

curl -u ${user}:${pass} ${url}

To read from stdin:

curl  https://xyz.abc.com -K- <<< "-u user:password"

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. 
like image 80
nbari Avatar answered Oct 13 '22 10:10

nbari


To expand on @nbari's answer, if you have a tool "get-password" that can produce a password on stdout, you can safely use this invocation:

user="abcuser"
url="https://xyz.abc.com"
get-password $user | sed -e "s/^/-u $user:/" | curl -K- $url

The password will be written to a pipe. We use sed to massage the password into the expected format. The password will therefore never be visible in ps or in the history.

like image 36
BertD Avatar answered Oct 13 '22 08:10

BertD