-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtest_peers_conn
executable file
·147 lines (118 loc) · 3.27 KB
/
test_peers_conn
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
#!/bin/bash
# Copyright 2020 DSR Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -eu
set -o pipefail
TIMEOUT_DEF=3
PERSISTENT_PEERS_FILE="${1:-./persistent_peers.txt}"
TIMEOUT="${TIMEOUT:-$TIMEOUT_DEF}"
PASSED_RES=PASSED
SKIPPED_RES=SKIPPED
FAILED_RES=FAILED
NETCAT=""
IP_TOOL=ip
SELF_IPS=""
CHECK_TOOL=""
final_res=true
function usage {
echo "\
Usage: $0 [persistent-peers-file]
Tests connections to peers from the persistent peers file.
If the file is not specified './persistent_peers.txt' is used.
The tool assumes that either netcat or telnet is available.
TIMEOUT environment variable may be used to set connections
timeout (seconds). Default: 3 seconds.
"
}
function test_env {
set -u
local i
for i in nc netcat ncat; do
if [[ -n "$(which $i)" ]]; then
NETCAT="$i"
CHECK_TOOL=netcat
# echo "NOTE: 'netcat' tool is found"
break
fi
done
if [[ -z "$NETCAT" ]]; then
if [[ -n "$(which telnet)" ]]; then
CHECK_TOOL=telnet
# echo "NOTE: 'netcat' tool is not found, switching to 'telnet'"
else
echo "ERROR: neither 'netcat' nor 'telnet' tool is available, please install one and run again"
exit 1
fi
fi
if [[ -n "$(which "$IP_TOOL")" ]]; then
# TODO may fail for specific cases
SELF_IPS="$(ip -br -f inet a | grep -v 127.0.0.1 | awk '{print $NF}' | paste -s)"
else
echo "WARNING: 'ip' tool is not found, skipping self IP filtering"
fi
}
function telnet_check {
set -eu
local _telnet_res="$(timeout "$TIMEOUT" telnet "$1" "$2" 2>&1)"
if [[ "$_telnet_res" == *"Connected"* ]]; then
echo "$PASSED_RES"
else
echo "$FAILED_RES"
fi
}
function netcat_check {
set -u
"$NETCAT" -w "$TIMEOUT" -z "$1" "$2" 2>&1 >/dev/null
if [[ $? -eq 0 ]]; then
echo "$PASSED_RES"
else
echo "$FAILED_RES"
fi
}
function check {
if [[ "$CHECK_TOOL" == "telnet" ]]; then
telnet_check "$@"
else
netcat_check "$@"
fi
}
if [[ ("${1-}" == "-h" || "${1:-}" == "--help") ]]; then
usage
exit 0
fi
if [[ ! -f "$PERSISTENT_PEERS_FILE" ]]; then
echo "Error. '$PERSISTENT_PEERS_FILE' file not found"
usage
exit 1
fi
test_env
IFS=',' read -a peers <<< "$(cat "$PERSISTENT_PEERS_FILE")"
for peer in "${peers[@]}"
do
IFS=':' read -a _arr <<< "$(echo "$peer" | cut -d @ -f 2)"
if [[ "$SELF_IPS" == *"${_arr[0]}/"* ]]; then
res="$SKIPPED_RES"
else
res="$(check "${_arr[0]}" "${_arr[1]}")"
fi
echo "Connection to peer '${_arr[0]}:${_arr[1]}' $res"
if [[ "$res" == "$FAILED_RES" ]]; then
final_res=false
fi
done
if [[ "$final_res" == true ]]; then
exit 0
else
exit 1
fi