Shell脚本 – 查看网络接口信息
本文介绍如何是用shell脚本查看网络接口的ip地址、MAC地址、网络速率等信息。
系统环境
Centos7
1)检查可用的网络接口
使用
ip
和awk
命令,过滤出状态为UP的网络接口。[root
@localhost ~]# ip ad|awk
'/state UP/ {print $2}'ens33:
ens38:
2)查看网络接口的IP地址
使用下面命令过滤出每个接口的ip地址:
[root
@localhost ~]# ip -o addr |awk
'/inet/{print $2,$4}'lo
127.0.0.1/
8lo ::
1/
128ens33
192.168.43.138/
24ens33 fe80::
214e:
53b4:
43f6:
5495/
64ens38
172.25.254.130/
24ens38 fe80::c2ff:
9dbc:
76be:
6dd9/
64或者只查看IPv4地址:
[root
@localhost ~]# ip addr | grep inet|grep -v
'inet6'|awk
'{print $NF, $2}'lo
127.0.0.1/
8ens33
192.168.43.138/
24ens38
172.25.254.130/
243)查看网卡的MAC地址
如果只想查看网络接口名称和相应的MAC地址,请使用以下命令。检查特定的网络接口的MAC地址:
[root
@localhost ~]# ip link show ens33 | awk
'/link/{print $2}'00
:
0c:
29:
99:ee:d9
查看所有网络接口的MAC地址,可以写一个脚本来实现:
[root
@localhost ~]# cat mac-address.sh
#!/bin/bash
ip addr |awk
'/state UP/{print $2}' | sed
's/://' |
while read output
do
echo $output:
ethtool -P $output
done
查看一下运行结果:
4)查看网络接口的速度
如果要在Linux上检查网络接口端口速度,可以使用ethtool工具。下面是查看特定网络接口的速度:
[root@localhost ~]# ethtool ens33|grep "Speed:"
Speed: 1000Mb/s
查看所有接口的网络速度,可以写一个脚本来实现:
[root
@localhost ~]# cat port-speed.sh
#!/bin/bash
ip addr |awk
'/state UP/{print $2}' | sed
's/://' |
while read output
do
echo $output:
ethtool $output |grep
"Speed:"done
查看一下运行结果:
5)查看网络接口信息的Shell脚本
下面这个脚本,我们来实现查看主机名、IPv4、IPv6、MAC地址、网络接口速度信息:
[root
@localhost ~]# cat nic-info.sh
#!/bin/bash
hostname
echo
"-------------"for
iname in $(ip addr |awk
'/state UP/{print $2}')
do
echo
"$iname"ip addr show $iname | grep inet | awk
'{printf "%s:\t%s\n",$1,$2}'ip link show $iname | grep link | awk
'{printf "MAC:\t%s\n",$2}'ethtool ens33 | awk
'/Speed/{printf "%s\t%s\n",$1,$2}'done
官方站点:www.linuxprobe.com
Linux命令大全:www.linuxcool.com
刘遄老师QQ:5604922
Linux技术交流群:193666693
(新群,火热加群中……)
想要学习Linux系统的读者可以点击"阅读原文"按钮来了解书籍《Linux就该这么学》,同时也非常适合专业的运维人员阅读,成为辅助您工作的高价值工具书!
阅读原文 最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。