Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Win32: Launch and wait for Pageant before resuming

by bart (Canon)
on Feb 04, 2009 at 01:06 UTC ( #741157=snippet: print w/ replies, xml ) Need Help??

Description:

I use PuTTY tools from a Perl script, in particular pscp (PuTTY scp), and I use Pageant for automated authentication, so that I don't have to type in my password for every single file to transfer.

But my Pageant uses a password to its vault, too. So Pageant must already be running and ready before I attempt to scp files, or I still have to type in my password, every time, manually.

And that's where this snippet comes in. It tries to start up Pageant, waits till it comes back and/or until you typed in the password and pressed return, and only then resumes.

I use Win32::Process to launch Pageant, and one of 2 things may happen:

  1. Pageant wasn't running and it will pop up a login dialog box asking for the password. And then, after the dialog box closes, the process will continue running in the background, with an icon in the systray. If you Wait for that process to finish, you can wait for a very long time...

  2. Pageant was already running in the systray and the process immediately exits.

So, what does the snippet do?

  1. It reads the parameters to launch Pageant with from a Windows shortcut file (*.lnk file) — You may skip this part.

  2. It launches Pageant using Win32::Process, and it waits for 2 seconds for it to finish. So, in case (2) it'll immediately return, but in case (1) it'll give the program enough time to show its password dialog window.

  3. It uses Win32::GuiTest to see if the password dialog is still up, and polls, and waits until it closes.

    use Win32::Process;
    use Win32;
    use Win32::Shortcut;
    my $link = Win32::Shortcut->new;
    $link->Load("D:\\Program Files\\Putty\\start pageant.lnk");
    $link->Close();
    # print "Shortcut to: $link->{Path} $link->{Arguments}\nDirectory:
+ $link->{WorkingDirectory}";
    Win32::Process::Create(my $process,
        $link->{Path},
        qq("$link->{Path}" $link->{Arguments}),
        $link->{ShowCmd},
        NORMAL_PRIORITY_CLASS,
        $link->{WorkingDirectory})
      or die Win32::FormatMessage( Win32::GetLastError() );

    require Win32::GuiTest;  # while we're waiting...
    my $result = $process->Wait(2_000);  # returns immediately if page
+ant was running
                                         # times out if it just got st
+arted up
    while(1) {
        my @windows = Win32::GuiTest::FindWindowLike(0, qr/^Pageant:\s
++Enter\s+Passphrase/i)
            or last;
        sleep 1;
    }
Comment on Win32: Launch and wait for Pageant before resuming
Download Code

Back to Snippets Section

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2014-11-20 23:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (103 votes), past polls