使用ifconfigl命令能很方便的查看網卡與網線是否連通:linux
# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:25:35:68:CC:D6
inet addr:192.168.1.168 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::215:c5ff:fe18:ccd6/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:130722 errors:0 dropped:0 overruns:0 frame:0
TX packets:112560 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:104371099 (99.5 MiB) TX bytes:20518584 (19.5 MiB)
Interrupt:16 ios
其中的RUNNING就表示網卡與網線正常連接,拔掉網線再運行此命令就會發現RUNNING不在了。shell
NTP-SERVER:/#mii-tool
eth0: negotiated 100baseTx-FD, link ok
eth1: no link
eth2: no link
eth3: no linksocket
[NTP-Fedora20 ~]#mii-tool p32p1
p32p1: negotiated 100baseTx-FD, link ok
[NTP-Fedora20 ~]#ethtool p32p1
Settings for p32p1:
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
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes
Speed: 100Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 1
Transceiver: internal
Auto-negotiation: on
MDI-X: on (auto)
Supports Wake-on: pumbg
Wake-on: g
Current message level: 0x00000007 (7)
drv probe link
Link detected: yes
[NTP-Fedora20 ~]#ui
方法一:
this使用ioctl向socket發送
SIOCETHTOOL
命令字
方法二: 使用ifconfigl命令能很方便的查看網卡與網線是否連通:
spa
# ifconfig eth0
.net
eth0 Link encap:Ethernet HWaddr 00:25:35:68:CC:D6
inet addr:192.168.1.168 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::215:c5ff:fe18:ccd6/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:130722 errors:0 dropped:0 overruns:0 frame:0
TX packets:112560 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:104371099 (99.5 MiB) TX bytes:20518584 (19.5 MiB)
Interrupt:16
其中的RUNNING就表示網卡與網線正常連接,拔掉網線再運行此命令就會發現RUNNING不在了。
code
linux系統提供了popen/pclose進程管道讓C和shell很方便的交互,下面C代碼結合shell命令檢測網卡與網線連通情況: