git.lirion.de

Of git, get, and gud

aboutsummaryrefslogtreecommitdiffstats
path: root/bin/ssh-with-gpg
blob: 7ca0d67c36f4b2687e1771d9b8997974e876cfba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env bash

# For this to work, you also need to import the public key of any private key you want to use
# in the target server's pubring first.

if [ -z "$1" ];then
	printf "USAGE: %b TARGET [OPTIONS...]\\n" "$(basename "$0")" >&2
	exit 1
fi

TGT="$1"

REMUID="$(ssh "$TGT" "id -u")"
ssh "$TGT" 'killall gpg-agent 2>/dev/null;rm -f /run/user/$(id -u)/gnupg/S.*'
if [ -z "$REMUID" ];then
	printf "Cannot fetch user id from %b.\\n" "$TGT" >&2
	exit 2
fi

ssh -R "/run/user/${REMUID}/gnupg/S.gpg-agent":"/run/user/$(id -u)/gnupg/S.gpg-agent-extra" -o "StreamLocalBindUnlink=yes" "$TGT" "${@:2}"
span class="nv">ROEDGROED="$(pgrep -U "$(whoami)" ssh-agent|grep -v grep|awk '{print $1}')" if [ -n "$ROEDGROED" ];then if [ "$KILLIT" -eq 1 ];then for i in $(echo "$ROEDGROED");do kill "$i" >/dev/null 2>&1||break;done case "$?" in 0);; *) [ "$VERB" -eq 1 ]&&printf "\\033[666D[\\033[31mFAIL\\033[0m]\\033[u\\033[K\\n" exit 2 ;; esac else [ "$VERB" -eq 1 ]&&printf "\\033[666D[\\033[30;1mSKIP\\033[0m]\\033[u\\033[K (running, PID: %s)\\n" "$ROEDGROED" exit 0 fi fi ssh-agent >"$SSHENVFILE" 2>&1&&chmod 0600 "$SSHENVFILE" >/dev/null 2>&1 case "$?" in 0) sed -i 's/^echo[^\;]*\;//g' "$SSHENVFILE" >/dev/null 2>&1 [ "$VERB" -eq 1 ]&&printf "\\033[666D[\\033[32m OK \\033[0m]\\033[u\\033[K\\n" exit 0 ;; *) [ "$VERB" -eq 1 ]&&printf "\\033[666D[\\033[31mFAIL\\033[0m]\\033[u\\033[K\\n" exit 1 ;; esac