-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh-instance
executable file
·138 lines (119 loc) · 2.97 KB
/
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
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
#!/usr/bin/env bash
#
# Copyright 2013 Victor Penso
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# exit if an uninitialised variable is used
#set -o nonunset
# exit of statement returns a non-true return value
#set -o errexit
VERSION=0.3
# Filename of this script
SCRIPT=${0##*/}
# Help text for this script
HELP=\
"Usage:
$SCRIPT [-rsh] [user@hostname] [command]
Execute a command on a remote node. Uses a ./ssh_config in
the working directory by default if it exists. If now
command is passed as argument /bin/bash will be spawned.
Options:
-r,--root
Login as root.
-s,--sudo
Remote execute a command with Sudo.
-A
Enables forwarding of the authentication agent connection.
-h,--help
Show this information.
-d,--debug
Enable debug messages."
# enable line numbers for debug output
if [ "$_DEBUG" = "true" ] ; then
export PS4='(${BASH_SOURCE}:${LINENO}):${FUNCNAME[0]}-[${SHLVL},${BASH_SUBSHELL},$?] '
fi
function _debug() {
if [ "$_DEBUG" = "true" ]; then
echo 1>&2 "Debug: $@"
fi
}
function _error() {
echo 1>&2 "Error: $@"
echo "See \`$SCRIPT --help\` for usage."
}
sudo=
options=
# Parse the command line options
ARGS=$(getopt -o hsrdA -l "help,sudo,root,debug" -- "$@")
eval set -- "$ARGS"
while true; do
case "$1" in
-h|--help)
echo "$HELP"
exit 0
;;
-s|--sudo)
sudo="sudo "
shift
;;
-r|--root)
options="-l root $options"
shift
;;
-d|--debug)
_DEBUG=true
shift
;;
-A)
options="-A $options"
shift
;;
--) shift; break ;;
*) break ;;
esac
done
ip=
if [ -e "$PWD/ssh_config" ]
then
options="-F $PWD/ssh_config $options"
# read the IP address from the configuration
ip=$(grep HostName $PWD/ssh_config | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')
fi
# if an IP address is known
if [ -n $ip ] ; then
# wait until VM instance can be pinged...
until $(ping -c 1 -w 1 $ip | grep -q "^64" ) ; do
sleep 1
done
# wait until the SSH port is available
while ! netcat -zv $ip 22 2>&- ; do
sleep 1
done
fi
host=instance
command=
if [ $# -eq 0 ] ; then
command="/usr/bin/env bash"
else
command="$@"
fi
command="$sudo$command"
_debug "Execute command [ssh -gt $options $host \"$command\"]"
ssh -A -gt $options $host "$command" 2>/dev/null
if [ $? -ne 0 ]
then
_error "Host [$host] failed to execute [$@]!"
fi
exit 0