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:
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
#!/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
No comments:
Post a Comment