Shell脚本 – 查看网络接口信息

系统 Linux
本文介绍如何是用shell脚本查看网络接口的ip地址、MAC地址、网络速率等信息。

[[404873]]

本文介绍如何是用shell脚本查看网络接口的ip地址、MAC地址、网络速率等信息。

系统环境

Centos7

1)检查可用的网络接口

使用ip和awk命令,过滤出状态为UP的网络接口。

  1. [root@localhost ~]# ip ad|awk '/state UP/ {print $2}' 
  2. ens33: 
  3. ens38: 

2)查看网络接口的IP地址

使用下面命令过滤出每个接口的ip地址:

  1. [root@localhost ~]# ip -o addr |awk '/inet/{print $2,$4}' 
  2. lo 127.0.0.1/8 
  3. lo ::1/128 
  4. ens33 192.168.43.138/24 
  5. ens33 fe80::214e:53b4:43f6:5495/64 
  6. ens38 172.25.254.130/24 
  7. ens38 fe80::c2ff:9dbc:76be:6dd9/64 
  8. 或者只查看IPv4地址: 
  9. [root@localhost ~]# ip addr | grep inet|grep -v 'inet6'|awk '{print $NF, $2}' 
  10. lo 127.0.0.1/8 
  11. ens33 192.168.43.138/24 
  12. ens38 172.25.254.130/24 

3)查看网卡的MAC地址

如果只想查看网络接口名称和相应的MAC地址,请使用以下命令。检查特定的网络接口的MAC地址:

  1. [root@localhost ~]# ip link show ens33 | awk '/link/{print $2}' 
  2. 00:0c:29:99:ee:d9 

查看所有网络接口的MAC地址,可以写一个脚本来实现:

  1. [root@localhost ~]# cat mac-address.sh  
  2. #!/bin/bash 
  3. ip addr |awk '/state UP/{print $2}' | sed 's/://' | while read output 
  4. do 
  5. echo $output
  6. ethtool -P $output 
  7. done 

查看一下运行结果:

4)查看网络接口的速度

如果要在Linux上检查网络接口端口速度,可以使用ethtool工具。下面是查看特定网络接口的速度:

  1. [root@localhost ~]# ethtool ens33|grep "Speed:" 
  2. Speed: 1000Mb/s 

查看所有接口的网络速度,可以写一个脚本来实现:

  1. [root@localhost ~]# cat port-speed.sh  
  2. #!/bin/bash 
  3. ip addr |awk '/state UP/{print $2}' | sed 's/://' | while read output 
  4. do 
  5. echo $output
  6. ethtool $output |grep "Speed:" 
  7. done 

查看一下运行结果:

5)查看网络接口信息的Shell脚本

下面这个脚本,我们来实现查看主机名、IPv4、IPv6、MAC地址、网络接口速度信息:

  1. [root@localhost ~]# cat nic-info.sh  
  2. #!/bin/bash 
  3. hostname 
  4. echo "-------------" 
  5. for iname in $(ip addr |awk '/state UP/{print $2}'
  6. do 
  7. echo "$iname" 
  8. ip addr show $iname | grep inet | awk '{printf "%s:\t%s\n",$1,$2}' 
  9. ip link show $iname | grep link | awk '{printf "MAC:\t%s\n",$2}' 
  10. ethtool ens33 | awk '/Speed/{printf "%s\t%s\n",$1,$2}' 
  11. done 

本文转载自微信公众号「Linux就该这么学」,可以通过以下二维码关注。转载本文请联系Linux就该这么学公众号。

 

责任编辑:武晓燕 来源: Linux就该这么学
相关推荐

2011-04-01 11:31:22

OSPF

2021-03-29 10:09:22

Windows服务器端口

2019-12-05 09:00:27

BashShellLinux

2010-06-17 14:45:50

RS-232C协议

2019-08-09 13:50:08

shellLinux

2019-03-20 10:00:34

Linux网络接口命令

2012-04-26 14:02:58

ibmdw

2021-07-02 06:54:44

Shell脚本 Linux

2022-06-21 09:26:21

Shell脚本JavaScript

2011-09-27 13:52:41

2020-06-17 10:42:54

shellshell脚本Linux

2011-06-28 10:11:05

Qt Qt 4.1.0 注册表

2020-06-16 08:44:23

Shell服务器

2015-08-10 14:42:40

Explain SheShell 命令

2009-11-18 13:52:30

PHP shell脚本

2020-11-02 08:23:36

shell脚本Linux

2023-07-31 08:45:10

Shell脚本

2014-08-08 16:17:49

shell脚本linux

2009-12-01 09:13:51

shell脚本linux

2019-11-13 08:31:43

Oracle数据库脚本
点赞
收藏

51CTO技术栈公众号