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