diff options
author | Andrew Cady <d@jerkface.net> | 2022-11-28 14:08:45 -0500 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2022-11-28 14:08:45 -0500 |
commit | 624658b8991650401852c60ee9c73615a6c22e89 (patch) | |
tree | 8f59b22124a47dd8b7174834a2575ad8fb50b53b | |
parent | 352de7543cc6ce173efee0758e5d5bd2746deb12 (diff) |
new command pingwatch
-rwxr-xr-x | dot/local/bin/pingwatch | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/dot/local/bin/pingwatch b/dot/local/bin/pingwatch new file mode 100755 index 0000000..a67d014 --- /dev/null +++ b/dot/local/bin/pingwatch | |||
@@ -0,0 +1,41 @@ | |||
1 | #!/bin/bash | ||
2 | loud= | ||
3 | target=${1:-2601:401:8200:2d4c:c6a5:6c8c:1304:bcf7} | ||
4 | |||
5 | process() | ||
6 | { | ||
7 | state= | ||
8 | while true | ||
9 | do | ||
10 | if read -t 5 line | ||
11 | then | ||
12 | if [ "$loud" ] | ||
13 | then | ||
14 | printf '%s\n' "$line" | ||
15 | fi | ||
16 | case "$line" in | ||
17 | PING*) continue ;; | ||
18 | esac | ||
19 | if [ "$state" = on ] | ||
20 | then | ||
21 | continue | ||
22 | else | ||
23 | date +'%c Online.' | ||
24 | state=on | ||
25 | fi | ||
26 | elif [ $? -gt 128 ] # read timeout | ||
27 | then | ||
28 | if [ "$state" = off ] | ||
29 | then | ||
30 | continue | ||
31 | else | ||
32 | date +'%c Offline.' | ||
33 | state=off | ||
34 | fi | ||
35 | else # non-timeout read error | ||
36 | break | ||
37 | fi | ||
38 | done | ||
39 | } | ||
40 | |||
41 | ping -n "$target" | process | ||