Search code examples
windowsbatch-filecmdputtypageant

Run a batch file when Pageant finishes loading SSH keys


I've written two batch files - one to launch Pageant and load my keys, and the other to ssh some files onto a remote server. Individually, the scripts work perfectly. I am trying to combine them into a single batch file, but I can't get it to work.

Here are the scripts - only a line each, really.

To launch Pageant and load keys:

start E:\PuTTY\pageant.exe E:\Keys\priv.ppk

exit

To use pscp:

pscp F:\website\foobar\src\* [email protected]:/var/www/html

The problem is that the first script launches a password prompt. If I finish this and then launch the next script, everything works perfectly. But I've been unable to combine these two into one script in a way where the second command runs after the prompt from the first one is complete. How would I create a batch file that did so?


Solution

  • You can hardly solve this in a batch file. Pageant is GUI application. It can hardly somehow signal back to a batch file that it finished loading keys.

    For this reason, Pageant has -c switch, which makes it run a specified program/batch-file after the keys are loaded:

    You can arrange for Pageant to start another program once it has initialised itself and loaded any keys specified on its command line. This program (perhaps a PuTTY, or a WinCVS making use of Plink, or whatever) will then be able to use the keys Pageant has loaded.

    You do this by specifying the -c option followed by the command, like this:

    C:\PuTTY\pageant.exe d:\main.ppk -c C:\PuTTY\putty.exe
    

    So this should to what you want:

    start E:\PuTTY\pageant.exe E:\Keys\priv.ppk -c C:\path\your_scp_batch.bat