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  ;)


Problem - "too many authentication failures"

Problem - "too many authentication failures"

I ran into a strange situation today.  I was trying to SSH into a partner's Ubuntu server in their lab over VPN from my laptop.  When I used PuTTY, I kept getting "too many authentication failures" immediately upon entering the username (no password given yet).  

The partner insisted they were able to access the server and verified the credentials.  But then I noticed they were using OpenSSH client.  I tried it, and it worked.  

Huh, this only affects PuTTY?  

Well, now I had access to the server's sshd logs, and I could see:

Dec 31 18:26:13 xxxxxxx sshd[1022]: userauth_pubkey: key type ssh-dss not in PubkeyAcceptedAlgorithms [preauth]
Dec 31 18:26:14 xxxxxxx sshd[1022]: error: maximum authentication attempts exceeded for admin from 192.168.101.254 port 57881 ssh2 [preauth]
Dec 31 18:26:14 xxxxxxx sshd[1022]: Disconnecting authenticating user admin 192.168.101.254 port 57881: Too many authentication failures [preauth]

Solution

Apparently, they've turned-off support for older DSS keys.  Makes sense, DSS has been obsolete for a while now.

So now I knew were to go look.  And low and behold, my PuTTY Pageant key agent had some old DSS keys loaded.  So I removed the old keys from Pageant, and now it works.  

That is odd though, you would expect sshd to fallback to password auth if none of the keys worked, but apparently it triggered the "too many authentication failures" logic.

I spent two days trying to figure this out.  Google searches were useless, so I thought I should add this pearl of wisdom to the vast sea of Internet knowledge, and hopefully save someone some time.