简体   繁体   中英

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\* foo@178.128.10.35:/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?

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

You can take advantage of the way pageant behaves if another instance is already running. This way you can load a key into pageant and then keep using it in a single script.

START pageant.exe
TIMEOUT 1
pageant.exe E:\Keys\priv.ppk

:: Do stuff here.

TASKKILL /im pageant.exe

Here's how it works:

  • START pageant.exe Start pageant in the background.
  • TIMEOUT 1 Suspend the script for a second to ensure pageant is running before executing next line.
  • pageant.exe E:\\Keys\\priv.ppk Launch pageant again and tell it to load the key. Since another instance of pageant is already running, this instance terminates after loading the key, making the script proceed only then.
  • TASKKILL /im pageant.exe Kill pageant so the key can no longer be used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM