본문 바로가기
HACKING/Network

Script#1 Bash script to sweep ping

by asdf12345 2022. 9. 6.

 

  • 다수 호스트의 ping 응답 여부 확인#1  Hosts up

In this example, the Bash script will scan the network for hosts attached to an IP address 10.1.1.1 – 255. The script will print message Node with IP: IP-address is up if ping command was successful. Feel free to modify the script to scan your hosts range.

#!/bin/bash

is_alive_ping()
{
  ping -c 1 $1 > /dev/null
  [ $? -eq 0 ] && echo Node with IP: $i is up.
}

for i in 10.1.1.{1..255} 
do
is_alive_ping $i & disown
done

 

 

  • 다수 호스트의 ping 응답 여부 확인#2  Hosts down

In this next ping Bash script example, we will send an email to a specified email address when ping cannot reach its destination. The system admin can execute this in script regularly with use of a cron scheduler.

The script first uses ping command to ping a host or IP supplied as an argument. In case that destination is unreachable, a mail command will be used to notify the system administrator about this event.

#!/bin/bash

for i in $@
do
ping -c 1 $i &> /dev/null

if [ $? -ne 0 ]; then
	echo "`date`: ping failed, $i host is down!" | mail -s "$i host is down!" my@email.address 
fi
done

 

 

  • IP 지정

 

#!/bin/bash
IPs="10.11.1.123 10.11.1.125 10.11.124"

is_alive_ping()
{
  ping -c 1 $1 > /dev/null
  [ $? -eq 0 ] && echo Node with IP: $i is up.
}

for i in $IPs; do
is_alive_ping $i & disown
done