制作 bash 脚本来检查连接并在必要时更改连接。帮我改进一下?
我的连接不稳定,但我有一个备份连接。我制作了一些 bash 脚本来检查连接并在当前连接已失效时更改连接。请帮助我改进它们。
这些脚本几乎可以工作,除了没有等待足够长的时间来接收IP(它在until循环中循环到下一步的速度太快)。这
#!/bin/bash
# Invoke this script with paths to your connection specific scripts, for example
# ./gotnet.sh ./connection.sh ./connection2.sh
until [ -z "$1" ] # Try different connections until we are online...
do
if eval "ping -c 1 google.com"
then
echo "we are online!" && break
else
$1 # Runs (next) connection-script.
echo
fi
shift
done
echo # Extra line feed.
exit 0
是从属脚本的示例:
#!/bin/bash
ifconfig wlan0 down
ifconfig wlan0 up
iwconfig wlan0 key 1234567890
iwconfig wlan0 essid example
sleep 1
dhclient -1 -nw wlan0
sleep 3
exit 0
My connection is flaky, however I have a backup one. I made some bash script to check for connectivity and change connection if the present one is dead. Please help me improve them.
The scripts almost works, except for not waiting long enough to receive an IP (it cycles to next step in the until loop too quick). Here goes:
#!/bin/bash
# Invoke this script with paths to your connection specific scripts, for example
# ./gotnet.sh ./connection.sh ./connection2.sh
until [ -z "$1" ] # Try different connections until we are online...
do
if eval "ping -c 1 google.com"
then
echo "we are online!" && break
else
$1 # Runs (next) connection-script.
echo
fi
shift
done
echo # Extra line feed.
exit 0
And here is an example of the slave scripts:
#!/bin/bash
ifconfig wlan0 down
ifconfig wlan0 up
iwconfig wlan0 key 1234567890
iwconfig wlan0 essid example
sleep 1
dhclient -1 -nw wlan0
sleep 3
exit 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试在某处使用 ConnectTimeout ${timeout} 。
Trying using ConnectTimeout ${timeout} somewhere.
一种方法是:
根据您的喜好调整睡眠时间和 ping 计数。该脚本永远不会退出,因此您很可能希望使用以下命令运行它:
./connection_daemon.sh 2>&1 > /dev/null &否认
Here's one way to do it:
Adjust the sleep times and ping count to your preference. This script never exits so you would most likely want to run it with the following command:
./connection_daemon.sh 2>&1 > /dev/null & disown
您是否尝试过从
dhclient
命令中省略-nw
选项?另外,如果不需要,请从您的
eval
和引号中删除它们。这样做:Have you tried omitting the
-nw
option from thedhclient
command?Also, remove the
eval
and quotes from yourif
they aren't necessary. Do it like this: