制作 bash 脚本来检查连接并在必要时更改连接。帮我改进一下?

发布于 2024-08-27 00:52:03 字数 800 浏览 5 评论 0原文

我的连接不稳定,但我有一个备份连接。我制作了一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜访吸血鬼 2024-09-03 00:52:04

尝试在某处使用 ConnectTimeout ${timeout} 。

Trying using ConnectTimeout ${timeout} somewhere.

煮茶煮酒煮时光 2024-09-03 00:52:03

一种方法是:

#!/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

根据您的喜好调整睡眠时间和 ping 计数。该脚本永远不会退出,因此您很可能希望使用以下命令运行它:

./connection_daemon.sh 2>&1 > /dev/null &否认

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

黎夕旧梦 2024-09-03 00:52:03

您是否尝试过从 dhclient 命令中省略 -nw 选项?

另外,如果不需要,请从您的 eval 和引号中删除它们。这样做:

if ping -c 1 google.com > /dev/null 2>&1

Have you tried omitting the -nw option from the dhclient command?

Also, remove the eval and quotes from your if they aren't necessary. Do it like this:

if ping -c 1 google.com > /dev/null 2>&1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文