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

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??

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");
    # print "Shortcut to: $link->{Path} $link->{Arguments}\nDirectory:
+ $link->{WorkingDirectory}";
    Win32::Process::Create(my $process,
        qq("$link->{Path}" $link->{Arguments}),
      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
            or last;
        sleep 1;
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2022-01-17 02:31 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (50 votes). Check out past polls.