21

Is there a way to pass the username and password from a file instead of the command line via --user and --password?

Background: I want to run wget via cron and don't want the username/password show up in process view

13

Use a .wgetrc file (GNU manual) in which you can set username and passwords for either or both ftp and http.

To use the same credentials for both specify

user=casper
password=CasperPassword

or individually

ftp_user=casperftp
ftp_password=casperftppass
http_user=casperhttp
http_password=casperhttppass
  • 2
    The GNU wget manual also suggests using the -i option and feeding the username and password in from standard input. – Richard May 8 '11 at 15:08
  • Does .wgetrc provide the flexibility to work with more than one server? If not, .netrc is a better solution, see the other answer from tobias.pal – ryenus Feb 6 '17 at 13:26
  • I obviously missed wget using a .netrc file, but it's documented. – Mike Renfro Feb 6 '17 at 18:13
17

I'm surprised nobody mentioned the .netrc file. First create the file if it doesn't exists and set safe permissions:

touch ~/.netrc
chmod 600 ~/.netrc

Then you can add the hostname, username and password all on one line:

echo 'machine example.com login casper password CasperPassword' >> ~/.netrc

Then when you do wget https://example.com and the server responds with 401 Authorization Required, wget will retry with the username and password from the ~/.netrc file.

When using this from cron make sure you have the right HOME directory. Often cron sets HOME=/ (in that case you would have to create the file as /.netrc, but it's better to set a proper HOME at the beginning of your script, like export HOME=/root).

You can specify multiple hosts in ~/.netrc, one per line. More info in man netrc.

  • 2
    For the syntax of .netrc, see its manual, or the related curl doc. – ryenus Feb 6 '17 at 13:32
  • At first I'm too careless to see the man netrc in the OP, wondering WHY this works, then @ryenus your comment saves me, thanks~ Manual is always welcome :P Then I know it's a rc file used by ftp, that is, it may not work for http. I'll try it on http later. – Weekend Apr 29 at 13:13
0

In many regards curl can be a better choice. Wget became a bit stale over time.

curl's -n switch can be used for this task: http://curl.haxx.se/docs/manpage.html#-n

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.