bash 如何在bash脚本中测试链接是否已启动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24008846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test if link is up in bash script
提问by RobertK
using this script I am trying to detect if there is a network link. I am failing in putting multiple commands in one line (ethtool...). What to do?
使用这个脚本,我试图检测是否有网络链接。我未能将多个命令放在一行中(ethtool ...)。该怎么办?
#!/bin/bash
COMMAND="( /sbin/ethtool eth0 ) | ( /bin/grep \"Link detected: yes\" ) | ( wc -l )"
ONLINE=eval $COMMAND
if $ONLINE; then
echo "Online"
else
echo "Not online"
fi
回答by konsolebox
You simply need
你只需要
if /sbin/ethtool eth0 | grep -q "Link detected: yes"; then
echo "Online"
else
echo "Not online"
fi
Also if you want to encapsulate checking, simply use a function:
另外,如果您想封装检查,只需使用一个函数:
function check_eth {
set -o pipefail # optional.
/sbin/ethtool "" | grep -q "Link detected: yes"
}
if check_eth eth0; then
echo "Online"
else
echo "Not online"
fi
How it works: if
simply interprets a command in front of it and check if the return value of $?
is 0
. grep
returns 0
when it finds a match on its search. And so because of this you don't need to use wc
and compare its output to 1
.
它是如何工作的:if
简单地解释它前面的命令并检查它的返回值是否$?
为0
。当它在搜索中找到匹配项时grep
返回0
。因此,您无需使用wc
其输出并将其与1
.
回答by ormaaj
It appears your script question has been answered. Under Linux I would implement this by reading sysfs directly.
看来您的脚本问题已得到解答。在 Linux 下,我将通过直接读取 sysfs 来实现这一点。
function ifup {
if [[ ! -d /sys/class/net/ ]]; then
printf 'No such interface: %s\n' "" >&2
return 1
else
[[ $(</sys/class/net//operstate) == up ]]
fi
}
if ifup enp7s0; then
echo Online
else
echo 'Not online'
fi
My second choice would probably be ip link
.
我的第二选择可能是ip link
.
# Returns true if iface exists and is up, otherwise false.
function ifup {
typeset output
output=$(ip link show "" up) && [[ -n $output ]]
}
...
回答by lutzhell
At least at my machine (Debian 7.5) only root is allowed to determine link status with ethtool
and it is not installed in all distributions.
至少在我的机器(Debian 7.5)上,只有 root 可以确定链接状态,ethtool
并且它并未安装在所有发行版中。
I can think of three alternatives to ethtool to determine network link status:
我可以想到 ethtool 的三种替代方法来确定网络链接状态:
ifconfig eth0
ip link show
(you have togrep
for your interface and"state UP"
or"state DOWN"
)send one
ping
to some known online host (could also be given as parameter) in your network (ping -c 1 -w 1 ip_address &> /dev/null
,-c
specifies the number of packets to send and-w
is the timeout in seconds)
ifconfig eth0
ip link show
(你必须grep
为你的界面和"state UP"
或"state DOWN"
)将一个发送
ping
到您网络中的某个已知在线主机(也可以作为参数给出)(ping -c 1 -w 1 ip_address &> /dev/null
,-c
指定要发送的数据包数量,-w
超时时间以秒为单位)
回答by Pratham
Looking at your question can you use /sbin/ifconfig eth0
and check if it has inet or inet6 address, or you can also check for the line UP BROADCAST RUNNING MULTICAST
. If the network is down the line will be UP BROADCAST MULTICAST
You can check the man page of ifconfigfor more information.
查看您的问题,您可以使用/sbin/ifconfig eth0
并检查它是否具有 inet 或 inet6 地址,或者您也可以检查 line UP BROADCAST RUNNING MULTICAST
。如果网络出现故障,UP BROADCAST MULTICAST
您可以查看ifconfig的手册页以获取更多信息。