Showing posts with label ping. Show all posts
Showing posts with label ping. Show all posts

Monday, April 28, 2014

Bash - Ping ip range and email result

In Bash script is a very simple to write script for checking availability of network devices. In this case, the script will ping an address range and rusults will be sent to your email address.

#!/bin/bash
om="mailtempfile"
dat="$(date)"
eval "echo 'This is Computer status from Linips Script at $dat' >${om}"
eval "echo '' >>${om}"

for i in {1..6}
do
    out="$(ping 192.168.1.$i -c 5|tail -1|awk '{print $4}'|cut -d '/' -f 2)"
    case $i in
    1)
        comp="Router One     "
        ;;
    2)
        comp="Router Two     "
        ;;
    3)
        comp="Router Three   "
        ;;
    4)
        comp="Server One     "
        ;;
    5)
        comp="Server Two     "
        ;;
    6)
        comp="Server Three   "
        ;;
    esac
    eval "echo '192.168.1.$i   $comp    $out'>>${om}"
done
mail -s "Net status $dat" myemail@adress <mailtempfile


Or, the same script, with using Bash script arrays:

#!/bin/bash
comp[1]="Router One      "
comp[2]="Router Two      "
comp[3]="Router Three    "
comp[4]="Server One      "
comp[5]="Server Two      "
comp[6]="Server Three    "

om="mailtempfile"
dat="$(date)"
eval "echo 'This is Computer status from Linips Script at $dat' >${om}"
eval "echo '' >>${om}"

for i in {1..6}
do
    out="$(ping 192.168.1.$i -c 5|tail -1|awk '{print $4}'|cut -d '/' -f 2)"
    eval "echo '192.168.1.$i   ${comp[$i]}    $out'>>${om}"
done
mail -s "Net status $dat" myemail@address <mailtempfile



Received email:

This is Computer status from Linips Script at Mon Apr 28 08:13:45 CEST 2014

192.168.1.1   Router One         2.493
192.168.1.2   Router Two         17.712
192.168.1.3   Router Three       13.669
192.168.1.4   Server One         4.601
192.168.1.5   Server Two         8.199
192.168.1.6   Server Three       9.302


And finally, the greatest strenght of this script is that it runs automatically, so you need to add one line in the /etc/crontab file: (example is for script running every 20 min)

*/20  *       * * *     root     /home/user/pingscript..sh

Thursday, April 24, 2014

BASH - customize ping output

Problem is simply: how to get the output of ping command suited to our needs. In this example we will extract average ping time in Bash scripting language.

First let's see output of ping command: ping 192.168.1.1 -c 5 (-c 5 tells to ping command to run five times, else it will run infinitely.)

$ ping 192.168.1.1 -c 5
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=2.62 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.477 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.482 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=0.482 ms
64 bytes from 192.168.1.1: icmp_seq=5 ttl=64 time=0.487 ms

--- 192.168.1.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4000ms
rtt min/avg/max/mdev = 0.477/0.911/2.629/0.859 ms

Average ping time is in last line. Use tail -1 to extract only last line. 

$ ping 192.168.1.1 -c 5|tail -1
rtt min/avg/max/mdev = 0.477/0.911/2.629/0.859 ms

Then extract values, count 'arguments' in this line separated by space: 'rtt' is the first, 'min/avg/max/mdev' 2nd, '=' 3rd, '0.477/0.911/2.629/0.859' 4th and 'ms' 5th. 

$ ping 192.168.1.1 -c 5|tail -1|awk '{print $4}'
0.477/0.911/2.629/0.859

and finally, from this output of four '/' separated values, take the second value: this is done by cut command: 

$ ping 192.168.1.1 -c 5|tail -1|awk '{print $4}'|cut -d '/' -f 2
0.911

and this is the value that we are looking for...