[英]How to get an output formatted as “Interface: IP Address” from ifconfig on Mac
[英]Best way to extract MAC address from ifconfig's output?
從ifconfig
的輸出中提取 MAC 地址的最佳方法是什么?
示例輸出:
bash-3.00# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 1F:2E:19:10:3B:52
inet addr:127.0.0.66 Bcast:127.255.255.255 Mask:255.0.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
....
....
我應該使用 cut、 AWK還是其他任何方法,以及一種方法相對於另一種方法的優缺點是什么。
你可以在/sys/class/
下做一只貓
cat /sys/class/net/*/address
專門針對eth0
cat /sys/class/net/eth0/address
我會用:
ifconfig eth0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
-o 將導致 grep 僅打印與表達式匹配的行部分。 [[:xdigit:]]{1,2}
將匹配 1 或 2 個十六進制數字(Solaris 不輸出前導零)。
我喜歡將 /sbin/ip 用於此類任務,因為解析起來要容易得多:
$ ip link show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:30:21:48 brd ff:ff:ff:ff:ff:ff
您可以使用 awk 從該輸出中輕松獲取 mac 地址:
$ ip link show eth0 | awk '/ether/ {print $2}'
00:0c:29:30:21:48
如果你想多花點力氣,解析出更多的數據,我建議在 ip 命令中使用 -online 參數,這會讓你把每一行都當作一個新設備:
$ ip -o link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue \ link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000\ link/ether 00:0c:29:30:21:48 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000\ link/ether 00:0c:29:30:21:52 brd ff:ff:ff:ff:ff:ff
4: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 100\ link/[65534]
5: sit0: <NOARP> mtu 1480 qdisc noop \ link/sit 0.0.0.0 brd 0.0.0.0
不確定是否真的有任何優勢,但您可以簡單地使用 awk:
ifconfig eth0 | awk '/HWaddr/ {print $5}'
由於 OP 的示例引用了 Bash,因此這里有一種無需使用其他工具即可提取 HWaddr 等字段的方法:
x=$(ifconfig eth0) && x=${x#*HWaddr } && echo ${x%% *}
在第一步中,這將 ifconfig 的輸出分配給 x。 第二步刪除“HWaddr”之前的所有內容。 在最后一步中,“”(MAC 后面的空格)之后的所有內容都將被刪除。
參考: http : //www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion
對於 Ubuntu/Debian
ifconfig | grep HW | awk '{print $5}'
對於 Rhat 或 CentOs 嘗試
ip add | grep link/ether | awk '{print $2}'
我更喜歡這里描述的方法(稍作修改): http : //www.askdavetaylor.com/how_do_i_figure_out_my_ip_address_on_a_mac.html
ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d " " -f2
然后您可以將其別名為一個簡短的“myip”命令以供將來使用:
echo "alias myip=\"ifconfig | grep 'inet ' | grep -v 127.0.0.1 | cut -d ' ' -f2\"" >> ~/.bash_profile
在終端的 Ubuntu 14.04 上
ifconfig | grep HW
這個怎么樣:
ifconfig eth0 | grep -Eo ..\(\:..\){5}
或更具體地說
ifconfig eth0 | grep -Eo [:0-9A-F:]{2}\(\:[:0-9A-F:]{2}\){5}
還有一個簡單的
ifconfig eth0 | head -n1 | tr -s ' ' | cut -d' ' -f5`
ifconfig en1 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
注意:在 OS X eth0 上可能不起作用。 使用 p2p0:
ifconfig p2p0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
這在 Mac OS X 上對我有用:
ifconfig en0 | grep -Eo ..\(\:..\){5}
也是如此:
ifconfig en0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
兩者都是上述示例的變體。
ifconfig | grep -i hwaddr | cut -d ' ' -f11
又快又好:
ifconfig eth0 | grep HWaddr | cut -d ' ' -f 11
我需要獲取活動適配器的 MAC 地址,因此最終使用了此命令。
ifconfig -a | awk '/^[a-z]/ { iface=$1; mac=$NF; next } /inet addr:/ { print mac }' | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
希望能幫助到你。
ifconfig en0 | grep ether - 用於有線 mac 地址
ifconfig en1 | grep ether - 用於無線 mac 地址
這對我有用
ifconfig eth0 | grep -o -E ..:..:..:..:..:..
而不是eth0
你可以寫你需要的接口它的 mac 地址
ifconfig 的輸出:
$ifconfig
eth0 Link encap:Ethernet HWaddr 00:1b:fc:72:84:12
inet addr:172.16.1.13 Bcast:172.16.1.255 Mask:255.255.255.0
inet6 addr: fe80::21b:fcff:fe72:8412/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:638661 errors:0 dropped:20 overruns:0 frame:0
TX packets:93858 errors:0 dropped:0 overruns:0 carrier:2
collisions:0 txqueuelen:1000
RX bytes:101655955 (101.6 MB) TX bytes:42802760 (42.8 MB)
Memory:dffc0000-e0000000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:3796 errors:0 dropped:0 overruns:0 frame:0
TX packets:3796 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:517624 (517.6 KB) TX bytes:517624 (517.6 KB)
提取MAC地址的最佳方法是:
ifconfig | sed '1,1!d' | sed 's/.*HWaddr //' | sed 's/\ .*//' | sed -e 's/:/-/g' > mac_address
用:
ifconfig eth0 | grep HWaddr
或者
ifconfig eth0 |grep HWaddr
這將只提取 MAC 地址而沒有其他任何內容。
您可以將您的 MAC 地址更改為任何您想要的:
ifconfig eth0 down,
ifconfig eth0 hw ether (new MAC address),
ifconfig eth0 up
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.