Ip 如何在Linux中ping MAC地址

Ip 如何在Linux中ping MAC地址,ip,ping,mac-address,arp,Ip,Ping,Mac Address,Arp,我想ping一个已知的MAC地址,我尝试使用nmap: sudo nmap -sP 192.168.15.1/24 | grep 20:64:32:3F:B1:A9 但在这种情况下,它会ping所有255个IP地址(从192.168.15.1到192.168.15.255),直到获得我的MAC地址,这需要很长时间,大约4秒 有什么想法吗?提高速度的唯一方法是测试mac地址是否已经进入您的arp表 #!/bin/bash # extract ip from local arp table i

我想ping一个已知的MAC地址,我尝试使用nmap:

sudo nmap -sP 192.168.15.1/24 | grep  20:64:32:3F:B1:A9
但在这种情况下,它会ping所有255个IP地址(从192.168.15.1到192.168.15.255),直到获得我的MAC地址,这需要很长时间,大约4秒


有什么想法吗?

提高速度的唯一方法是测试mac地址是否已经进入您的arp表

#!/bin/bash

# extract ip from local arp table
ip=$(arp | grep 20:64:32:3F:B1:A9 | awk ' { print $1 } ')

# found an ip tied to the mac address?
if [ ! -z $ip ]; then

    # if found, do you want to ping it?
    ping $ip
else
    echo "Not found into local arp table. Trying another way..."

    # wanna try your nmap strategy?
    # sudo nmap -sP 192.168.15.1/24 | grep  20:64:32:3F:B1:A9
fi;

你不能ping一个MAC地址。您只能ping一个IP地址,所以您要做的是找出哪个IP地址属于某个MAC地址,然后ping该IP。ARP用于查找具有特定IP地址的机器的MAC地址,但实际上不能反过来(从技术上讲,存在一种称为ARP的协议,但它从未在典型的操作系统中使用)。一旦找到MAC地址,它将被保存在ARP缓存中,这样您就不必在几分钟内再次查找它,但这不是找到MAC的可靠方法,因为条目不会在缓存中停留很长时间。你已经知道了如何创建一个静态条目,但是如果你要将192.168.15.196硬编码到那个MAC地址,为什么不直接ping 192.168.15.196(这就是你要做的一切)?

nmap有-t选项来加速类似的事情-t5是最快的


您还可以尝试--min并行选项

将上述好答案组合成一个脚本: (用法:
macping aa:bb:cc:dd:ee:ff

扩展:按mac地址维护网络设备列表,并显示每个设备的联机/脱机状态。
用途包括:

  • 监视服务器的状态
  • 检查您的internet连接已启动
  • 检查特定设备是否已连接到wifi
  • 检查你的智能电视真的关机了
每个设备名称在线时显示为绿色,离线时显示为红色。
当设备状态更改时,将显示桌面通知

在LinuxMint下测试,应该可以在其他发行版上使用

#!/bin/bash

#Create associated array's 
declare -A devicelist #device name: mac address
declare -A statuslist #device name: online status

devicelist[Server01]=aa:bb:cc:dd:ee:01
devicelist[Server02]=aa:bb:cc:dd:ee:02
devicelist[MyPhone] =aa:bb:cc:dd:ee:03
devicelist[SmartTV] =aa:bb:cc:dd:ee:04

#Colour Constants
BRed='\033[1;31m'  
BGreen='\033[1;32m' 
Reset='\033[m'

function mactoip(){
  echo $(arp -n | grep -i $mac | awk ' { print $1 }')
}

while [ true ]; do
    clear
    arp_cache_rebuilt=no
    for devicename in ${!devicelist[@]}; do
        status=OFFLINE
        mac=${devicelist[${devicename}]}
        ip=$( mactoip $mac )
        if [ -z $ip ] && [ $arp_cache_rebuilt = "no" ]; then
            #we need to rebuild the arp cache...
            nmap -sn -T4 192.168.1.0/24 >& /dev/null
            ip=$( mactoip $mac )
            arp_cache_rebuilt=yes
        fi;

        if [ ! -z $ip ]; then
            ping $ip -n -q -c 2 -i 0.2 -w 1 >& /dev/null
            if [ $? -eq 0 ]; then status=ONLINE; fi
        fi;
        #if device's previous status not yet recorded, then set it now.
        if [ ! ${statuslist[${devicename}]+_} ]; then statuslist[${devicename}]=$status; fi

        if [ $status = "ONLINE" ]; then colour=$BGreen; else colour=$BRed; fi;
        echo -e ${colour}${devicename}${Reset} - $ip
        if [ ${statuslist[${devicename}]} != $status ]; then
          notify-send -i ac-adapter -u critical -t 1000 $status "$devicename"
        fi;
        statuslist[$devicename]=$status
    done
    echo -
    sleep 5
done

这是另一个更简单的答案

ping $(arp-scan --localnet | grep 80:1f:02:fa:90:b7  | awk ' { printf $1 } ')
请注意,mac地址必须使用小写字母


arp扫描似乎比arp运行得快得多。

考虑到回复,代码不起作用,它给了我:用法:ping[-LRUbdfnqrvVaAD][c count][i interval][w deadline][p pattern][s packetsize][t ttl][i interface][M pmtudisc hint][M mark s sndbuf t tstamp options][Q tos][hop1 destinationOk,这是一个很好的解决方案和它的工作,但它总是给我“在本地arp表中找不到。尝试另一种方法…”甚至我使用了另一个mac地址,而这两个地址都在本地网络中找到!这意味着mac地址不在arp表中。如何将其插入表中?我使用了sudo nmap-sP 192.168.15.1/24 | grep 20:64:32:3F:B1:A9,但它仍然不在arp表中?我通过sudo arp添加了mac地址-s 192.168.15.196 20:64:32:3F:B1:A9,我不知道这是否正确。我认为应该自动添加它,因为mac地址在本地网络中找到,但无论如何,您的脚本现在工作得很好。感谢您提供的解决方案。使用“arp-n”将使速度大大加快,从17.0秒提高到0.003秒。这对于监控谁正在连接我的网络非常有用。现在。。。我通过VPN连接到我公司的网络:有没有办法让这个
arp
magic在远程网络上工作?我试过一点,但到目前为止还没有成功。因为我经常在这里和那里复制和粘贴终端内容,在传输过程中,颜色往往会褪色。因此,我稍微修改了您的脚本以处理色盲和文本复制粘贴:我只是将颜色的定义替换为:`breed='\033[1;31m[OFF]'BGreen='\033[1;32m[ON]`我还更改了循环开始时的
clear
,用于
echo“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
我想只是口味问题……nmap需要
--发送ip
来更新系统ARP表。
ping $(arp-scan --localnet | grep 80:1f:02:fa:90:b7  | awk ' { printf $1 } ')