如何在perl中使用一个互联网连接

时间:2015-11-06 02:43:09

标签: perl

我有2个互联网连接wlan0和eth0,我想只在wlan0中运行这个脚本。

#!/usr/bin/perl
use LWP::UserAgent;
my $ua = LWP::UserAgent->new( timeout => 1 );
$ua->agent("007");
my $req = HTTP::Request->new( GET => 'http://120.28.53.178/favicon.ico' ); # HTTP::Request in wlan0 connection
my $ok = 1;
my $res;
for ( 1 .. 10 ) {
    $res = $ua->request($req);
    if ( $res->is_success ) {
        print "+ Google is up\n";
    }
    else {
        print "- Google is down\n";
        $ok = 0;
        last;
    }
    sleep 1;
}
if ($ok) {
    print "Result: Google is OK!\n";
}
else {
    print "Result: Google is not OK!\n";
}

如何编写代码才能使用wlan0 internet。这在perl脚本中是可行的吗?

1 个答案:

答案 0 :(得分:1)

你可以试试这个黑客

@LWP::Protocol::http::EXTRA_SOCK_OPTS = (LocalAddr => $source_ip);