简体   繁体   English

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

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

I want script in perl that can check if my internet is stable, not stable or no internet connection. 我想要perl中的脚本,该脚本可以检查我的互联网是否稳定,不稳定或没有互联网连接。

I use Ping::Net script but reply is "You are connected to the internet.", not checking internet connection in 30 seconds if stable, not stable or no internet connection. 我使用Ping :: Net脚本,但回答是“您已连接到Internet。”,如果稳定,不稳定或没有Internet连接,则在30秒内不检查Internet连接。 Just reply "You are connected to the internet.". 只需回答“您已连接到互联网。”。 But the truth my internet connection is unstable. 但事实是我的互联网连接不稳定。 Every 3 seconds connect - disconnect. 每3秒钟连接-断开连接。

This is the script 这是脚本

$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();

I want to use wget as my tester, but I don't know how to script it in perl. 我想将wget用作测试仪,但是我不知道如何在perl中编写脚本。 My project is written on perl. 我的项目是用perl编写的。

I'm not sure I get your question properly, you mention you want to check with wget. 我不确定我是否正确回答了您的问题,您提到您要使用wget进行检查。 I'm using LWP here to make the request in perl but you want to execute a program and get the result just use: 我在这里使用LWP在perl中发出请求,但是您想执行一个程序并仅使用以下命令获得结果:

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

But I'd suggest going for something like: 但我建议您去做类似的事情:

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;
}

That won't check for exactly 30 seconds since the requests take time but I'm assuming that isn't really important 自从请求花费时间以来,这不会准确检查30秒,但我认为那并不重要

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

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