Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make Cmder work with ssh-agent
@ECHO OFF
REM Set default sock file
SET SSH_AUTH_SOCK=/tmp/ssh-agent.sock
REM Check socket is available
IF NOT EXIST "%TMP%\ssh-agent.sock" GOTO:RUNAGENT
REM Check if an ssh-agent is running
FOR /f "tokens=*" %%I IN ('ps ^| grep ssh-agent ^| sed "s/^ *\([0-9]\+\) .*/\1/"') DO SET VAR=%%I
IF "%VAR%" == "" GOTO:RUNAGENT
REM Check if socket file is valid
ssh-add -l 1> NUL 2>&1
IF ERRORLEVEL 1 GOTO:RUNAGENT
GOTO:ADDKEYS
:RUNAGENT
REM Remove old socket file
rm -f /tmp/ssh-agent.sock
REM Run ssh-agent and save (last) PID in VAR
SET VAR=
FOR /f "tokens=*" %%J IN ('ssh-agent -a /tmp/ssh-agent.sock') DO FOR /f "tokens=*" %%K IN ('echo %%J ^| grep "SSH_AGENT_PID" ^| sed "s/^SSH_AGENT_PID=\([0-9]\+\); .*/\1/"') DO SET VAR=%%K
:ADDKEYS
SET SSH_AUTH_PID=%VAR%
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 "%1"') DO SET VAR=%%J
IF "%VAR%" == "" SET KEYS='%HOME%\.ssh\%1' %KEYS%
GOTO:EOF
:END
@ECHO ON
@ciscoheat

This comment has been minimized.

Copy link

ciscoheat commented May 4, 2015

Thanks for doing all the %%work%% 😄

@hsarret

This comment has been minimized.

Copy link

hsarret commented Jun 14, 2016

Thanks :)

@L30Bola

This comment has been minimized.

Copy link

L30Bola commented Jul 8, 2016

How do I make this work?

@e7d

This comment has been minimized.

Copy link

e7d commented Dec 8, 2016

@Shoozza You sir sure deserve an award.

@eromoe

This comment has been minimized.

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

This comment has been minimized.

Copy link

mlotfi2005 commented Mar 27, 2017

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

Copy link

Ciwan1859 commented May 30, 2018

@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?

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