Tuesday, December 31, 2024

Load all PuTTY PPKs into Pageant

 

I use PuTTY a lot, and with the proliferation of cloud, that means I have lots of SSH keys to manage.  One thing that has always annoyed me is how PuTTY Pageant doesn't have a nice UI to load all your keys automatically.  It does, however, have a CLI which allows you to script it, but there is no sample script and the syntax is a little cumbersome, so here is another gift to the Internet:

Code

@echo off
setlocal EnableDelayedExpansion
:: Set the path to the directory containing the .ppk files
set PPK_DIR=%USERPROFILE%\my_ssh_keys
:: Set the full path to the Pageant executable
set PAGEANT_PATH="C:\Program Files\PuTTY\pageant.exe"
:: Check if Pageant executable exists
if not exist %PAGEANT_PATH% (
    echo Pageant not found at %PAGEANT_PATH%.
    echo Please install PuTTY and update the PAGEANT_PATH variable in this script.
    exit /b 1
)
:: Check if there are .ppk files in the directory
if not exist "%PPK_DIR%\*.ppk" (
    echo No .ppk files found in %PPK_DIR%.
    exit /b 1
)
:: Gather all .ppk files into a single command
set "PPK_FILES="
for %%F in ("%PPK_DIR%\*.ppk") do (
:: echo Adding %%F ...
    SET "PPK_FILES=!PPK_FILES! %%F"
)
echo
echo Loading all PPKs into Pageant... %PPK_FILES%
:: Load all .ppk files into Pageant in one call
start "Pageant Title" /B %PAGEANT_PATH% %PPK_FILES%
echo All .ppk files loaded into Pageant.

Instructions

  1. Save the code above as "load-ssh-keys.bat" someplace, like c:\bin
  2. Edit the code where it sets the PPK_DIR to point to the folder where you keep all your SSH keys
  3. Open Windows File Explorer, go to c:\bin
  4. Verify it works
    1. Double click on the BAT file.  You will see a black window open for a second and disappear.  That is normal.
    2. On the bottom of the screen, click on the "^" icon.  You should see the Pageant icon (looks like a computer wearing a hat).
    3. Right click on it, and choose View Keys.
    4. If it worked correctly, you should see a list of all your keys.
  5. Make it run when you start your computer
    1. Right click on "load-ssh-keys.bat" and Create a Shortcut to it.  This creates a file called "load-ssh-keys.lnk"
    2. Right click on "load-ssh-keys.lnk" and choose Cut.
    3. In the Windows File Explorer, click on the "+" to open a second tab
    4. In the path field (the text field under the tab bar), enter this:
      %USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\
    5. Paste "load-ssh-keys.lnk" in the Startup folder
  6. Rejoice  ;)


No comments:

Post a Comment