Skip to content

Instantly share code, notes, and snippets.

@mackncheesiest
Last active December 20, 2019 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mackncheesiest/18ad4420acb8a5fb11864c436f5641b9 to your computer and use it in GitHub Desktop.
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
--- 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
}
@mackncheesiest
Copy link
Author

mackncheesiest commented Nov 18, 2019

Prerequisites:

  1. Ensure that Win10-based SSH client features are installed.
    a) See instructions for installing OpenSSH Client Features
  2. Download or build a binary for rupor-github's wsl-ssh-agent and place the extracted folder containing the binary in your .ssh folder (or update the Set-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.

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