-
Notifications
You must be signed in to change notification settings - Fork 1
/
pair-ssh-instance
executable file
·86 lines (71 loc) · 1.92 KB
/
pair-ssh-instance
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
DEBUG="${DEBUG:-false}"
if [ ! "$DEBUG" = "false" ]; then
set -x
fi
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-s|--ssh-opts)
SSH_OPTS="$2"
shift # past argument
shift # past value
;;
-n|--no-keys)
SSH_OPTS="-t -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
shift
;;
-c|--command)
COMMAND="$2"
shift
shift
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
PROG_NAME="$(basename $0)"
PAIR_ADDR="${1:-$PAIR_ADDR}"
PAIR_BASE_ADDR="${PAIR_BASE_ADDR:-pair.sharing.io}"
SSH_OPTS="${SSH_OPTS:--tA -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null}"
if [ $# -eq 0 ]; then
cat << EOF
Usage: ${PROG_NAME} [OPTS...] INSTANCE
-s|--ssh-opts specify SSH parameters
-n|--no-keys don't forward SSH keys
-c|--command change the command on connection
Examples:
- ${PROG_NAME} coolbox
- ${PROG_NAME} bobymcbobs
- ${PROG_NAME} 133.7.0.0
- ${PROG_NAME} -c bash heyste.pair.sharing.io
- ${PROG_NAME} -n bernokl
Notes:
- to not forward SSH keys set the 'SSH_OPTS' var to something like '-t'.
example: SSH_OPTS=-t ${PROG_NAME} heyste
- default SSH_OPTS are '${SSH_OPTS}'
- default Pair domain is '${PAIR_BASE_ADDR}'
EOF
exit 0
fi
function errorMsg {
echo "error: $*" > /dev/stderr
exit 1
}
function envEmpty {
if [ -z "$2" ]; then
errorMsg "'$1' is empty and is required"
fi
}
envEmpty PAIR_ADDR "${PAIR_ADDR}"
echo "${PAIR_ADDR}" | grep -q '\.'
PAIR_ADDR_IS_FQDN_OR_IPADDR=$?
if [ "${PAIR_ADDR_IS_FQDN_OR_IPADDR}" -eq 1 ]; then
PAIR_ADDR="${PAIR_ADDR}.${PAIR_BASE_ADDR}"
fi
COMMAND="${COMMAND:-attach}"
ssh ${SSH_OPTS} ii@${PAIR_ADDR} -- $COMMAND