繁体   English   中英

如果稳定,不稳定并且没有互联网连接,Perl会检查互联网连接30秒

[英]Perl check internet connection for 30 seconds if stable, not stable and no internet connection

我想要perl中的脚本,该脚本可以检查我的互联网是否稳定,不稳定或没有互联网连接。

我使用Ping :: Net脚本,但回答是“您已连接到Internet。”,如果稳定,不稳定或没有Internet连接,则在30秒内不检查Internet连接。 只需回答“您已连接到互联网。”。 但事实是我的互联网连接不稳定。 每3秒钟连接-断开连接。

这是脚本

$ping = Net::Ping->new("icmp");
$ping->port_number("80");
if ( $ping->ping( 'www.google.com', '10' ) ) {
    print "You are connected to the internet.\n";
}
else {
    print "You are not connected to the internet.\n";
}
$ping->close();

我想将wget用作测试仪,但是我不知道如何在perl中编写脚本。 我的项目是用perl编写的。

我不确定我是否正确回答了您的问题,您提到您要使用wget进行检查。 我在这里使用LWP在perl中发出请求,但是您想执行一个程序并仅使用以下命令获得结果:

my $res = `wget -O - -q http://google.com`

但我建议您去做类似的事情:

use strict;
use warnings;
use LWP::UserAgent;

my $ua = LWP::UserAgent->new;
$ua->agent("007");

my $req = HTTP::Request->new(GET => 'http://google.com');

my $res;
for (1..30) {
    $res = $ua->request($req);
    if ($res->is_success) {
        print localtime." Google is here\n";
    } else {
        print localtime."Google is gone\n";
    }
    sleep 1;
}

自从请求花费时间以来,这不会准确检查30秒,但我认为那并不重要

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM