Linux判断服务器网线是否连接

1.获得网卡名

使用命令ip a获取到3个网卡,lo为本地回环,ens33和ens37为2块网卡的网卡名

[20:44:20 root@centos8 ~]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:df:05:a1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.31.157/24 brd 192.168.31.255 scope global dynamic noprefixroute ens33
       valid_lft 1386sec preferred_lft 1386sec
    inet6 fe80::20c:29ff:fedf:5a1/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
3: ens37: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether 00:0c:29:df:05:ab brd ff:ff:ff:ff:ff:ff

2.查看网线是否连接

方法1:

使用mii-tools 网卡名

[20:41:57 root@centos8 ~]#mii-tool ens37
ens37: no link
[20:42:04 root@centos8 ~]#mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok

方法2:

使用ethtool命令,查看最后一行Link detected: 值,yes为已连通,no为未连接

[20:34:30 root@centos8 ~]#ethtool ens33
Settings for ens33:
	Supported ports: [ TP ]
	Supported link modes:   10baseT/Half 10baseT/Full
	                        100baseT/Half 100baseT/Full
	                        1000baseT/Full
	Supported pause frame use: No
	Supports auto-negotiation: Yes
	Supported FEC modes: Not reported
	Advertised link modes:  10baseT/Half 10baseT/Full
	                        100baseT/Half 100baseT/Full
	                        1000baseT/Full
	Advertised pause frame use: No
	Advertised auto-negotiation: Yes
	Advertised FEC modes: Not reported
	Speed: 1000Mb/s
	Duplex: Full
	Auto-negotiation: on
	Port: Twisted Pair
	PHYAD: 0
	Transceiver: internal
	MDI-X: off (auto)
	Supports Wake-on: d
	Wake-on: d
        Current message level: 0x00000007 (7)
                               drv probe link
	Link detected: yes

不通的是

[20:44:11 root@centos8 ~]#ethtool ens37
Settings for ens37:
	Supported ports: [ TP ]
	Supported link modes:   10baseT/Half 10baseT/Full
	                        100baseT/Half 100baseT/Full
	                        1000baseT/Full
	Supported pause frame use: No
	Supports auto-negotiation: Yes
	Supported FEC modes: Not reported
	Advertised link modes:  10baseT/Half 10baseT/Full
	                        100baseT/Half 100baseT/Full
	                        1000baseT/Full
	Advertised pause frame use: No
	Advertised auto-negotiation: Yes
	Advertised FEC modes: Not reported
	Speed: Unknown!
	Duplex: Unknown! (255)
	Auto-negotiation: on
	Port: Twisted Pair
	PHYAD: 0
	Transceiver: internal
	MDI-X: Unknown (auto)
	Supports Wake-on: d
	Wake-on: d
        Current message level: 0x00000007 (7)
                               drv probe link
	Link detected: no

方法3:

其实看网卡名的时候仔细的话也能发现网卡状态的区别
state UP

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:df:05:a1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.31.157/24 brd 192.168.31.255 scope global dynamic noprefixroute ens33
       valid_lft 1386sec preferred_lft 1386sec
    inet6 fe80::20c:29ff:fedf:5a1/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

state DOWN

3: ens37: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
   link/ether 00:0c:29:df:05:ab brd ff:ff:ff:ff:ff:ff
上一篇:KVM网络实验


下一篇:架构的基本概述