bash 有没有更好的方法从arp表中获取mac地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13778035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:58:08  来源:igfitidea点击:

Is there any better way to get mac address from arp table?

linuxalgorithmbashawkarp

提问by ibrahim

I want to get a mac address from arp table by using ip address. Currently I am using this command

我想通过使用 ip 地址从 arp 表中获取 mac 地址。目前我正在使用这个命令

arp -a $ipAddress | awk '{print }'

This command prints what I want. But I am not comfortable with it and I wonder if there is any built-in way or more stable way to do this.

此命令打印我想要的内容。但我对此并不满意,我想知道是否有任何内置方式或更稳定的方式来做到这一点。

回答by gniourf_gniourf

You can parse the /proc/net/arpfile using awk:

您可以/proc/net/arp使用awk以下方法解析文件:

awk "/^${ipAddress//./\.}\>/"' { print  }' /proc/net/arp

but I'm not sure it's simpler (it saves one fork and a subshell, though).

但我不确定它是否更简单(不过,它节省了一个叉子和一个子外壳)。

If you want a 100% bash solution:

如果您想要 100% bash 解决方案:

while read ip _ _ mac _; do
    [[ "$ip" == "$ipAddress" ]] && break
done < /proc/net/arp
echo "$mac"

回答by paxdiablo

Well, you could write a program (such as in C) to actually use the ARP protocol (yes, I knowthat's redundant, like ATM machine or PIN number) itself to get you the information but that's likely to be a lot harder than a simple pipeline.

好吧,您可以编写一个程序(例如在 C 中)来实际使用 ARP 协议(​​是的,我知道这是多余的,例如 ATM 机或 PIN 码)本身来获取信息,但这可能比简单的管道。

Perhaps you should examine your comfort level a little more critically, since it's likely to cause you some unnecessary effort :-)

也许您应该更严格地检查您的舒适度,因为它可能会给您带来一些不必要的努力:-)

The manpage for the Linux ARP kernel modulelists several methods for manipulating or reading the ARP tabes, ioctlprobably being the easiest.

Linux ARP 内核模块联机帮助页列出了几种操作或读取 ARP 表的方法,ioctl可能是最简单的方法。

回答by Lars Noschinski

The output of arp -ais locale dependent (i.e. it changes with your system language). So it might be a good idea to at least force it to the default locale:

的输出arp -a取决于区域设置(即它会随着您的系统语言而变化)。因此,至少将其强制为默认语言环境可能是个好主意:

LC_ALL=C arp -a $ipAddress | awk '{print }'

However, I share your fear that the output of arp -ais not meant to be parsed. If your program is restricted to linux system, another option would be to parse the file /proc/net/arp. This file is exported by the kernel and is what arp itself parses to get its information. The format of this file is described in the manpage proc(5), see man 5 proc. This can be easily done with awk:

但是,我和您一样担心 的输出arp -a不应该被解析。如果您的程序仅限于 linux 系统,另一种选择是解析文件/proc/net/arp. 该文件由内核导出,是 arp 本身解析以获取其信息的文件。此文件的格式在联机帮助页 proc(5) 中进行了描述,请参见man 5 proc。这可以使用 awk 轻松完成:

awk '==IPADDRESS {print }' /proc/net/arp

回答by ddoxey

Here's an awk + sed solution which doesn't assume the column number is always 4.

这是一个 awk + ​​sed 解决方案,它不假设列号始终为 4。

#!/bin/bash

cat /proc/net/arp |\
    # remove space from column headers
    sed 's/\([^ ]\)[ ]\([^ ]\)/_/g' |\
    # find HW_address column number and/or print that column
    awk '{
        if ( !column ) {
            for (i = 1; i <= NF; i++ ) {
                if ( $i ~ /HW_address/ ) { column=i }
            };
            print $column
         }
         else {
            print $column
         }
    }'

There are still fragile assumptions here, such as the column name being "HW address".

这里仍然存在脆弱的假设,例如列名是“HW 地址”。

回答by koola

Update, removed PIPE

更新、移除 PIPE

sed -nr 's/^'${ipAddress//./\.}'.*(([0-9A-Za-z]{2}:){5}[0-9A-Za-z]{2}).*$//p' /proc/net/arp

Solution for non-fixed column;

非固定柱解决方案;

arp -a $ipAddress | sed -n 's/^.*\(\([0-9A-Z]\{2\}:\)\{5\}[0-9A-Z]\{2\}\).*$//p'

Explanation

解释

  • ^.*- Match start of string ^followed by any character .*.
  • [0-9A-Z]\{2\}:- Match any character of numeric alpha-numeric twice followed by colon.
  • \([0-9A-Z]\{2\}:\)\{5\}- Match the pattern between the ( )five times.
  • [0-9A-Z]\{2\}- Match any character of numeric alpha-numeric twice.
  • .*$- Match any characters zero or more times .*until end of string $.
  • \1/p- Return capture pattern 1 / pprint the match.
  • ^.*- 匹配字符串开头,^后跟任何字符.*
  • [0-9A-Z]\{2\}:- 匹配任何数字字母数字字符两次后跟冒号。
  • \([0-9A-Z]\{2\}:\)\{5\}- 匹配( )五次之间的模式。
  • [0-9A-Z]\{2\}- 匹配任何数字字母数字字符两次。
  • .*$- 匹配任何字符零次或多次,.*直到字符串结束$
  • \1/p- 返回捕获模式 1 /p打印匹配项。

回答by Mcgrog

You can use this one for scripting:

您可以使用此脚本编写脚本:

awk ' ~/[[:digit:]]/ {print }' /proc/net/arp

what it do:

它的作用:

1) read /proc/net/arp (stanart arp output)

1) 读取/proc/net/arp (stanart arp 输出)

2) searchig for stings with [0-9]

2) 用 [0-9] 搜索蜇伤

3) get the 4rd "column" with mac adresses

3)使用mac地址获取第四个“列”

Enjoy!

享受!