Skip to content

Instantly share code, notes, and snippets.

@dregad
Created December 16, 2012 07:46
Show Gist options
  • Save dregad/4304091 to your computer and use it in GitHub Desktop.
Save dregad/4304091 to your computer and use it in GitHub Desktop.
Load Putty Pageant and Charade SSH agent in memory and initialize the environment variables for future use by OpenSSH (e.g. Cygwin)
@ECHO OFF
REM ===========================================================================
REM
REM ssh-agent-init.bat
REM
REM Load Putty Pageant and Charade SSH agent in memory and initialize the
REM environment variables for future use by OpenSSH (e.g. Cygwin)
REM
REM Prerequisites:
REM - charade [1]
REM - cygwin packages: gcc-c++, make, openssh, psmisc
REM - setx command [2]
REM
REM Credits
REM Thanks to Wesley Darlington for writing Charade and to Russell Davis who
REM wrote the script I used as a starting point for this [3]
REM
REM References:
REM [1] https://github.com/wesleyd/charade
REM [2] http://www.microsoft.com/download/en/confirmation.aspx?id=4200
REM [3] http://russelldavis.blogspot.com/2011/02/using-charade-to-proxy-cygwin-ssh-agent.html
REM
REM Installation:
REM 1. Install Charade as per the README file (steps 1 & 2 only)
REM 2. Install setx
REM 3. Make sure setx and Cygwin bin folder are on the PATH
REM 4. Edit PAGEANT and PKEYS variables below, as appropriate
REM 5. Execute this batch file at startup
REM
REM Authors:
REM Damien Regad (DRE)
REM
REM Revision history:
REM 2011-10-04 DRE Created
REM 2011-10-12 DRE Updated comments and prerequisites
REM 2012-11-20 DRE Remove leftover temporary socket directory at startup
REM
REM ===========================================================================
SETLOCAL
REM ---------------------------------------------------------------------------
REM Initialization
REM Path to Pageant executable
SET PAGEANT="C:\Program Files\PuTTY\pageant.exe"
REM Putty Private Key (PPK) files to preload (space-delimited)
SET PKEYS="c:\path\to\my.ppk"
REM Do not edit below this line
REM ---------------------------------------------------------------------------
SET TEMPSCRIPT=%TEMP%\ssh-agent-init-%RANDOM%.bat
ECHO Starting Pageant
START "Starting Pageant" %PAGEANT% %PKEYS%
IF NOT DEFINED SSH_AGENT_PID GOTO StartCharade
REM If the agent PID is defined and is a charade process, we are already set
ps -ef | grep "%SSH_AGENT_PID%" | grep charade >NUL
IF %ERRORLEVEL% EQU 0 (
ECHO Charade is already running
GOTO End
)
ECHO SSH_AGENT_PID (%SSH_AGENT_PID%) is not a running Charade process
:StartCharade
ECHO Starting Charade...
ECHO Kill any previous instances
killall charade
ECHO Deleting old ssh agent temp directories
FOR /F %%d IN ('cygpath -w /tmp') DO PUSHD %%d
FOR /F %%d IN ('DIR /B ssh-*') DO (
RMDIR /S /Q %%d
)
POPD
REM The -csh script works perfectly since we can just substitute setenv for setx
charade -c | sed 's/setenv/setx/; s/;$//' |tee %TEMPSCRIPT%
CALL %TEMPSCRIPT%
DEL %TEMPSCRIPT%
:End
ENDLOCAL
rem Uncomment this for debugging
rem PAUSE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment