Perl 检查互联网连接 30 秒,看是否稳定、是否不稳定以及是否没有互联网连接

Perl 检查互联网连接 30 秒,看是否稳定、是否不稳定以及是否没有互联网连接

我想要一个可以检查我的互联网是否稳定、不稳定或没有互联网连接。

我用网络::Ping脚本,但回复是“ You are connected to the internet.”,不会在 30 秒内检查互联网连接是否稳定、不稳定或没有互联网连接。只需回复“ You are connected to the 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 编写的。

答案1

您的脚本似乎已经接近正常工作。这是我经过一些调整后的结果:

#!/usr/bin/perl

use warnings;
use strict;
use Net::Ping;

my $ping = Net::Ping->new("tcp");
$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();

笔记:

  • icmp ping 需要 root 权限,并且可能被 google 或其他某个人阻止,因此请坚持使用 tcp ping。没有人会阻止它。
  • use strict并且use warnings是良好的 Perl 习惯
  • 你需要use模块

相关内容