This page looks best with JavaScript enabled

Shell Check Connection

 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
#!/bin/bash
#bash to check ping and telnet status.
cd /os-backup
#set -x;
#
#clear
SetParam() {
export URLFILE="Host_File.txt"
export PORTFILE="Host_PortFile.txt"
export TIME=`date +%Y-%m-%d_%H.%M.%S`
export port=80
export STATUS_UP=`echo -e "\E[32m[ RUNNING ]\E[0m"`
export STATUS_DOWN=`echo -e "\E[31m[ DOWN ]\E[0m"`
export MAIL_TO="aaa@xxx.com.tw,bbb@xx.com.tw"
export SHELL_LOG="`basename $0`.log"
}

Ping_Hosts() {

SetParam
cat $URLFILE | while read next
do

server=`echo $next | cut -d : -f1`

ping -i 2 -c 6 $server > /dev/null 2>&1

if [ $? -eq 0 ] ; then
echo "$TIME : Status Of Ping Host $server = $STATUS_UP";
else
echo "$TIME : Status Of Ping Host $server = $STATUS_DOWN";
echo "$TIME : Status Of Ping Host $server = $STATUS_DOWN" | mailx -s "$server Host DOWN!!!" $MAIL_TO

fi
done;
}

Telnet_Status() {

SetParam

cat $PORTFILE | while read next
do

server=`echo $next | cut -d : -f1`
port=`echo $next | awk -F":" '{print $2}'`

TELNETCOUNT=`sleep 5 | telnet $server $port | grep -v "Connection refused" | grep "Connected to" | grep -v grep | wc -l`

if [ $TELNETCOUNT -eq 1 ] ; then

echo -e "$TIME : $server ,Port :$port is \E[32m[ OPEN ]\E[0m";
else
echo -e "$TIME : $server ,Port :$port is \E[31m[ NOT OPEN ]\E[0m";
echo -e "$TIME : $server ,Port :$port is NOT OPEN" | mailx -s "$server:$port not open!!!" $MAIL_TO;

fi
done;
}
Main() {
Ping_Hosts
Telnet_Status
}
SetParam
Main | tee -a $SHELL_LOG

Host_File.txt

172.16.1.XXa
172.16.1.XXb
172.16.1.XXc
Host_PortFile.txt
172.16.1.XXa:d
172.16.1.XXb:e
172.16.1.XXc:f