Skip to content

Instantly share code, notes, and snippets.

@Shoozza
Last active April 20, 2022 02:45
Show Gist options
  • Save Shoozza/8fe8b0c85fa89cd3b1c4 to your computer and use it in GitHub Desktop.
Save Shoozza/8fe8b0c85fa89cd3b1c4 to your computer and use it in GitHub Desktop.
Make Cmder work with ssh-agent
@ECHO OFF
REM ssh-agent already running?
SET VAR=
for /f "tokens=*" %%i in ('ps x ^| grep ssh-agent') do set VAR=%%i
IF NOT "%VAR%" == "" GOTO:GETPID
REM start ssh-agent
ssh-agent -a /tmp/ssh-agent.sock | grep -v echo | sed -e "s/^/@set /" | sed -e "s/;.*$//" - > call.cmd
CALL call.cmd
DEL call.cmd
GOTO:CHECKS
:GETPID
REM Get SSH_AUTH_SOCK and SSH_AGENT_PID from running ssh-agent
SET VAR=
for /f "tokens=*" %%i in ('ps x ^| grep ssh-agent ^| sed "s/^ *\([0-9]\+\) .*/\1/"') do set VAR=%%i
SET SSH_AUTH_SOCK=/tmp/ssh-agent.sock
SET SSH_AGENT_PID=%VAR%
:CHECKS
REM Check if ssh keys are known
SET KEYS=
FOR /f "tokens=*" %%I IN ('DIR /B "%HOME%\.ssh\*_rsa"') DO CALL:CHECKKEY %%I
REM Add missing ssh keys at once
IF NOT "%KEYS%" == "" ssh-add %KEYS%
GOTO:END
REM Functions
REM Check if ssh key has to be added
:CHECKKEY
SET VAR=
for /f "tokens=*" %%J in ('ssh-add -l ^| grep soldat_rsa') do set VAR=%%J
IF "%VAR%" == "" SET KEYS=%KEYS% "%HOME%\.ssh\%1"
GOTO:EOF
:END
@ECHO ON
@ciscoheat
Copy link

Thanks for doing all the %%work%% 😄

@hsarret
Copy link

hsarret commented Jun 14, 2016

Thanks :)

@L30Bola
Copy link

L30Bola commented Jul 8, 2016

How do I make this work?

@e7d
Copy link

e7d commented Dec 8, 2016

@Shoozza You sir sure deserve an award.

@eromoe
Copy link

eromoe commented Jan 9, 2017

@Shoozza

First time worked. After I restart cmder, got

bind: Address already in use
unix_listener: cannot bind to path: /tmp/ssh-agent.sock
Error connecting to agent: Connection refused
Error connecting to agent: Connection refused

exit /b

PS: I amd using {cmd} and {minnty}/git-bash at same time, git-bash also start ssh-agent.

@mlotfi2005
Copy link

Hi, Please bear with me, I am a beginner, I have Cmder in my wincdows 7, I use ssh IPaddress to connect to our servers, I have always to enter the password,
please is the above script for the same reason to do ssh IP and you don't need to enter the password ?

If so can you please show me the steps ?

Thanks, your help is appreciated.

@Ciwan1859
Copy link

Ciwan1859 commented Apr 16, 2017

@Shoozza can you please shed some light on what we are supposed to put that script? I have the latest stable version of Cmder installed, but each time I do a git push or git pull I'm asked for my SSH key. I believe your script fixes that, but I'm not sure what to do with your script. Any n00b friendly instructions would be greatly appreciated.

@Tsarpf
Copy link

Tsarpf commented Apr 30, 2017

@Ciwan1859 with the newest version of Cmder, in:
[your cmder folder]\config\user-profile.cmd
you can just uncomment the line:
call "%GIT_INSTALL_ROOT%/cmd/start-ssh-agent.cmd"
by removing the :: in front of it and it should start working when you restart cmder.

@Ciwan1859
Copy link

@Tsarpf thank you, I uncommented that, and now when I open Cmder, it says ssh is open.

Where do I need to upload my .ppk keys for the SSH to look them up?

@kangpaidjo
Copy link

@Ciwan1859 with the newest version of Cmder, in:
[your cmder folder]\config\user-profile.cmd
you can just uncomment the line:
call "%GIT_INSTALL_ROOT%/cmd/start-ssh-agent.cmd"
by removing the :: in front of it and it should start working when you restart cmder.

it work
thanks :)

@Shoozza
Copy link
Author

Shoozza commented Sep 22, 2021

Hey everyone since this script is not really necessary for cmder,
I decided to fix and update this script to work with MSYS2 (and possibly cygwin).
Cmder provides a start-ssh-agent script from git-for-windows by default.

Updated and extended the script:
Add license
Add kill command
Add verbose output
Add help/usage info
Add version/license info
Add dependency check support
Add support for adding all kinds of ssh keys
Add comments
Add support for checking already set SSH_AUTH_SOCK before using DEFAULT_AUTH_SOCK
Modify use ECHO, for faster output
Fix don't leak/overwrite environment variables except SSH_AUTH_SOCK and SSH_AGENT_PID if successfull
Fix check for the sock file in the correct folder (/tmp != %TEMP%)
Fix valid sock file check needs to check with fingerprint instead of non working path

Have fun ;)

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