-
Notifications
You must be signed in to change notification settings - Fork 3
/
remote_vme
executable file
·129 lines (109 loc) · 2.26 KB
/
remote_vme
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
#!/bin/bash
#
# remote_vme <hostname> <username> <command for remote host>
#
CRATE=
USER_ARG=
SSH_ARG=
CONNECT_TIMEOUT=600
USENMAP=1
isConnected=0
iter=0
if [ "$#" -lt 3 ]; then #server name provided
echo "ERROR: Mush have 3 arguments!"
echo " $# args provided: $@"
exit 1
fi
CRATE=$1
if [ "$2" == "nobody" ]; then
USER_ARG=
else
USER_ARG="-l $2"
fi
shift 2
SSH_ARG="$@"
SSH_COMMAND="ssh -t -o ServerAliveInterval=10 $USER_ARG $CRATE $SSH_ARG"
trap "exit" exit
function updateTicker()
{
let "iter += 1"
case "$iter" in
0 ) printf " - " "";;
1 ) printf " \ " "";;
2 ) printf " | " "";;
3 ) printf " / " ""; iter=-1;
esac
}
function check4nmap()
{
nmap -version &> /dev/null
rval=$?
if [ "$rval" = "0" ]; then
USENMAP=1
else
echo "no nmap"
USENMAP=0
fi
}
function checkCrate()
{
local __hostname=$1
local tmpfile=`mktemp`
local statHostname=''
if [ "$USENMAP" = "1" ]; then
nmap -oG $tmpfile -T5 -p22 $__hostname > /dev/null
statHostname=`grep -v "^#" $tmpfile | grep $CRATE | grep open`
/bin/rm -f $tmpfile
else
ssh -o ConnectTimeout=1 $__hostname echo
local rval=$?
if [ "$rval" = 0 ]; then
statHostname="ok"
fi
fi
printf "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
if [ -z "$statHostname" ]; then
printf "${CRATE}: Disconnected" ""
updateTicker
isConnected=0
else
printf "${CRATE}: Connected" ""
echo
isConnected=1
fi
}
if [ "$USENMAP" = "1" ]; then
check4nmap
fi
while true; do
STARTTIME=$(date '+%s')
while [ $isConnected == 0 ]; do
checkCrate $CRATE isConnected
if [ $isConnected == 0 ]; then
CURRENT=$(date '+%s')
TIMEDIFF=$(($CURRENT-$STARTTIME))
if [ "$TIMEDIFF" -gt "$CONNECT_TIMEOUT" ]; then
echo
echo "Connection Timeout"
exit;
fi
# echo TIMEDIFF = $TIMEDIFF
sleep 5
else
CURRENT=0
fi
done
echo
echo "****************************************"
echo "* SSHing to $CRATE"
echo "****************************************"
date
echo
${SSH_COMMAND}
isConnected=0
echo
echo "****************************************"
echo "* ssh KILLED... RESTARTING"
echo "****************************************"
echo
done