使得bash脚本来检查连接,必要时更换连接。帮助我提高了吗? [英] Making bash script to check connectivity and change connection if necessary. Help me improve it?
问题描述
我的连接是片状的,但是我有备份的。我做了一些bash脚本来检查连接和改变连接,如果present一个是死了。请帮我改善。
脚本几乎工作,但不等待足够长的时间来接收IP(它循环到下一个步骤中,直至循环太快)。这里所说:
#!/斌/庆典
#调用这个脚本路径到你的连接特定的脚本,例如
#./gotnet.sh ./connection.sh ./connection2.sh直到[-z$ 1]#尝试不同的连接,直到我们的在线...
做
如果EVAL平-c 1 google.com
然后
回声我们是在网上! &功放;&安培;打破
其他
$ 1号运行(下一个)连接的脚本。
回声
科幻
转移
DONE 回声#额外的换行符。
退出0
这里是从脚本的例子:
#!/斌/庆典
使用ifconfig为wlan0下来
使用ifconfig为wlan0了
iwconfig的为wlan0关键1234567890
iwconfig的为wlan0例如ESSID
睡眠1
dhclient的-1 -nw为wlan0
睡眠3
退出0
下面是做到这一点的一种方法:
#!/斌/庆典
而真实的;做
如果! ['平-c 1 google.com;回声$`?];然后#如果平退出非零...
./connection_script1.sh #run第一个脚本
睡10 #give它几秒钟即可完成
科幻
如果! ['平-c 1 google.com;回声$`?];然后#如果平* *仍然非零退出...
./connection_script2.sh #run第二个脚本
睡10 #give它几秒钟即可完成
科幻
睡五分钟后再次#检查300
DONE
调整睡眠时间和ping计数您的preference。该脚本永远不会退出,所以你最有可能想用下面的命令来运行它:
./ connection_daemon.sh 2>&安培; 1>的/ dev / null的&安培;认
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
Here's one way to do it:
#!/bin/bash
while true; do
if ! [ "`ping -c 1 google.com; echo $?`" ]; then #if ping exits nonzero...
./connection_script1.sh #run the first script
sleep 10 #give it a few seconds to complete
fi
if ! [ "`ping -c 1 google.com; echo $?`" ]; then #if ping *still* exits nonzero...
./connection_script2.sh #run the second script
sleep 10 #give it a few seconds to complete
fi
sleep 300 #check again in five minutes
done
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
这篇关于使得bash脚本来检查连接,必要时更换连接。帮助我提高了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!