capser
capser

Reputation: 2635

passing a shells output to a perl script log

what this script does is pull down a list of usernames and passowrds and hashes it and then adds it to a list. When there are non asci characters, the quotemeta escapes them, which allows to htpasswd command to consume the vaule. When I run it it prints the results of /usr/local/apache2/bin/htpasswd to the terminal. I want to pass all the results to a file

#!/usr/bin/perl
use strict; 
use warnings;
use DBI;
use File::Copy;

unshift @INC,"/production/lib";
require "config.pl";
$configFile = "/production/cfg/syncUsers.cfg";
readConfig($configFile);

#doBackup($prefs{passwdFile});
generatePasswdFile("tmpusers");
getUsers($prefs{dbUser},$prefs{dbPass},$prefs{dbSid});
#copyPasswdFile($prefs{passwdFile});

# getUsers - grab user from the db and set them up
sub getUsers { 
    $dbUser = shift;
    $dbPass = shift;
    $dbSid  = shift;
    $dbh =  DBI->connect("dbi:Oracle:$dbSid","$dbUser","$dbPass") or die( "Couldn't connect: $!" );
    $sql = "SELECT user_name,passwd FROM login_tbl";
    $sth = $dbh->prepare($sql);
    $sth->execute();
    while ( ( $user_name,$passwd ) = $sth->fetchrow_array ) {
            $user_name = quotemeta($user_name);
            $passwd = quotemeta($passwd);
            #print "$user_name,$passwd\n";
            updatePasswdFile($user_name,$passwd);   
    }
    $dbh->disconnect();
}

# generatePasswdFile - generate a dummy file to start with
sub generatePasswdFile {
    $file = shift;
    $cmd = "$prefs{htpasswd} -bc /tmp/$file dummyuser dummyuser";
    system($cmd); 
}

# updatePasswdFile - update the passwdfile
sub updatePasswdFile {
    $file = "/tmp/tmpusers"; 
    $user = shift;
    $pass = shift;
    print "Adding user: $user\n";
    $cmd = "$prefs{htpasswd} -b $file $user $pass";
    system($cmd);
}

this is what it does its I print out to screen.

Adding user: dev_ops
Adding password for user dev_ops
Adding user: dev_shed
Adding password for user dev_shed
Adding user: robogrl
Adding password for user robogrl
Adding user: a_foo
Adding password for user a_foo
Adding user: atank
Adding password for user atank
Adding user: acar
Adding password for user acar
Adding user: acarlson
Adding password for user acarlson
Adding user: dnick
Adding password for user dnick

When I pass it to a file so I can check for errors only one half goes to a file and the other half is output to the screen.

bash-3.00$ ./caspser.pl > /tmp/checkFORerrors2013Nov19   

this output still goes to the screen -

Adding password for user dev_ops
Adding password for user dev_shed
Adding password for user robogrl
Adding password for user a_foo
Adding password for user atank
Adding password for user acar
Adding password for user acarlson
Adding password for user dnick

and this goes to the file

Adding user: dev_ops
Adding user: dev_shed
Adding user: robogrl
Adding user: a_foo
Adding user: atank
Adding user: acar
Adding user: acarlson
Adding user: dnick

how do i get everything to go to a file? better yet - is there a way to just print errors?

Upvotes: 0

Views: 131

Answers (1)

Matthew Franglen
Matthew Franglen

Reputation: 4532

The htpasswd command you use prints to STDERR (standard error). The redirect you have performed redirects STDOUT (standard out) into the file. To redirect both, you need to update your command:

bash-3.00$ ./caspser.pl > /tmp/checkFORerrors2013Nov19 2>&1

Basically, errors are printed to a different stream. Both streams are showing in the terminal without redirections, and the difference only becomes obvious when you start changing them.

Upvotes: 3

Related Questions