Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

perl Expect; "set home"

by Stoomy (Novice)
on May 18, 2011 at 16:14 UTC ( #905506=perlquestion: print w/replies, xml ) Need Help??
Stoomy has asked for the wisdom of the Perl Monks concerning the following question:

Hello Brothers,
I'm trying to use to import a bunch of PGP keys into GPG.

I think I've got most every thing working except I'm not able to set the home dir. The result is the import tries to create /root/.gnupg...which fails due to perms (as it should).
If this were a straight expect script, it would look like:

set home "/home/username"

If I were doing this manually, it would look something like this:

su - <user> gpg --import ./.pgp/pubring.pkr (gpg asks if I'm sure (y/n), I say, "y") (gpg then asks for a passphrase and I enter it). then ~/.gnupg is created by gpg for <user>

Here is the relevant bit of code:

my $exp = new Expect; $exp->spawn("sudo -u $user gpg --import $home/.pgp/secring.skr"); $exp->send("set home $home\n"); #THS DOESN'T WORK...just an example of + something i tried $exp->expect(5, [qr'\(y/N\)\s*' , sub {my $exph = shift; print $exph " +y\r" ;exp_continue; }], [qr'passphrase:\s*' , sub {my $exph = shift; print $ +exph "$passphrase";exp_continue; }], [EOF => sub {die "Error: Could not login!\n"; }], [timeout => sub {die "Error: Could not login!\n"; }] +, '-re', '\$');

I'm not sure if doing sudo is the right approach or not....any suggestions much appreciated.

Replies are listed 'Best First'.
Re: perl Expect; "set home"
by Fletch (Chancellor) on May 18, 2011 at 17:08 UTC

    Not that it's a perl problem but the -H option to sudo is probably what you want; that or use the correct shell syntax to change the HOME environment variable rather than making something up.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Thanks flex/fletchvault,
      The -H option to sudo did what I needed.
Re: perl Expect; "set home"
by flexvault (Monsignor) on May 18, 2011 at 16:29 UTC

    On the gpg command you can add '--homedir "./.gnupg"', which must already exist.

    Good Luck

    "Well done is better than well said." - Benjamin Franklin

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://905506]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2018-06-21 11:00 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.