簡體   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