-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsshbar
executable file
·219 lines (205 loc) · 5.96 KB
/
sshbar
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/bin/bash
# Wrapper script to ssh to a robot. It starts a private instance of ssh-agent
# that makes the user's github key (and only that key) available during the
# session. It also environment variables for the user's git identity, read
# from the local machine's ~/.gituser file.
function valid_ip()
{
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
function usage() {
echo "Usage: sshbar [-d <path>] [-s | -k] [-p] [-h] [-v] <destination>"
echo ""
echo "Options:"
echo " -h, --help Display this help message"
echo " -d, --key_path Use <path> for keys *see notes*"
echo " -s, --secret Use barkey for authentication (core; port 2222)"
echo " -k, --classic Use barkey for authentication (classic; port 22)"
echo " -p, --payload Login to payload instead of truck (port 3022 or 3222)"
echo " -v, --verbose Verbose"
echo " -c, --nosshchecks Use -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
echo " --no-discover Don't ping the destination or try to find it on the network"
echo ""
echo " destination can be robot number (bar<destination>), IP address, or a host."
echo ""
echo " Examples:"
echo " sshbar 672"
echo " ssh bar@bar672"
echo " sshbar -p 672"
echo " ssh -i /.ssh/id_rsa-<hostname>-github -p3022 bar@bar672"
echo " sshbar -ps 672"
echo " ssh -i /data/.secrets/barkey -p3222 bar@bar672"
echo " sshbar -s 672"
echo " ssh -i /data/.secrets/barkey -p2222 bar@bar672"
echo " sshbar -k 672"
echo " ssh -i /data/.secrets/barkey -p22 bar@bar672"
echo " sshbar -kd /home/dnorris/.secrets 672"
echo " ssh -i /home/dnorris/.secrets/barkey -p22 bar@bar672"
echo " sshbar 10.10.39.209"
echo " ssh bar@10.10.39.209"
echo ""
echo "NOTE: 1. -s and -k options use the barkey for authentication."
echo " when not specified, authentication uses"
echo " id_rsa-<hostname>-github"
echo " 2. The defalt path is ~/.ssh when -s or -k are not specified."
echo " If -s or -k are specified, the default path is /data/.secrets"
echo " 3. The env var SECRET_PATH will override the default for both cases."
exit 1
}
prod=0
payload=0
secret=0
dev=0
user_path=0
verbose=0
ssh_checks=""
discover=1
short_opts="chpskvd:"
long_opts="help,payload,secret,classic,key_path:,nosshchecks,verbose,no-discover"
PARSED_ARGS=$(getopt -a --name "$(basename $0)" --options="${short_opts}" --longoptions="${long_opts}" -- "$@") || usage
eval set -- "$PARSED_ARGS"
while true; do
case "$1" in
-c|--nosshchecks) ssh_checks="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
;;
-d|--key_path)
user_path=1
secret_path="$2"
shift
;;
-p|--payload)
payload=1
;;
-s|--secret)
if [ $dev -eq 1 ] ; then
usage
fi
prod=1
secret=1
if [ $user_path -ne 1 ] ; then
if [ -z $SECRET_PATH ] ; then
secret_path='/data/.secrets'
else
secret_path=$SECRET_PATH
fi
fi
;;
-k|--classic)
if [ $prod -eq 1 ] ; then
usage
fi
dev=1
secret=1
if [ $user_path -ne 1 ] ; then
if [ -z $SECRET_PATH ] ; then
secret_path='/data/.secrets'
else
secret_path=$SECRET_PATH
fi
fi
;;
-v|--verbose)
verbose=1
;;
--no-discover)
discover=0
;;
-h|--help)
usage
return
;;
--)
shift
break
;;
esac
shift
done
if [ -z "$1" ] ; then
usage
fi
host="${@: -1}"
if valid_ip ${@: -1}; then
host="${@: -1}"
elif [[ discover -eq 0 ]]; then
host="${@: -1}"
elif ping -c 1 -W 1 bar${host} > /dev/null 2>&1 ; then
host=bar${host}
elif ! ping -c 1 -W 1 ${host} > /dev/null 2>&1 ; then
if [[ ! ${host} =~ ^[0-9]+$ && ! ${host} =~ ^bar[0-9]+$ ]] ; then
echo "Bad hostname ${host}"
exit 1
fi
host=$(host -l badger-technologies.com ns.badger-technologies.com | grep ${host#bar} | sed 's/.*address //')
if [[ "${host}" == "" ]]; then
echo "Unable to find host $1"
exit 1
fi
fi
if [ $secret -eq 1 ] || [ $dev -eq 1 ] ; then
id_file=${secret_path}/barkey
else
secret_path=$HOME/.ssh
if [[ -e ${secret_path}/id_ed25519-$(hostname)-github ]] ; then
id_file=${secret_path}/id_ed25519-$(hostname)-github
elif [[ -e ${secret_path}/id_rsa-$(hostname)-github ]] ; then
id_file=${secret_path}/id_rsa-$(hostname)-github
else
echo 'Identity File not found in path '${secret_path}
exit 1
fi
fi
zshell=0
xfor=0
if [ $prod -eq 1 ] ; then
if [ $payload -eq 1 ] ; then
port='3222'
else
port='2222'
fi
elif [ $payload -eq 1 ] ; then
port='3022'
zshell=1
xfor=1
else
port='22'
zshell=1
xfor=1
fi
if [ $zshell -eq 1 ] ; then
shell='zsh'
else
shell='bash'
fi
if [ $xfor -eq 1 ] ; then
options='-X -t -A'
else
options='-t -A'
fi
uds_addr=$(mktemp --dry-run $XDG_RUNTIME_DIR/ssh-agent-XXXXXX)
git_email=$(sed -n -e 's/^\s\+email\s\?=\s\?//p' < ~/.gituser)
git_name=$(sed -n -e 's/^\s\+name\s\?=\s\?//p' < ~/.gituser | sed -e 's/\s/\\ /')
if [ $verbose -eq 1 ] ; then
set -x
fi
exec ssh-agent -a $uds_addr ssh ${options} -p${port} -l bar \
-o IdentityFile=$id_file -o AddKeysToAgent=yes ${ssh_checks} -l bar ${host} \
GIT_AUTHOR_EMAIL="${git_email}" \
GIT_COMMITTER_EMAIL="${git_email}" \
GIT_AUTHOR_NAME="${git_name}" \
GIT_COMMITTER_NAME="${git_name}" \
EDITOR=emacs \
TERM=screen-256color \
${shell} ;
set +x