This repository has been archived by the owner on Mar 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcssh
executable file
·75 lines (66 loc) · 1.45 KB
/
gcssh
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
#!/bin/bash
CACHE_FILE=~/.gssh-zone-cache
if [ $# -lt 1 -o "$1" = "-h" -o "$1" = "--help" ]
then
echo "Usage:"
echo "$0 --get-zone hostname : Get the zone for a host"
echo "$0 --flush-cache : Flush all hosts from the zone cache"
echo "$0 --flush-cache hostname : Flush a specific host from the zone cache"
echo "$0 hostname [gcloud arguments...]"
echo
exit 1
fi
if [ "$1" = "--flush-cache" ]
then
if [ $# -eq 1 ]
then
rm -f "$CACHE_FILE"
else
# BSD sed needs the -E argument but GNU sed does not
if [ `uname` = "Darwin" ]
then
sed -E -i.bak "/^$2:/d" "$CACHE_FILE"
else
sed -i.bak "/^$2:/d" "$CACHE_FILE"
fi
fi
exit 0
fi
GETZONE=0
if [ "$1" = "--get-zone" ]
then
GETZONE=1
shift
fi
# Check for the zone in the cache
GCEHOST="$1"
shift
if [ -f "$CACHE_FILE" ]
then
ZONE=`grep "^$GCEHOST:" "$CACHE_FILE" | sed 's/^[^:]*:\(.*\)$/\1/'`
if [ "$ZONE" != "" ]
then
echo "Using zone $ZONE (from cache)" >&2
fi
else
ZONE=""
fi
if [ "$ZONE" = "" ]
then
# Get the zone from gcloud and store it in the cache
echo "Detecting zone for $GCEHOST..." >&2
ZONE=`gcloud compute instances list --filter="name=('$GCEHOST')" | grep "$GCEHOST" | awk '{print $2}'`
if [ "$ZONE" = "" ]
then
echo "ERROR: Could not detect the zone for $GCEHOST" >&2
exit 1
fi
echo "Zone detected as $ZONE" >&2
echo "$GCEHOST:$ZONE" >>"$CACHE_FILE"
fi
if [ $GETZONE -eq 1 ]
then
echo $ZONE
else
gcloud compute ssh $GCEHOST --ssh-flag="-A" --zone $ZONE $@
fi