-
Notifications
You must be signed in to change notification settings - Fork 1
/
awsssh
executable file
·100 lines (82 loc) · 2.19 KB
/
awsssh
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
#!/bin/bash
#
# Given an aws instance name, ssh to it
#
# Usage:
# awssh <instance_name> [ <command> ]
#
# Depends on:
# 1) ~/git/mci/config/distros.yml for usernames
# 2) ~/.ssh/<key name>.pem with ssh private keys named after amazon key name
# 3) cache_run executable
# 4) instance_info_by_name.sh executable
set -o nounset
set -o errexit
# how long to cache aws info
CACHE_SECONDS=300
. getuserbydistro.sh
if [[ "${1:-}" == "-t" ]]
then
extra_args=" -t"
shift
else
extra_args=""
fi
instance_name="${1:-}"
shift
if echo $instance_name | grep -q @
then
user=$(echo $instance_name | cut -f1 -d@)
instance_name=$(echo $instance_name | cut -f2 -d@)
fi
if [ ! "$instance_name" ]
then
echo "usage: $0 <instance_name> [ <command> ]"
exit 2
fi
if [ ! "${AWS_SECRET_ACCESS_KEY:-}" -o ! "${AWS_ACCESS_KEY_ID:-}" ]
then
. ~/amazon-build.sh
fi
count=$(ec2list running $instance_name | wc -l)
header=$(ec2list | head -1)
data=$(ec2list running $instance_name | tail -1)
if [ "$count" -gt 2 ]
then
data=$(ec2list ernie running $instance_name | tail -1)
if [ ! "$data" ]
then
data=$(ec2list running $instance_name | head -1)
fi
fi
if [ ! "$data" ]
then
clear_cache
data=$(ec2list ernie running $instance_name | tail -1)
fi
# get column numbers
distro_column="$(echo "$header" | tr , \\n | grep -nx distro | cut -f1 -d: )"
dns_column="$(echo "$header" | tr , \\n | grep -nx PublicDnsName | cut -f1 -d: )"
key_column="$(echo "$header" | tr , \\n | grep -nx KeyName | cut -f1 -d: )"
name_column="$(echo "$header" | tr , \\n | grep -nx Name | cut -f1 -d: )"
distro="$(echo "$data" | cut -f$distro_column -d,)"
dns="$(echo "$data" | cut -f$dns_column -d,)"
key="$(echo "$data" | cut -f$key_column -d,)"
name="$(echo "$data" | cut -f$name_column -d,)"
if [ ! "$dns" -o "$dns" = "running" ]
then
echo "Can't find dns info for server ($data)"
exit 3
fi
if [ ! "${user:-}" ]
then
user=$(getuserbydistro "$distro" "$name")
fi
if [ ! "$user" ]
then
user=root
fi
echo connecting to $name
echo "$data"
set -x
ssh $extra_args -o UserKnownHostsFile=/dev/null -o ConnectTimeout=5 -o KbdInteractiveAuthentication=no -o PasswordAuthentication=no -o StrictHostKeyChecking=no -i ~/.ssh/$key.pem $user@$dns "$@"