bash-networkconnections © Maximilian Riess / Riess Group.

Netzwerkverbindungen per Shellscript prüfen

Für wiederkehrendes Prüfen, ob Netzwerkverbindungen erreichbar sind, gibt es hier ein kleines Shellscript:

 

root@hostname:~$ cat checkconnection.sh
#!/bin/bash
#title           :checkconnection.sh
#description     :checks network connectivity to specified host:port tupples.
#author          :maximilian.riess@riess-group.de
#date            :20160201
#version         :1.1
#usage           :bash ./checkconnection.sh
#notes           :needs a config file named remotes.csv which contain hosts.
#notes           :v1.1: without port information, ping is used to determine 
#notes           :      connection is working. works also with port=0
#bash_version    :4.1.5(1)-release
#==============================================================================
# example remotes.csv:
# # check connections to defined hosts
# # format is:
# # hostname:port:<opt infotext>
#
# 127.0.0.1:22:lokales ssh
# 127.0.0.1:0:ping test
# 127.0.0.1::
# 127.0.0.1
#==============================================================================
filename="$1"
if [ "x$filename" == "x" ] ; 
then
        filename="remotes.csv"
fi

okcnt=0
facnt=0

function check_host_ping {
        host=$1
        info=$2

        infostr="ping connection to ${host} "
        filler=".................................................................."
        if ping -c 1 -W 1 "$host" > /dev/null; then
                printf "%s %s [OK]     %s\n" "${infostr}" "${filler:${#infostr}}" "$info"
                okcnt=$(($okcnt+1))
        else
                printf "%s %s [failed] %s\n" "${infostr}" "${filler:${#infostr}}" "$info"
                facnt=$(($facnt+1))
        fi

}

function check_host_port {
        host=$1
        port=$2
        info=$3

        if [ "${info}" == "${port}" ] ; then
                info=""
        fi

        if [ "${port}" == "" ] || [ "${port}" == "${host}" ] || [ "${port}" == "0" ] ; then
                check_host_ping $host $info
                return
        fi

        infostr="tcp connection to ${host} port ${port} "
        filler=".................................................................."
 
        if nc $host $port -w 3 -z 
        then
                printf "%s %s [OK]     %s\n" "$infostr" "${filler:${#infostr}}" "$info"
                okcnt=$(($okcnt+1))
        else
                errorcode=$?
                printf "%s %s [failed] %s\n" "$infostr" "${filler:${#infostr}}" "$info"
                facnt=$(($facnt+1))
        fi
}


function runtest {

        while read -r line
        do
                strhost=$(echo $line | cut -f1 -d: )
                strport=$(echo $line | cut -f2 -d: )
                strinfo=$(echo $line | cut -f3 -d: )
                if [ "x$strhost" == "x" ] || [ "${strhost:0:1}" == "#" ] 
                then
                        continue;
                fi

                check_host_port $strhost $strport "$strinfo"
                done < "$filename"
}


echo "check connectivity run at $(date)" 
echo "================================================================================" 
runtest
echo "================================================================================" 
echo "             suceeded: ${okcnt}     failed: ${facnt}"
echo "test completed."



 

Um die zu testenden Hosts zu konfigurieren, die Hosts mit der entsprechenden Portnummer in der Datei remotes.csv ablegen:

root@hostname:~$ cat remotes.csv 
#
# check connections to defined hosts
# format is:
# hostname:port:<opt infotext>
#
#

127.0.0.1:22:lokales ssh
192.168.178.1:8080:proxy
192.168.178.1:22:ssh
192.168.178.2:22:offlinehost
192.168.178.1:0:nur ping test

 

Das Ergebnis sieht dann exemplarisch so aus:

root@hostname:~$ ./checkconnection.sh 
check connectivity run at Tue Nov 10 13:15:00 CET 2015
================================================================================
tcp connection to 127.0.0.1 port 22  .............................. [OK]     lokales ssh
tcp connection to 192.168.178.1 port 8080  ........................ [OK]     proxy
tcp connection to 192.168.178.1 port 22  .......................... [OK]     ssh
tcp connection to 192.168.178.5 port 22  .......................... [failed] offlinehost
ping connection to 192.168.178.1  ................................. [OK] nur ping test
================================================================================
             suceeded: 4     failed: 1
test completed.

Das Script wurde am 01.02.2016 angepasst um auch Verbindungen über Ping prüfen zu können.

Schreibe einen Kommentar