简体   繁体   中英

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.

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. Just reply "You are connected to the internet.". But the truth my internet connection is unstable. Every 3 seconds connect - disconnect.

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. My project is written on perl.

I'm not sure I get your question properly, you mention you want to check with 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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