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/
added this code inside it
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)
        echo "Host off"
        echo "Host on"
Make sure the script has permission to execute
$ chmod +x
Test it !
$ 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

No comments: