This repository has been archived by the owner on Mar 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathssh_otc.sh
executable file
·183 lines (161 loc) · 5.5 KB
/
ssh_otc.sh
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
#!/bin/bash
# Little helper to ssh into VMs
#
# (c) Kurt Garloff <t-systems@garloff.de>, 1/2018, CC-BY-SA-4.0
#set -x
usage()
{
echo "Usage: ssh_otc.sh [--wait] [sshopts] [USERNAME@]VM [CMD]"
echo "VM may be specified by IP, NAME, UUID"
exit 2
}
if test -z "$1"; then usage; fi
if test "$1" == "--wait"; then WAIT=1; shift; fi
#OTC_TENANT=${OTC_TENANT:-210}
#SSHKEY=~/SSHkey-$OTC_TENANT.pem
NORM="\e[0;0m"
YELLOW="\e[0;33m"
RED="\e[0;31m"
# FIXME: Should allow for options here ...
SUSER=linux
declare -a ARGS=()
while [[ $1 = -* ]]; do
ARGS[${#ARGS[*]}]="$1"
# FIXME: Need to handle all opts with args here ...
if test "$1" == "-p"; then ARGS[${#ARGS[*]}]="$2"; PORT="$2"; shift; fi
if test "$1" == "-b" -o "$1" == "-c" -o "$1" == "-D" \
-o "$1" == "-E" -o "$1" == "-e" -o "$1" == "-F" -o "$1" == "-F" \
-o "$1" == "-L" -o "$1" == "-m" -o "$1" == "-m" -o "$1" == "-O" \
-o "$1" == "-o" -o "$1" == "-Q" -o "$1" == "-R" -o "$1" == "-S" \
-o "$1" == "-W" -o "$1" == "-w"; then ARGS[${#ARGS[*]}]="$2"; shift; fi
if test "$1" == "-l"; then ARGS[${#ARGS[*]}]="$2"; shift; SUSER="$1"; fi
if test "$1" == "-i" -o "$1" == "-I"; then ARGS[${#ARGS[*]}]="$2"; shift; ISET=1; fi
shift
done
VM=$1
shift
USER=${VM%@*}
if test "$USER" != "$VM"; then VM=${VM##*@}; else USER=$SUSER; fi
is_uuid() { echo "$1" | grep '^[0-9a-f]\{8\}\-[0-9a-f]\{4\}\-[0-9a-f]\{4\}\-[0-9a-f]\{4\}\-[0-9a-f]\{12\}$' >/dev/null 2>&1; }
is_ip() { echo "$1" | grep '^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}$' >/dev/null 2>&1; }
getVPC()
{
# By convention, VMs are normally tagged with the VPC in OTC
firsttag=$(echo "$VMINFO" | jq '.tags[0]' | tr -d '"')
# If not, then look for router ports
if is_uuid $firsttag; then echo $firsttag; return 0; fi
NET=$(echo "$VMINFO" | jq '.interfaceAttachments[].net_id' | tr -d '"')
VPC=$(otc.sh custom GET "\$NEUTRON_URL/v2.0/ports.json?device_owner=network\:router_interface_distributed\&network_id=$NET" 2>/dev/null | jq '.ports[].device_id' | tr -d '"')
if is_uuid $VPC; then echo $VPC; return 0; fi
return 1
}
ipval()
{
declare -i val=0
OLDIFS="$IFS"
IFS="."
for oct in $1; do
val=$(((val<<8)+$oct))
done
IFS="$OLDIFS"
echo "$val"
}
# $1: net/pref
# $2-: IPs to test
# echos matching IP (if any)
ipmatch()
{
PREF=${1#*/}
MASK=$((0xffffffff^(0xffffffff>>$PREF)))
NET=$(ipval ${1%%/*})
NET=$((NET&MASK))
shift
#echo "$PREF $MASK $NET"
for ip in "$@"; do
#echo "Debug: ipval $ip & $MASK == $NET: $(ipval $ip): $(($(ipval $ip)&$MASK))" 1>&2
if test $(($(ipval $ip)&$MASK)) == $NET; then echo "$ip"; return 0; fi
done
return 1
}
getvm()
{
VM=$1
if ! is_uuid $VM; then
#echo "Looking up VM \"$VM\" ... " 1>&2
VM=$(otc.sh vm list name=$VM | head -n1 | awk '{ print $1; }')
if ! is_uuid $VM; then echo "No such VM \"$1\"" 1>&2 ; exit 2; fi
fi
VMINFO=$(otc.sh vm show $VM) || { echo "No such VM \"$VM\"" 1>&2; exit 2; }
IPS=$(echo "$VMINFO" | jq '.interfaceAttachments[] | select(.port_state == "ACTIVE") | .fixed_ips[].ip_address' | tr -d '"')
NAME=$(echo "$VMINFO" | jq '.server.name' | tr -d '"')
FLAVOR=$(echo "$VMINFO" | jq '.server.flavor.id' | tr -d '"')
IMGID=$(echo "$VMINFO" | jq '.server.image.id' | tr -d '"')
KEYNAME=$(echo "$VMINFO" | jq '.server.key_name' | tr -d '"')
IMGINFO=$(otc.sh image show $IMGID 2>/dev/null)
if test $? != 0 -o -z "$IMGINFO"; then
if test -z "$OSVER"; then OSVER=UNKNOWN; fi
IMGNAME="?"
else
IMGNAME=$(echo "$IMGINFO" | jq '.name' | tr -d '"')
OSVER=$(echo "$IMGINFO" | jq '.__os_version' | tr -d '"')
fi
if [[ "$OSVER" = "Ubuntu"* ]] && [ "$USER" == "linux" ]; then USER=ubuntu; fi
MYSUB=$(ip route show | grep '^[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/[0-9]* '); MYSUB=${MYSUB%% *}
#echo "$Debug: ipmatch $MYSUB" $IPS
IP=$(ipmatch $MYSUB $IPS)
echo -e "${YELLOW}#VM Info: $VM $NAME $FLAVOR $IMGNAME $OSVER${NORM}" $IPS 1>&2
# Fallback to old behavior
if test -z "$IP" -a -n "$IPS"; then IP=$(echo "$IPS" | tail -n1); fi
# Check VPC and use EIP if present and needed
MYVPC=$(otc.sh mds meta_data 2>/dev/null | jq .meta.vpc_id | tr -d '"')
if test -z "$MYVPC" -o "$MYVPC" == "null" -o -z "$IP" || test "$(getVPC)" != "$MYVPC"; then
#NPORT=$(echo "$VMINFO" | jq '.interfaceAttachments[] | select(.port_state == "ACTIVE") | .port_id' | tr -d '"')
for ip in $IPS; do
EIP=$(otc.sh eip list | grep " $ip " | awk '{ print $2; }')
if test -n "$EIP"; then
echo "#Using EIP $EIP ($ip) instead of IP $IP" 1>&2
IP=$EIP
break
fi
done
fi
}
getSSHkey()
{
if test -n "$SSH_AUTH_SOCK"; then
KEYS=$(ssh-add -l)
if echo "$KEYS" | grep "$KEYNAME" >/dev/null 2>&1; then unset SSHKEY; return; fi
fi
SSHKEY=~/.ssh/"$KEYNAME.pem"
test -r $SSHKEY || SSHKEY=~/"$KEYNAME.pem"
if ! test -r $SSHKEY; then
echo -e "${RED}#Need ~/.ssh/$KEYNAME.pem${NORM}" 1>&2
unset SSHKEY
else
SSHKEY="-i $SSHKEY"
fi
}
if is_ip "$VM"; then
IP=$VM
else
getvm $VM
fi
if test "$ISET" != 1; then getSSHkey; fi
if test "$WAIT" == "1"; then
declare -i ctr=0
PORT=${PORT:-22}
#TODO NCPROXY
echo "nc $NCPROXY -w 3 $IP $PORT" 1>&2
while test $ctr -le 150; do
echo "quit" | nc $NCPROXY -w 3 $IP $PORT >/dev/null 2>&1 && break
sleep 1
let ctr+=1
done
fi
echo "ssh ${ARGS[@]} $SSHKEY $USER@$IP $@" 1>&2
ssh ${ARGS[@]} $SSHKEY $USER@$IP $@
RC=$?
if test "$OS_USER_DOMAIN_NAME" == "OTC00000000001000000210" -o "$OS_USER_DOMAIN_NAME" == "OTC00000000001000010702" -o "$OS_USER_DOMAIN_NAME" == "OTC-AP-SG-00000000001000012052"; then
echo -e "${YELLOW}#Delete VM with otc.sh vm delete --rename $VM or nova delete $VM if you no longer need it.${NORM}" 1>&2
fi
exit $RC