写一个shell脚本,把192.168.0.0/24网段在线的ip列出来。
思路: for循环, 0.1 -- 0.254 依次去ping,能通说明在线。
思路: for循环, 0.1 -- 0.254 依次去ping,能通说明在线。
0
- #!/bin/bash
- #
- #时间:
- #作者
- #说明
- IP_num="192.168.0."
- for i in $(seq 1 254)
- do
- ping -c 1 ${IP_num}$i > /dev/null
- if [ $? -eq 0 ]
- then
- echo ${IP_num}$i" OK"
- else
- echo ${IP_num}$i" error"
- fi
- done
0
#!/bin/sh
ipd=192.168.1.
for a in `seq 1 254`
do
ping -c 2 $ipd$a >/dev/null
if [ $? = 0 ]
then
echo "echo $ipd$a is on"
echo $ipd$a is on >> 2.txt
else
echo "echo $ipd$a is not"
echo $ipd$a is not >>3.txt
fi
done
ipd=192.168.1.
for a in `seq 1 254`
do
ping -c 2 $ipd$a >/dev/null
if [ $? = 0 ]
then
echo "echo $ipd$a is on"
echo $ipd$a is on >> 2.txt
else
echo "echo $ipd$a is not"
echo $ipd$a is not >>3.txt
fi
done
0
#!/bin/bash
##
for i in `seq 1 254`
do
ping -c1 -w1 192.168.0.$i
if [ "$?" -eq 0 ]
then
echo "192.168.0.$i" >> 1.txt
fi
done
#!/bin/bash
##
for i in `seq 1 254`
do
ping -c1 -w1 192.168.0.$i
if [ "$?" -eq 0 ]
then
echo "192.168.0.$i" >> 1.txt
fi
done
0
ping -c 1 ${IP_num}$i > /dev/null ping的通返回码是0,反之就是非零
if [ $? -eq 0 ] $?表示获取的是程序执行的返回码
方琪 发表于 2015-11-29 18:11
能否解释下 ping -c 1 ${IP_num}$i > /dev/null
if [ $? -eq 0 ]
这两句的后面这句。为什么 ...
ping -c 1 ${IP_num}$i > /dev/null ping的通返回码是0,反之就是非零
if [ $? -eq 0 ] $?表示获取的是程序执行的返回码
0
明白了 多谢说明
陈沛 发表于 2015-11-29 22:05
ping -c 1 ${IP_num}$i > /dev/null ping的通返回码是0,反之就是非零
if [ $? -eq 0 ] ...
明白了 多谢说明
0
#! /bin/bash
while :;
do
read -p "please enter the scan iprang: " num
if echo "$num"|[ -n "`sed -n '/[a-Z]/p'`" ]
then
echo "error,please enter a number,again"
elif [ $num -ge 0 ] && [ $num -le 254 ]
then
IP_num="192.168.$num."
for i in $(seq 1 254)
do
ping -c 1 ${IP_num}$i > /dev/null
if [ $? -eq 0 ]
then
echo ${IP_num}$i" up"
else
echo ${IP_num}$i" down"
fi
done
else
echo "error,please enter a number,again"
fi
done
while :;
do
read -p "please enter the scan iprang: " num
if echo "$num"|[ -n "`sed -n '/[a-Z]/p'`" ]
then
echo "error,please enter a number,again"
elif [ $num -ge 0 ] && [ $num -le 254 ]
then
IP_num="192.168.$num."
for i in $(seq 1 254)
do
ping -c 1 ${IP_num}$i > /dev/null
if [ $? -eq 0 ]
then
echo ${IP_num}$i" up"
else
echo ${IP_num}$i" down"
fi
done
else
echo "error,please enter a number,again"
fi
done
0
- #!/bin/bash
- # By xueyongbo
- # 2015/12/6 20:00
- trap "exit 1" INT
- ip_num=192.168.0.
- for i in `seq 1 254`
- do
- ping -c 1 -w 5 ${ip_num}$i > /dev/null
- if [ $? -eq 0 ]
- then
- echo ${ip_num}$i >> onlineip
- else
- echo ${ip_num}$i >> offlineip
- fi
- done
- echo "those is online."
- cat onlineip
- echo "those is offline."
- cat offlineip
0
本帖最后由 balich 于 2015-12-14 10:48 编辑
#!/bin/bash
#2015-12-12
#
for n in {1..254};do
host=192.168.0.$n
ping -c2 $host &> /dev/null
if [ $? =0 ];then
echo "$host is UP."
echo "$host" >> /root/onlinehost.txt
else
echo "$host is DOWN."
fi
done
#!/bin/bash
#2015-12-12
#
for n in {1..254};do
host=192.168.0.$n
ping -c2 $host &> /dev/null
if [ $? =0 ];then
echo "$host is UP."
echo "$host" >> /root/onlinehost.txt
else
echo "$host is DOWN."
fi
done
0
本帖最后由 zql254 于 2015-12-12 22:20 编辑
- #!/bin/bash
- ipa="192.168.0."
- for i in `seq 1 254`;do
- ping -c 1 $ipa$i>/dev/null&&echo "$ipa$i in online"||echo "$ipa$i is offline"
- done
0
- #!/bin/bash
- for i in `seq 1 254`;do
- ping -c1 -W1 192.168.0.$i > /dev/null
- [ $? -eq 0 ] && echo 192.168.0.$i
- done
0
#!/bin/bash
#
for i in `seq 1 254`; do
ping -c 1 -W 1 192.168.0.$i >& /dev/null
if [ $? -eq 0 ];then
echo "192.168.0.$i is online"
else
echo "192.168.0.$i is down"
fi
done
#
for i in `seq 1 254`; do
ping -c 1 -W 1 192.168.0.$i >& /dev/null
if [ $? -eq 0 ];then
echo "192.168.0.$i is online"
else
echo "192.168.0.$i is down"
fi
done
0
- #!/bin/bash
- #functions:
- # check alive hosts!!!
- # then echo the ip.
- IP=121.42.170.
- echo "the online hosts ip:"
- for i in `seq 1 245`;do
- ping -c1 $IP$i > /dev/null
- if [ $? -eq 0 ];then
- echo "$IP$i"
- fi
- done
- exit 0
0
- #!/bin/bash
- ip="192.168.0."
- for i in `seq 1 254`;do
- ping -c1 -w1 ${ip}${i} >/dev/null
- if [[ $? -eq 0 ]];then
- echo ${ip}${i}" is useful!"
- fi
- done
0
#!/bin/sh
#author@zhanwang.xie
IP_graph='192.168.1.'
for i in `seq 1 254`
do
ping -c 1 ${IP_graph}$i >/dev/null
if [[ $? -eq 0 ]];then
echo ${IP_graph}$i" is online!"
fi
done
#author@zhanwang.xie
IP_graph='192.168.1.'
for i in `seq 1 254`
do
ping -c 1 ${IP_graph}$i >/dev/null
if [[ $? -eq 0 ]];then
echo ${IP_graph}$i" is online!"
fi
done
0
#!/bin/bash
ip=192.168.0.
for i in `seq 1 254`
do
ping -c 4 $ip$i >/dev/null
if [ $? -eq 0 ]
then
echo "$ip$i is up" >> up.txt
else
echo "$ip$i is down" >> down.txt
fi
done
ip=192.168.0.
for i in `seq 1 254`
do
ping -c 4 $ip$i >/dev/null
if [ $? -eq 0 ]
then
echo "$ip$i is up" >> up.txt
else
echo "$ip$i is down" >> down.txt
fi
done
0
#!bin/bash
#判断网段内哪些IP在线
for i in {1..254}
do
m=`ping -c1 172.28.17.$i |grep 'packets' |awk 'OFS=" [ ]+" {print$6}' |sed 's/%//g'`
if [ $m -eq 0 ]
then
echo "IP 172.28.17.$i is online"
fi
done
#!bin/bash
#判断网段内哪些IP在线
for i in {1..254}
do
m=`ping -c1 172.28.17.$i |grep 'packets' |awk 'OFS=" [ ]+" {print$6}' |sed 's/%//g'`
if [ $m -eq 0 ]
then
echo "IP 172.28.17.$i is online"
fi
done
0
本帖最后由 初秋飞马 于 2016-1-28 12:08 编辑
- #!/bin/bash
- # 2016-01-28 11:54:05
- for i in `seq 1 254`
- do
- ip=192.168.0.$i
- pingmsg=`ping -c 1 $ip |grep Unreachable`
- if [ "$pingmsg" == "" ]
- then
- echo "$ip ""on line..."
- fi
- done
0
本帖最后由 lyhabc 于 2016-1-29 18:44 编辑
#!/bin/bash
##write by 2016-1-29
echo "" > /tmp/err.log
ipn=192.168.0.
for i in $(seq 1 254)
do
ping -c 1 ${ipn}${i} > /dev/null 2>&1
if [ $? -ne 0 ]
then
echo "${ipn}${i} no " >> /tmp/err.log
else
echo "${ipn}${i} yes " >> /tmp/err.log
fi
done
cat /tmp/err.log
192.168.1.1 yes
192.168.1.2 no
192.168.1.3 no
192.168.1.4 no
192.168.1.5 no
192.168.1.6 yes
#!/bin/bash
##write by 2016-1-29
echo "" > /tmp/err.log
ipn=192.168.0.
for i in $(seq 1 254)
do
ping -c 1 ${ipn}${i} > /dev/null 2>&1
if [ $? -ne 0 ]
then
echo "${ipn}${i} no " >> /tmp/err.log
else
echo "${ipn}${i} yes " >> /tmp/err.log
fi
done
cat /tmp/err.log
192.168.1.1 yes
192.168.1.2 no
192.168.1.3 no
192.168.1.4 no
192.168.1.5 no
192.168.1.6 yes
0
#!/bin/bash
for i in `seq 1 254`
do
ping -c 2 -w 10 192.168.1.$i > /root/ip.txt
if [ $? -eq 0 ]
then
echo "192.168.1.$i is online" >> /root/ip.log
fi
done
for i in `seq 1 254`
do
ping -c 2 -w 10 192.168.1.$i > /root/ip.txt
if [ $? -eq 0 ]
then
echo "192.168.1.$i is online" >> /root/ip.log
fi
done
0
#! /bin/bash
for a in `seq 1 254`
do
ip=192.168.0.${a}
ping -c 1 -w 3 ${ip}&>/dev/null
if [ $? eq 0 ]
then
echo "$ip is up"
else
echo "$ip is down!!!"
fi
done
for a in `seq 1 254`
do
ip=192.168.0.${a}
ping -c 1 -w 3 ${ip}&>/dev/null
if [ $? eq 0 ]
then
echo "$ip is up"
else
echo "$ip is down!!!"
fi
done
编辑回复