SHELL编程局域网IP探活脚本
[*]SHELL脚本支持指定特定的网段;
[*]对特定的网段进行探活;
[*]将存活的IP地址写入存活的列表;
[*]将不存活的IP地址写入不存活的列表;
#!/bin/bash
#by author www.jfedu.net 2024
#######################
INPUT="0"
IP_LIST="$*"
RES_FILE1="/tmp/available.txt"
RES_FILE2="/tmp/unavailable.txt"
#Define check function 2022
check_lan(){
read -p "Please enter the LAN segment,example 192.168.1.0 (Netmask/24): " INPUT
if [ `echo $INPUT|sed 's/ /\n/g'|wc -l` -ne 0 ];then
for IP in $(seq 1 254)
do
IP_PREFIX=$(echo $INPUT|awk -F\. '{print $1"."$2"."$3"."}')
ping -c 2 -W1 ${IP_PREFIX}$IP >/dev/null 2>1
if [ $? -eq 0 ];then
echo "${IP_PREFIX}$IP is up."
echo "${IP_PREFIX}$IP" >> $RES_FILE1
else
echo "${IP_PREFIX}$IP is down."
echo "${IP_PREFIX}$IP" >> $RES_FILE2
fi
done
echo -e "\033[32m-------------------------\033[0m"
echo -e "\033[32mPlease check the following files:\033[0m"
echo "Available IP addresses: $RES_FILE1"
echo "Unavailable IP addresses: $RES_FILE2"
echo
fi
}
check_list()
{
read -p "Please enter the IP list to be checked,example list.txt: " INPUT
if [ ! -z $INPUT ];then
for IP in $(cat $INPUT)
do
ping -c 2 -W1 $IP >/dev/null 2>1
if [ $? -eq 0 ];then
echo "$IP is up."
echo $IP >> $RES_FILE1
else
echo "$IP is down."
echo $IP >> $RES_FILE2
fi
done
echo -e "\033[32m-------------------------\033[0m"
echo -e "\033[32mPlease check the following files:\033[0m"
echo "Available IP addresses: $RES_FILE1"
echo "Unavailable IP addresses: $RES_FILE2"
echo
fi
}
check_ip(){
read -p "Please enter the IP to be checked,example 1.1.1.1 | 1.1.1.2: " INPUT
for INPUT in `echo $INPUT`
do
while true
do
echo $INPUT|grep -E "\<({1,3}\.){3}{1,3}\>"
if [ $? -eq 0 ];then
IP=(`echo $INPUT|sed 's/\./ /g'`)
IP1=`echo ${IP}`
IP2=`echo ${IP}`
IP3=`echo ${IP}`
IP4=`echo ${IP}`
if [ $IP1 -gt 0 -a $IP1 -le 255 -a $IP2 -ge 0 -a $IP2 -le 255 -a $IP3 -ge 0 -a $IP3 -le 255 -a $IP4 -ge 0 -a $IP4 -lt 255 ];then
if [ `echo $INPUT|sed 's/ /\n/g'|wc -l` -ne 0 ];then
for IP in $(echo $INPUT)
do
ping -c 2 -W1 $IP >/dev/null 2>1
if [ $? -eq 0 ];then
echo "$IP is up."
echo $IP >> $RES_FILE1
else
echo "$IP is down."
echo $IP >> $RES_FILE2
fi
done
echo -e "\033[32m-------------------------\033[0m"
echo -e "\033[32mPlease check the following files:\033[0m"
echo "Available IP addresses: $RES_FILE1"
echo "Unavailable IP addresses: $RES_FILE2"
echo
fi
break;
else
read -p "Please Enter server IP address:" INPUT
fi
else
read -p "Please Enter server IP address:" INPUT
fi
done
done
}
case $1 in
1)
check_lan
;;
2)
check_ip
;;
3)
check_list
;;
*)
echo "---------------------------------------------"
echo -e "\033[34mWelcome to LAN live scripts:\033[0m"
echo -e "\033[32m1) check_lan\033[0m"
echo -e "\033[32m2) check_ip\033[0m"
echo -e "\033[32m3) check_list\033[0m"
echo -e "\033[32mUsage:{/bin/sh $0 1|2|3|4|5|help}\033[0m"
echo "---------------------------------------------"
esac
既然你诚信诚意的推荐了,那我就勉为其难的看看吧!京峰教育Linux论坛不走平凡路。
页:
[1]