Last active
October 8, 2015 18:04
-
-
Save silverkorn/1854bda9ac38db1bd7d9 to your computer and use it in GitHub Desktop.
Starting or recycling SSH agent from Cygwin for Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:: Start or recycle SSH agent | |
@echo off | |
:: Set the SSH_KEY if passed by argument, otherwise, use default file | |
IF NOT "%1" == "" ( | |
SET SSH_KEY=%1 | |
) ELSE IF "%SSH_KEY%" == "" ( | |
SET SSH_KEY=%USERPROFILE%\.ssh\id_rsa | |
) | |
:: Call once before to avoid hanging in loop parsing | |
cmd /C %~dp0launch-ssh-agent > NUL | |
FOR /f "tokens=1,2 delims=;" %%a in ('%~dp0launch-ssh-agent') do ( | |
SET SSH_AUTH_SOCK=%%a | |
SET SSH_AGENT_PID=%%b | |
) | |
:: Hide key addition (success pass in stderr for some reason...) | |
%~dp0ssh-add "%SSH_KEY%" > NUL 2>&1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:: Fluid SSH agent launcher for Windows/Cygwin, by Danny Boisvert | |
@echo off | |
setlocal EnableDelayedExpansion | |
SET SSH_AUTH_SOCK=%TEMP%\ssh-agent.proc | |
SET SSH_AGENT_PID_FILE=%TEMP%\ssh-agent.pid | |
SET SSH_AGENT_PID= | |
:: Private use | |
SET _SSH_AGENT_START_NEW=1 | |
:: If PID file exists, verify if the process really exists | |
IF EXIST "%SSH_AGENT_PID_FILE%" ( | |
SET /p SSH_AGENT_PID=<"%SSH_AGENT_PID_FILE%" | |
TASKLIST /NH /FI "PID eq !SSH_AGENT_PID!" /FI "IMAGENAME eq ssh-agent.exe" 2> NUL | FIND /I "ssh-agent.exe" > NUL | |
IF "!ERRORLEVEL!" == "0" ( | |
SET _SSH_AGENT_START_NEW=0 | |
) | |
) | |
:: Create a new SSH agent with it's necessary files | |
IF "!_SSH_AGENT_START_NEW!" == "1" ( | |
SET SSH_AGENT_PID= | |
IF EXIST "%SSH_AUTH_SOCK%" ( | |
ATTRIB -S "%SSH_AUTH_SOCK%" 2> NUL & DEL /F "%SSH_AUTH_SOCK%" | |
) | |
FOR /f "tokens=4 delims=; " %%a in ('%~dp0ssh-agent -a "%SSH_AUTH_SOCK%"') do ( | |
SET SSH_AGENT_PID=!SSH_AGENT_PID!%%a | |
) | |
ECHO !SSH_AGENT_PID!>"%SSH_AGENT_PID_FILE%" | |
) | |
:: Return SSH_AUTH_SOCK & SSH_AGENT_PID for parsing | |
ECHO !SSH_AUTH_SOCK!;!SSH_AGENT_PID! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment