Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Script works when run with sudo, but not when run directly as root...?

by bmalnad (Initiate)
on May 07, 2014 at 18:46 UTC ( #1085362=perlquestion: print w/ replies, xml ) Need Help??
bmalnad has asked for the wisdom of the Perl Monks concerning the following question:

Hey Guys,

I just inherited this Perl script from a former coworker. It works fine if I run it from my account using: sudo perl decrypt.pl foo.pgp bar.txt

If I do a sudo su - root and type: perl decrypt.pl foo.pgp bar.txt

It fails with this error: Use of uninitialized value $plaintext[0] in print at decrypt.pl line 23, <$ciphertext_fh> line 2486.

#!/usr/bin/perl use strict; use warnings; use Crypt::GPG; use Data::Dumper; my $gpg = new Crypt::GPG; $gpg->gpgbin('/usr/bin/gpg'); $gpg->secretkey('xxxxxxxxxx'); $gpg->passphrase('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); my $inputfile = shift; my $outputfile = shift; open my $ciphertext_fh, '<', $inputfile or die "couldn't open $inputfi +le: $!"; open my $output_fh, '>', $outputfile or die "couldn't open $outputfile + for writing: $!"; my @ciphertext = <$ciphertext_fh>; my (@plaintext) = $gpg->decrypt(\@ciphertext) or die "didn't work: $!" +; print $output_fh @plaintext; close $ciphertext_fh; close $output_fh;
Thoughts??

Comment on Script works when run with sudo, but not when run directly as root...?
Download Code
Re: Script works when run with sudo, but not when run directly as root...?
by LanX (Canon) on May 07, 2014 at 18:51 UTC
    That's the only error message you get?

    No complaint about not locating a module?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Correct. I don't get any other error messages.
        My thought:

        Check the environment variables of your account and of root created by the login shell.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re: Script works when run with sudo, but not when run directly as root...?
by MidLifeXis (Prior) on May 07, 2014 at 19:50 UTC

    Which perl is being run? su - root will get you root's login shell along with root's default environment. sudo blah blah blah gets you your environment (notwithstanding any settings in your sudo configuration).

    Try the difference between the env and which perl commands in both environments.

    --MidLifeXis

      I can't find anything that makes any sense. dlamb@xxxxxxxxxx:~$ env && which perl
      TERM=xterm SHELL=/bin/bash SSH_CLIENT=172.20.202.137 38907 22 SSH_TTY=/dev/pts/0 USER=dlamb MAIL=/var/mail/dlamb PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr +/games PWD=/home/dlamb LANG=en_US.UTF-8 SHLVL=1 HOME=/home/dlamb LANGUAGE=en_US: LOGNAME=dlamb SSH_CONNECTION=172.20.202.137 38907 172.21.202.5 22 LESSOPEN=| /usr/bin/lesspipe %s LESSCLOSE=/usr/bin/lesspipe %s %s _=/usr/bin/env /usr/bin/perl
      root@xxxxxxxxxx:~# env && which perl
      SHELL=/bin/bash TERM=xterm USER=root MAIL=/var/mail/root PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin PWD=/root LANG=en_US.UTF-8 SHLVL=1 HOME=/root LANGUAGE=en_US: LOGNAME=root LESSOPEN=| /usr/bin/lesspipe %s LESSCLOSE=/usr/bin/lesspipe %s %s _=/usr/bin/env /usr/bin/perl

        Unsurprisingly, the two environments you have listed there differ in the value of $HOME. Crypt::GPG uses this to find the keyrings, so if the keys aren't present on root's keyring, it won't stand a chance.

        It is likely that hippo's message above gives the right explanation, but I can tell you that I met a similar problem with a (DBI-Oracle) script that worked perfectly under my user and did not under the crontab (i.e. root user) because of environment variables being defined differently. I wasted almost a full day figuring out what the problem was, quite a poor performance if you think that I originally wrote the whole script in 2 or 3 hours.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1085362]
Approved by igelkott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-12-28 16:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls