Last active
December 20, 2019 17:03
-
-
Save mackncheesiest/18ad4420acb8a5fb11864c436f5641b9 to your computer and use it in GitHub Desktop.
Bash+Powershell scripts to start up Windows-based SSH agent in WSL as necessary
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
--- Bash Profile Check and Invocation --- | |
export USER="" | |
export SSH_AUTH_SOCK=/mnt/c/users/${USER}/.ssh/ssh-agent.sock | |
check_sshagent() { | |
if ! ssh-add -l 2>&1 1>/dev/null; then | |
touch /mnt/c/users/${USER}/.ssh/.customlock | |
powershell.exe -command "Start-Process -Verb RunAs powershell.exe -Args '-executionpolicy bypass -file', \"C:\Users\${USER}\.ssh\start-ssh.ps1\"" | |
printf "Waiting for password entry in powershell" | |
while [ -f /mnt/c/users/${USER}/.ssh/.customlock ]; do | |
printf "." | |
sleep 1s | |
done | |
printf "\nLockfile removed! Proceeding...\n" | |
fi | |
} | |
alias ssh="check_sshagent && ssh" | |
--- Powershell Script --- | |
$serviceName = "ssh-agent" | |
$agentStatus = Get-Service -Name $serviceName | |
$user = "" | |
while ($agentStatus.Status -ne "Running") { | |
Start-Service $serviceName | |
write-host $agentStatus.Status | |
write-host "Starting service" | |
Start-Sleep -seconds 5 | |
$agentStatus.Refresh() | |
if ($agentStatus.Status -eq "Running") { | |
write-host "Service is now Running" | |
} | |
} | |
Set-Location C:\Users\$user\.ssh | |
ssh-add -D | |
ssh-add .\id_ed25519 | |
.\wsl-ssh-agent\wsl-ssh-agent-gui.exe -socket C:\Users\$user\.ssh\ssh-agent.sock | |
# If WSL placed a lockfile here prior to calling this script, delete it so it knows to proceed | |
if (Test-Path .\.customlock) { | |
Remove-Item .\.customlock | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisites:
a) See instructions for installing OpenSSH Client Features
.ssh
folder (or update theSet-Location
call and corresponding-socket
argument)Then, create the powershell script from the gist + add the check to your bash_profile/bashrc and whenever you start an SSH session, a powershell window should pop up to ask for SSH key password only as it's needed.