Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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 Expect.pm 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.

Comment on perl Expect; "set home"
Select or Download Code
Re: perl Expect; "set home"
by flexvault (Parson) 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

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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-12-20 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (99 votes), past polls