-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-conectivity.sh
70 lines (61 loc) · 1.8 KB
/
check-conectivity.sh
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
#!/bin/bash
# Run as root
# /home/sshuttle/conncheck.sh
## replace 'root' user with any user that has global SSH access to servers
##host file must include IP Adressess that you want to check
file=/home/script/hosts
ncat_port=22
RED='\033[1;31m'
GREEN='\033[1;32m'
NC='\033[0m' # no color
bold=$(tput bold)
clear=$(tput sgr0)
RESULT=result.txt
if [ -f result.txt ]
then
echo
else
touch /home/script/$RESULT
fi
## check if netcat is installed
if (type nc 2>&1 >/dev/null)
then
echo "netcat is installed, proceeding.."
else
echo -e "${RED}[ERROR]${NC} netcat is not installed on this host"
exit 1
fi
while read -r line
do
if [[ -n $line ]] && [[ "${line}" != \* ]]
then
ip=$(echo $line | awk '{print $1}')
hostname=$(echo $line | awk '{print $2}')
## if ipv4
if [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "--------------------------------------"
## check netcat connectivity
if (nc -z -w 2 $ip $ncat_port 2>&1 >/dev/null)
then
ncat_status="nc OK"
echo "${hostname} (${ip}): ${ncat_status} " >> $RESULT
else
netcat_status2="nc ERROR"
echo "${hostname} (${ip}): ${netcat_status2} " >> $RESULT
fi
## attempt ssh connection, get exit code
ssh -o ConnectTimeout=3 \
-o "StrictHostKeyChecking no" \
-o BatchMode=yes \
-i /root/.ssh/id_rsa \
-q root@"${ip}" exit </dev/null
if [ $? -eq 0 ]
then
ssh_status="ssh OK"
else
ssh_status="${bold}ssh ERROR${clear}"
fi
echo "${hostname} (${ip}): ${ncat_status} | ${ssh_status}" >> $RESULT
fi
fi
done < "${file}"