Question

I have a few PHP scripts across servers that have DIGEST auth setup to offer a basic protection. These scripts are executed by the cron on each system like this:

/usr/bin/curl -s --digest -u "user:password" "http://path/to/script

In my present setup, if I happen to change the username/password combination, I'll have to manually edit ALL the crons to update the password.

Is there a way I can store the auth details (the password's a hash anyway) in a text file that I can pass to cURL? Something like..

/usr/bin/curl -<some option> /var/auth.txt

Was it helpful?

Solution

Using -d @filename is the method to include a file in a POST call, however I suspect it won't work for -u.

Best option is to either use a .curlrc, a standalone curl config file, or pass config file via stdin.

EDIT: Short version: add a -K <config file path> parameter to the cURL request and move the -u "user:password" to the first line of this file.


From the man pages for curl http://curl.haxx.se/docs/manpage.html

-K, --config <config file>

Specify which config file to read curl arguments from. The config file is a text file in which command line arguments can be written which then will be used as if they were written on the actual command line.

Options and their parameters must be specified on the same config file line, separated by whitespace, colon, or the equals sign. Long option names can optionally be given in the config file without the initial double dashes and if so, the colon or equals characters can be used as separators. If the option is specified with one or two dashes, there can be no colon or equals character between the option and its parameter.

If the parameter is to contain whitespace, the parameter must be enclosed within quotes. Within double quotes, the following escape sequences are available: \, \", \t, \n, \r and \v. A backslash preceding any other letter is ignored. If the first column of a config line is a '#' character, the rest of the line will be treated as a comment. Only write one option per physical line in the config file.

Specify the filename to -K, --config as '-' to make curl read the file from stdin.

Note that to be able to specify a URL in the config file, you need to specify it using the --url option, and not by simply writing the URL on its own line. So, it could look similar to this:

url = "http://curl.haxx.se/docs/"

When curl is invoked, it always (unless -q is used) checks for a default config file and uses it if found. The default config file is checked for in the following places in this order:

1) curl tries to find the "home dir": It first checks for the CURL_HOME and then the HOME environment variables. Failing that, it uses getpwuid() on UNIX-like systems (which returns the home dir given the current user in your system). On Windows, it then checks for the APPDATA variable, or as a last resort the '%USERPROFILE%\Application Data'.

2) On windows, if there is no _curlrc file in the home dir, it checks for one in the same dir the curl executable is placed. On UNIX-like systems, it will simply try to load .curlrc from the determined home dir.

# --- Example file ---
#this  is a comment
 url = "curl.haxx.se"
 output = "curlhere.html"
 user-agent = "superagent/1.0"

# and fetch another URL too
 url = "curl.haxx.se/docs/manpage.html"
 -O
 referer = "http://nowhereatall.com/"
#- -- End of example file ---

This option can be used multiple times to load multiple config files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top