Jea*_*ach 138 linux connection networking detection
在Linux环境中,我需要检测RJ45连接器到其插槽的物理连接或断开状态.最好只使用BASH脚本.
以下在其他网站上提出的解决方案不适用于此目的:
是不是有一些状态可以在/ proc文件系统中使用(其他一切都在那里)?
Linux世界是如何拥有自己的Windows泡泡版本,从图标托盘中弹出,表明您刚刚拔掉了网线?
Kent Fredric和lothar,你的两个答案都满足了我的需求......非常感谢!哪一个我会用...我还是不知道.
我想我不能把你们两个都当作正确的答案吗?对我来说,我选择一个可能是公平的.我猜是翻硬币吗?再次,谢谢!
Ken*_*ric 214
你想看看中的节点
/sys/class/net/
我试验过我的:
电线插入:
eth0/carrier:1
eth0/operstate:unknown
Run Code Online (Sandbox Code Playgroud)
电线已删除:
eth0/carrier:0
eth0/operstate:down
Run Code Online (Sandbox Code Playgroud)
电线又插入:
eth0/carrier:1
eth0/operstate:up
Run Code Online (Sandbox Code Playgroud)
Side Trick:轻松获取所有属性:
grep "" eth0/*
Run Code Online (Sandbox Code Playgroud)
这形成了很好的key:value
对列表 .
lot*_*har 82
你可以使用ethtool:
$ sudo ethtool eth0
Settings for eth0:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised auto-negotiation: Yes
Speed: 1000Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 0
Transceiver: internal
Auto-negotiation: on
Supports Wake-on: umbg
Wake-on: g
Current message level: 0x00000007 (7)
Link detected: yes
Run Code Online (Sandbox Code Playgroud)
要仅获取链接状态,您可以使用grep:
$ sudo ethtool eth0 | grep Link
Link detected: yes
Run Code Online (Sandbox Code Playgroud)
Pet*_*ing 26
使用'ip monitor'获取REAL TIME链接状态更改.
小智 17
cat /sys/class/net/ethX
是迄今为止最简单的方法.
接口必须是up,否则你将得到一个无效的参数错误.
首先:
ifconfig ethX up
Run Code Online (Sandbox Code Playgroud)
然后:
cat /sys/class/net/ethX
Run Code Online (Sandbox Code Playgroud)
在较低级别,可以使用rtnetlink套接字捕获这些事件,而不进行任何轮询.旁注:如果使用rtnetlink,则必须与udev一起使用,否则当udev重命名新的网络接口时,您的程序可能会感到困惑.
使用shell脚本进行网络配置的问题在于,shell脚本对于事件处理非常糟糕(例如插入和拔出网络电缆).如果您需要更强大的功能,请查看我的NCD编程语言,这是一种专为网络配置而设计的编程语言.
例如,一个简单的NCD脚本将打印"cable in"和"cable out"到stdout(假设接口已经启动):
process foo {
# Wait for device to appear and be configured by udev.
net.backend.waitdevice("eth0");
# Wait for cable to be plugged in.
net.backend.waitlink("eth0");
# Print "cable in" when we reach this point, and "cable out"
# when we regress.
println("cable in"); # or pop_bubble("Network cable in.");
rprintln("cable out"); # or rpop_bubble("Network cable out!");
# just joking, there's no pop_bubble() in NCD yet :)
}
Run Code Online (Sandbox Code Playgroud)
(内部net.backend.waitlink()
使用rtnetlink,并net.backend.waitdevice()
使用udev)
NCD的想法是你专门用它来配置网络,所以通常,配置命令介于两者之间,例如:
process foo {
# Wait for device to appear and be configured by udev.
net.backend.waitdevice("eth0");
# Set device up.
net.up("eth0");
# Wait for cable to be plugged in.
net.backend.waitlink("eth0");
# Add IP address to device.
net.ipv4.addr("eth0", "192.168.1.61", "24");
}
Run Code Online (Sandbox Code Playgroud)
需要注意的重要部分是允许执行倒退 ; 例如,在第二个示例中,如果拉出电缆,IP地址将自动删除.
我使用此命令来检查电线是否已连接:
cd /sys/class/net/
grep "" eth0/operstate
Run Code Online (Sandbox Code Playgroud)
如果结果会上升或下降。有时显示未知,则需要检查
eth0/carrier
Run Code Online (Sandbox Code Playgroud)
显示0或1