Friday, December 07, 2012

Checking Connection Via Ping using Bash

Create bash file on the /tmp (you can put anywhere), this is just an example.
$ touch /tmp/ping.sh
added this code inside it
#!/bin/bash
ip=192.168.110.33
ping -c 3 $ip > result
grep 100% result > /dev/null
if [ $? = 0 ]
        # result dari $? kalau 0 artinya tidak output grep di screen
        # artinya, grep tidak mendapatkan apa yang di cari
        # dalam hal ini grep mencari 100% packet loss (host yg off)
        #
        then
        echo "Host off"
        else
        echo "Host on"
fi
Make sure the script has permission to execute
$ chmod +x ping.sh
Test it !
$ sh ping.sh
Result is "Host on" it mean the computer you try to check is a live.
Host on
else computer you try to check is off
Host off
Done

No comments: