-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpong.sh
More file actions
190 lines (160 loc) · 4.07 KB
/
Copy pathpong.sh
File metadata and controls
190 lines (160 loc) · 4.07 KB
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
183
184
185
186
187
188
189
190
#! /bin/cat
type show_pass >/dev/null 2>&1 || . ~/bash/crayons.sh
# set -e
# x
# xx
# xxx
# xxxx
pong () {
local __doc__="""pong(""$@"")"""
declare -a hosts_=($(ssh_host "$@"))
local host_= remote_= options_="$@"
if [[ $hosts_ ]]; then
host_=${hosts_[0]}
remote_=${hosts_[1]}
options_=$(echo "$options_" | sed -e "s/$host_//")
else
remote_="$@"
fi
[[ $options_ =~ -t ]] || options_="-t1 $options_"
[[ $options_ =~ -c ]] || options_="-c1 $options_"
local quiet_=
[[ $options_ =~ -q ]] && quiet_=1
[[ $options_ =~ -q ]] && options=${options_/ -q/}
local ping_="ping $options_ $remote_"
local pinged_=$([[ $quiet_ ]] && $ping_ 2>/dev/null || $ping_)
[[ $? == 0 ]] || return 1
[[ $quiet_ == 1 ]] || echo $_pinged | grep "([0-9]*[.][0-9.]*)" | sed -e "s/PING //" -e "s/ (/ -> /" -e "s/).*//" | grep ' [0-9.]*'
return 0
}
pongable () {
local remote_=$1
pong -q -c1 -t1 $remote_
}
poss () {
ssh $1 "hostname"
}
# xxxxx
# xxxxxx
online () {
[[ "$@" ]] && show_online "$@" || online_all
}
# xxxxxxx
# xxxxxxxx
ssh_host () {
local __doc__="""Get a server name from the hostname in ~/.ssh/config ($@)"""
local host_= remote_=
while [[ $1 ]]; do
if [[ $1 =~ ^- ]]; then
shift
elif [[ $1 =~ ^[0-9]*$ ]]; then
shift
else
host_=$1
remote_=$(ssh -G $host_ | grep ^hostname | cut -d' ' -f2)
shift
fi
done
[[ $host_ ]] || return 1
[[ $remote_ ]] || return 1
echo $host_ $remote_
return 0
}
# xxxxxxxxx
# xxxxxxxxxx
online_all () {
show_online www.google.com
online_macs
online_workers
}
pong_local () {
pong -t1 $1.local
}
is_online () {
pingable "$@" && show_pass $(pong -q "$@") || show_fail "$@"
}
pingable () {
quick_pong -q "$@" >/dev/null 2>&1 && return 0
return 1
}
is_local_server () {
[[ $1 =~ [.]local$ ]]
}
quick_pong () {
# local __doc__="""quick_pong(""$@"")"""
local _timeout="-t 3" _wait="-W 1" _remote="$1"
# pingable && _timeout="-t 1"
is_local_server $_remote && _timeout="-t 1"
is_work_server $_remote && _timeout="-t 2"
if pong $_timeout $_wait $_remote 2>&1 | grep -q -e usage -e illegal -e invalid; then
pong ${_timeout/t/w} $_wait $_remote
else
# re-create stdout
pong $_timeout $_wait $_remote
fi
}
# xxxxxxxxxxx
show_online () {
local line_end_=-l
[[ $1 == --no-line ]] && line_end_=
[[ $line_end_ ]] || shift
if pingable "$@"; then
local pong_out_=$(pong "$@")
[[ $pong_out_ ]] || pong_out_="pingable $@"
if [[ $line_end_ == -l ]]; then
green_line $pong_out_
else
green $pong_out_
fi
return 0
fi
show_red $line_end_ "$@" offline
return 1
}
show_worker () {
local options_=
while [[ $1 =~ ^[-] ]]; do
options_="$options_ $1"
shift
done
show_online $options_ $(worker $1)
}
online_mac () {
local show_=green_line
pongable $1 || show_=red_line
$show_ $1
}
online_worker () {
local show_=green_line
pongable $1 || show_=red_line
$show_ $1
}
online_macs () {
online_mac book.local || return 1
online_mac mac.local || return 2
online_mac mini.local || return 3
return 0
}
online_workers () {
runnable worker || return 1
online_worker git.wwts.com || return 2
online_worker twkgit32.wwts.com || return 3
local host_= project_= worker_=
show_portals "twkwbe40:eopdev" "twkwbe41:eoptest" "twkwbe42:eopstage" "atlwbe43:eopbeta" "twkwbe43:eop"
show_portals "twkwbe54:wmpdev" "twkwbe55:wmptest" "twkwbe56:wmpbeta" "twkwbe57:wmp"
show_portals "twkwbe63:sosotest " "twkwbe64:sosobeta" "twkwbe65:soso"
}
show_portals () {
for portal_ in "$@" ; do
host_="${portal_/:*/}"
project_="${portal_/*:/}"
show_worker --no-line $project_
echo -n ", "
show_worker $host_
done
}
# xxxxxxxxxxxx
dns_refresh () {
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
}