Skip to content

Instantly share code, notes, and snippets.

@dizyart
Last active December 15, 2015 05:59
Show Gist options
  • Save dizyart/5213558 to your computer and use it in GitHub Desktop.
Save dizyart/5213558 to your computer and use it in GitHub Desktop.
Add Putty SSH key files (PPK) to Pageant from the command line. Type pageant_add_keys /? or /help for usage.
@echo off
goto script
REM inspired by code in http://jelledepot.blogspot.com/2009/04/putty-pageant-autoload-your-private.html
REM @author DizyArt (https://github.com/dizyart)
REM @copyright 2013 Dizyart
REM licensed under WTFPL (Do What the Fuck You Want to Public License) http://en.wikipedia.org/wiki/WTFPL
:script
REM this enables the FOR loop to use the expanded version of KEYS (!KEYS!)
SETlocal enabledelayedexpansion
REM change this to "C:\Program Files\Whatever your PuTTY directory is"
SET PuttyPath="%PROGRAMFILES%\Putty"
IF NOT EXIST %PuttyPath% (
REM Try the (x86) alternative path for programs
SET PuttyPath="%PROGRAMFILES% (x86)\Putty"
)
SET self=%~f0
SET /a count=0
SET recursive=
:parseArguments
IF NOT "%1"=="" (
IF "%1"=="/?" GOTO :help
IF "%1"=="/help" GOTO :help
IF "%1"=="/v" (
SET verbose=1
SHIFT
)
IF "%1"=="/d" (
SET dir=%2
SHIFT && SHIFT
)
IF "%1"=="/f" (
SET fileMatch=%2
SHIFT && SHIFT
)
IF "%1"=="/r" (
SET recursive=/s
SHIFT
)
GOTO :parseArguments
)
IF NOT exist %PuttyPath%\pageant.exe (
IF DEFINED verbose echo Pageant NOT found in %PuttyPath%. Set your PuttyPath correctly in %self%
GOTO error
)
IF NOT DEFINED dir (
SET dir=%CD%
)
IF DEFINED dir (
IF NOT EXIST %dir% (
IF DEFINED verbose echo The path "%dir%" matches no directories in "%CD%"
GOTO error
)
CD %dir%
)
IF NOT DEFINED fileMatch (
SET fileMatch=*.ppk
)
REM loop over all files in current directory
IF DEFINED verbose echo Adding PPK files matching '%fileMatch%' in %CD% to Pageant...
FOR /f %%f in ('dir %recursive%/b %fileMatch%') DO (
SET /a count=count+1
REM see what's being added to the list (this line is optional)
IF DEFINED verbose echo Found %%~ff
REM add the path of the file to the list
IF DEFINED KEYS SET KEYS=!KEYS! "%%~ff"
IF NOT DEFINED KEYS SET KEYS="%%~ff"
)
REM inspect the list (delete these lines IF you're NOT debugging)
REM IF DEFINED verbose ( echo List of keys added: & echo %KEYS%)
IF NOT DEFINED KEYS (
IF DEFINED verbose echo No PPK keys found in %CD% matching %fileMatch%
GOTO end
)
start /d%PuttyPath% pageant.exe %KEYS%
GOTO end
:error
IF NOT DEFINED verbose echo -1
IF DEFINED verbose echo Stopped
GOTO EOF
:end
IF NOT DEFINED verbose echo %count%
IF DEFINED verbose echo %count% keys added to Pageant
endLocal
goto EOF
:help
echo.##### Pageant add keys #####
echo.
echo.Adds private keys (PPK key files) from a directory to Pageant and outputs the count of added keys.
echo.If your PPK requires a password, pageant will open and ask you for it for each file.
echo.
echo.Usage:
echo.pageant_add [/d path] [/f filename] [/v] [/?^|/help]
echo.
echo.Arguments:
echo./d Directory where the key files are stored
echo. default is the current directory you're in
echo. can be an absolute or relative path
echo./r Searches for filename pattern in all subderectories (recursive)
echo./f filename Filename or file match pattern
echo. default is *.ppk
echo./v Enable verbose output
echo./? /help Outputs this help
echo.
echo.Examples:
echo.C:\Users\MyName^>pageant_add
echo. adds all filenames with .ppk extension from current (C:\Users\MyName) directory
echo.C:\Users\MyName^>pageant_add /f example.com-root.ppk
echo. adds "example.com-root.ppk" key to pageant
echo.C:\Users\MyName^>pageant_add /d SSH_keys
echo. adds all *.ppk files from "C:\Users\MyName\SSH_keys" folder
echo.C:\Users\MyName^>pageant_add /f example.com* /d ./SSH_keys
echo. would load:
echo. - C:\Users\MyName\SSH_keys\example.com-root.ppk
echo. - C:\Users\MyName\SSH_keys\example.com.system.ppk
echo. - C:\Users\MyName\SSH_keys\example.comapache.ppk
echo. but not:
echo. - C:\Users\MyName\SSH_keys\foobar.com.root.ppk
echo.C:\websites\^>pageant_add /f *power-user.ppk /r
echo. would load all *power-user.ppk filenames from subdirectories:
echo. - C:\websites\foo\foo.com-power-user.ppk
echo. - C:\websites\foo\bar.net-power-user.ppk
echo. - C:\websites\ding\dingdong.com-power-user.ppk
echo. && echo.
goto EOF
:EOF
@TheBoegl
Copy link

you should apply the the following fix: https://gist.github.com/TheBoegl/5655851/revisions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment