扫描网络的Linux脚本

时间:2015-05-28 07:49:09

标签: linux bash

我有一个问题,我想运行一个扫描我的网络的脚本,我可以看到设备在我的网络中。我想在脚本中粘贴一些参数

当我运行脚本时:./ ping.sh --xx-YY 20 50

输出:

192.168.0.20 is down 

192.168.0.21 is up 

..................................

192.168.0.50 is up

参数-t,以ex的最后一位数表示200。 ./ping.sh -t 20 输出:192.168.0.220上架

但我的问题是de argument --up en -mac 我想在运行脚本./ping.sh --up时,它只显示那些计算机 当我运行脚本时,参数 - mac也是一个问题我想在计算机ping之后显示ip地址和de mac地址

这是我的剧本。

#!/bin/bash

while [[ -n "$@" ]]
do
    case "$1" in

    -h|--help)
    Extension=1
    shift
    ;;

    -XX-YY)
    Extension=2
    shift
    min=$1
    shift
    max=$1
    shift

    ;;

    --up)
    Extension=3
    shift
    end=$1
    shift
    ;;

    -t)
    Extension=4
    shift
    count=$(($1+200))
    shift
    ;;
    esac
done

if [ "$Extension" -eq 1 ] ; then

    echo "dit is de help"

fi


if [ "$Extension" -eq 2 ] ; then

for ((n=$min ; n<=$max ; n+=1))
do
ip=192.168.0.$n
if ping -c 1 -w 1 ${ip} > /dev/null 2> /dev/null >> logping.txt; then
echo "${ip} is up"
else
echo "${ip} is down"
fi
done


fi

if [ "$Extension" -eq 3 ] ; then


                ip=192.168.0.$end
                if ping -c 1 -w 1 ${ip} > /dev/null 2> /dev/null >> /logping.txt; then
                        echo "${ip} is up"

                fi

fi

if [ "$Extension" -eq 4 ] ; then

ip=192.168.0.$count
if ping -c 1 -t 1 ${ip} > /dev/null 2> /dev/null >> logping.txt; then
echo "${ip} is up"
else
echo "${ip} is down"
fi
fi

1 个答案:

答案 0 :(得分:1)

不要重新发明轮子 - nmap做得非常好。 <怎么样

nmap -sP -n -r 192.168.1.0/24 | grep "is up"

根据您的需要改变IP范围。如果省略grep,则Ping扫描将显示MAC地址(假设您与接口位于同一链接上),例如:

...
Host 192.168.1.249 is up (0.00011s latency).
MAC Address: 00:08:9B:BF:A9:FC (ICP Electronics)
Host 192.168.1.251 is up (0.0035s latency).
MAC Address: 00:27:0C:5A:5B:A8 (Unknown)
...

干杯,