-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbashrc_ssh
More file actions
62 lines (53 loc) · 1.13 KB
/
bashrc_ssh
File metadata and controls
62 lines (53 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash
function ssh-start {
trap '' SIGINT
if [ -z "$SSH_AGENT_PID" ]; then
eval `ssh-agent -s | sed '/^echo/d'`
echo "SSH agent with pid $SSH_AGENT_PID"
else
echo "SSH agent with pid $SSH_AGENT_PID (already running)"
fi
KEY_FINGERPRINT=$(ssh-keygen -lf ~/.ssh/id_rsa | grep -oP "^\d+\s+[^\s]+")
if ! ssh-add -l | grep "$KEY_FINGERPRINT" > /dev/null; then
ssh-add
fi
trap SIGINT
trap ssh-stop EXIT
}
function ssh-stop {
if [ -n "$SSH_AGENT_PID" ]; then
ssh-add -D
ssh-add -l
eval `ssh-agent -k`
fi
sleep 1
}
function ssh-pass {
if [[ -z "$1" ]]; then
echo "Missing password." >&2
return 1
fi
export SSH_PASS=$1
export SSH_ASKPASS=`mktemp`
SSH_ASKPASS_TRIES=`mktemp`
echo 3 > "$SSH_ASKPASS_TRIES"
cat << EOF > "$SSH_ASKPASS"
#!/bin/bash
echo "\$1" >&2
TRIES=\$((\$(<"$SSH_ASKPASS_TRIES") - 1)) >&2
if [[ \$TRIES -lt 0 ]]; then
echo "[ssh-pass] Aborting..." >&2
exit 1
fi
echo \$TRIES > "$SSH_ASKPASS_TRIES"
echo "[ssh-pass] Sending password..." >&2
echo "\$SSH_PASS"
exit 0
EOF
chmod 0700 "$SSH_ASKPASS"
"${@:2}" 0> /dev/null
rm "$SSH_ASKPASS_TRIES"
rm "$SSH_ASKPASS"
unset SSH_ASKPASS
unset SSH_PASS
}