Skip to content

Instantly share code, notes, and snippets.

@guitarmasaki
Forked from yaegashi/win-ssh-agent.sh
Created June 20, 2016 07:45
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 guitarmasaki/f79acdc34d2840f1cb80bc2721261c17 to your computer and use it in GitHub Desktop.
Save guitarmasaki/f79acdc34d2840f1cb80bc2721261c17 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Start ssh-agent and register environment variables permanently in HKCU.
# Copyright (C) 2013 YAEGASHI Takeshi <t@keshi.org>
# Use a fixed path for the ssh-agent socket endpoint.
# SSH_FIXED_SOCK="/tmp/ssh-agent-$(id -u).sock"
eval $(ssh-agent -k)
if test -n "$SSH_FIXED_SOCK"; then
rm -f "$SSH_FIXED_SOCK"
eval $(ssh-agent -a "$SSH_FIXED_SOCK")
else
eval $(ssh-agent)
fi
PID=$SSH_AGENT_PID
case "$(uname -s)" in
CYGWIN*)
SOCK=$(cygpath --windows "$SSH_AUTH_SOCK") ;;
*)
SOCK=$(cmd //c echo "$SSH_AUTH_SOCK") ;;
esac
echo "Agent endpoint $SOCK"
echo
cat <<EOF | powershell -command -
[Environment]::SetEnvironmentVariable("SSH_AGENT_PID", "$PID", "User")
[Environment]::SetEnvironmentVariable("SSH_AUTH_SOCK", "$SOCK", "User")
# P/Invoke SendMessageTimeout(): Broadcast WM_SETTINGCHANGE (0x1a)
# to notify other processes of the environment variable changes.
\$WinAPI = add-type -Name WinAPI -PassThru -MemberDefinition @"
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageTimeout(
IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam,
uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);
"@
\$result = [UIntPtr]::Zero
\$ret = \$WinAPI::SendMessageTimeout([IntPtr]0xffff, 0x1a,
[UIntPtr]::Zero, "Environment", 2, 5000, [ref]\$result)
EOF
while ! ssh-add; do
echo "Try again... (or ^C to abort)"
done
echo
echo "Agent added fingerprint"
ssh-add -l
echo
echo -n "Press ENTER to exit... "
read
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment