Learn how to a build a cloud-first strategyRegister Now


how to redirect stdout for htpasswd in script

Posted on 2004-11-08
Medium Priority
Last Modified: 2013-12-15


I've got an htpasswd authentication file that needs to be updated with the usernames and passwords from a database every day.

I've written a cron script that gets the values of the database and then calls htpasswd to update the authentication file.  The output from the cron job gets emailed to me every day - usually, just a lot of "Updating password for user **username**" lines.  I'd rather only get the cron email if there's a problem - a missing password or something.

My first thought was to just redirect stdout to /dev/null in the crontab, like this:

0 19 * * * /usr/bin/perl /home/user/bin/scripname.pl >/dev/null

but it doesn't seem to work.   I still get the output from the htpasswd command emailed to me.  Is it getting printed on stderr?  if so, I don't want to redirect stderr to /dev/null because then I'll miss any errors that occur.

Am I doing anything wrong?  Anyone had any experience like this?

Question by:gothamww
  • 2
  • 2
LVL 40

Accepted Solution

jlevie earned 500 total points
ID: 12526876
> Is it getting printed on stderr?


Since you are using perl to update the auth file why not directly manuipulate the file with the Perl script. It is pretty easy to generate the encrypted password within Perl. For DES encrypted passwords you can use:

sub descrypt()
  my($pw) = @_;
  $salts  = "0123456789";
  $salts .= "abcdefghijklmnopqrstuvwxyz";
  $nsalts = length($salts);
  $i = int(rand $nsalts);
  $ii = int(rand $nsalts);
  $salt = substr($salts, $i, 1).substr($salts, $ii, 1);
  $epw = crypt($pw, $salt);

which is used like:

$epw = descrypt($passwd);

$passwd holds the plain text password and $epw the DES encrypted result.

Author Comment

ID: 12528059

so, the authentication file that htpasswd generates is just an ordinary text file that can be edited by any means, as long as the password is "DES" encrypted and each record is in the format:

LVL 40

Expert Comment

ID: 12528318
Yep, assuming that you are using the flat file rather than a DB. If that's the case you can use any text editor to examine the auth file to see what it looks like. On a Linux system the encrypted passwords in the auth file can be encrypted with DES or MD5. The format of the encrypted password will auto-select the correct encryption method. See 'man 3 crypt' for details.

Author Comment

ID: 12528570
Thanks a lot jlevie

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month20 days, 17 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question