wget脚本连接,成功则等待,未连接则继续

wget script to connect, wait if successful, continue if no connection

我必须创建一个脚本来向我们网络上的所有设备发送 wget 联系信息。 wget 连接到 url,这会触发端点联系服务器。

我遇到的问题是我需要能够将命令发送到网络上的每个 IP 地址,如果连接成功,请注意 30 秒,然后转到下一个 url列表。但是,如果连接不成功,我希望脚本继续执行下一个 url,不要停顿。

目前我正在使用 bash 脚本发送命令,在 url 之间暂停 =30,连接尝试设置为 1,超时设置为 1。这适用于成功的连接,但它也会挂在不成功的地址上。

关于如何暂停成功并在死地址超时后继续前进的任何建议?

这是我目前运行,

的命令

wget --connect-timeout=1 --tries=1 http://xx.xx.xx.xx:8085/contact_dls.html/ContactDLS
睡 30
wget --connect-timeout=1 --tries=1 http://xx.xx.xx.xx:8085/contact_dls.html/ContactDLS
等等等等等等

谢谢

此任务不需要 wget - 一切都可以在 Perl 中完成。

只需使用如下代码:

use LWP::UserAgent;
use HTTP::Request;

my $ua = new LWP::UserAgent;
$ua->timeout(1);
my $req = new HTTP::Request(GET => $url);
my $res = $ua->request($req);
if ($res->is_success()) {
    print("Connection to '$url' was OK");
    sleep(30);
} else {
    print("Cannot access '$url'");
    sleep(1);
}

这会影响您的 url,但会在 1 秒后超时。

我可能会将 url 加载到一个数组中并遍历该数组。像这样

#!/usr/bin/perl

use warnings;
use strict;

my @urls = qw(test.com url.com 123abc.com fail.aa);

foreach my $url (@urls){
    my $check = `wget --server-response $url 2>&1 | grep HTTP/ | awk '{print $2}'`;
    #if there is a successful response you can set the sleep(3) to sleep(30) for your instance.
    if($check){
        print "Found $url -- sleeping 3 seconds\n";
        sleep(3);
    }
    else{
        print "$url does not exist\n";
        #If the url does not exist or respond it will move on to the next item in the array.  You could add a sleep(1) before the next for a 1 second pause
        next;
    }
}

当然这是假设您使用的是linux。网址也可以通过其他方式加载,我不知道您当前的脚本是如何获取它们的。以上是一个示例,您当然需要进行调整以适应您的环境