在perl脚本中使用代理服务器

时间:2018-04-25 04:48:34

标签: perl proxy

我尝试将代理服务器添加到以下perl脚本中:

#!/usr/bin/perl

TO='list of email adresses here';

require "/usr/local/SCRIPTS/www-tools/service-name/jcode.pl";
use LWP::UserAgent;
$sendmail = '/usr/lib/sendmail -t -oi';
######################
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = $year+1900;
$mon  = $mon+1;

$MMDD = sprintf("%02d/%02d",$mon,$mday);
$HHMM = sprintf("%02d:%02d",$hour,$min);

$err = 0;

#Ph.1 WebAccess-1stServer(VIP)
@ret = &SiteAccess( 'http://IP:PORT/hostname/' , 
                    'http://IP:PORT/hostname/Login.do' );

if( $ret[0] != 200 || $ret[2] != 200 ){
  $err = 1;
}
$body  = &jcode::euc($ret[1]);
if( $body !~ m#<title>ServiceName<\/title># ){
  $err = 2;
}

$title = &jcode::euc('<title>web$BO"7H(J</title>');
$body  = &jcode::euc($ret[3]);
if( $body !~ m#$title# ){
    $err = 3;
}
&MailSend('Web')  if( $err > 0 );
$err=0;

#Ph.2 WebAccess-2ndServer(Direct)
@ret = &SiteAccess( 'http://2ndServerIP:PORT/hostname/' , 
                    'http://2ndServerIP:PORT/hostname/Login.do' );


if( $ret[0] != 200 || $ret[2] != 200 ){
    $err = 4;
}
$body  = &jcode::euc($ret[1]);
if( $body !~ m#<title>ServiceName<\/title># ){
    $err = 5;
}

$title = &jcode::euc('<title>web$BO"7H(J</title>');
$body  = &jcode::euc($ret[3]);
if( $body !~ m#$title# ){
    $err = 6;
}
&MailSend('Web2nd')  if( $err > 0);
$err=0;

#Ph.3 POP
@ret = &SiteAccess( 'http://IP:PORT/hostname/' ,
                    'http://IP:PORT/hostname/Login.do' );

if( $ret[0] != 200 || $ret[2] != 200 ){
    $err = 7;
}
$body  = &jcode::euc($ret[1]);
if( $body !~ m#<title>ServiceName</title># ){
    $err = 8;
}

$title = &jcode::euc('<title>TitleJapanese<(J</title>');
$body  = &jcode::euc($ret[3]);
if( $body !~ m#$title# ){
    $err = 9;
}

&MailSend('POP')  if( $err > 0);
$err=0;


#Ph.4 Exchange(EWS)
@ret = &SiteAccess( 'http://IP:PORT/hostname/' ,
                    'http://IP:PORT/hostname/Login.do' );

if( $ret[0] != 200 || $ret[2] != 200 ){
    $err = 10;
}
$body  = &jcode::euc($ret[1]);
if( $body !~ m#<title>ServiceName</title># ){
    $err = 11;
}

$title = &jcode::euc('<title>TitleJapanese<(J</title>');
$body  = &jcode::euc($ret[3]);
if( $body !~ m#$title# ){
    $err = 12;
}

&MailSend('Exchange-EWS')  if( $err > 0);
$err=0;



sub SiteAccess{
    my $url1 = shift;
    my $url2 = shift;
    # $ua,$req,$res;i
    my @r;

    if($url1 ne ''){
        $ua = LWP::UserAgent->new;

        $req = HTTP::Request->new(GET => $url1);
        $res = $ua->request($req);

        $r[0] = $res->code;
    $r[1] = $res->content;
    }
    if($url2 ne ''){
        $ua = LWP::UserAgent->new;

        $res = $ua->post( $url2,
            {
                    "c" => "don't knwo what that is",
                    "u" => "users ldap",
                    "p" => "password"
                    }, 
                    "Content-Type" => "application/x-www-form-urlencoded",
              "User-Agent"   => "DoCoMo/2.0 N901iS(c100;TB;W24H12;ser123445654654645;icc898114564645667716666f)");

        $r[2] = $res->code;
        $r[3] = $res->content;
     }
    return @r;
}

sub MailSend{
    my $title = shift;
    my $body;
    my $from;
    my @message;

#the following lines need another file name jcode to display properly.    It     just means error code or auth error.

    $message[1] = &jcode::jis('[Web]$B%(%i!<%3!<%I(J');
    $message[4] = &jcode::jis('[Web2nd]$B%(%i!<%3!<%I(J');
    $message[7] = &jcode::jis('[POP]$B%(%i!<%3!<%I(J');
    $message[10] = &jcode::jis('[EWS]$B%(%i!<%3!<%I(J');

    $message[2] = &jcode::jis('[Web]$B%m%0%$%s2hLL(J');
    $message[5] = &jcode::jis('[Web2nd]$B%m%0%$%s2hLL(J');
    $message[8] = &jcode::jis('[POP]$B%m%0%$%s2hLL(J');
    $message[11] = &jcode::jis('[EWS]$B%m%0%$%s2hLL(J');

    $message[3] = &jcode::jis('[Web]auth$B%(%i!<(J');
    $message[6] = &jcode::jis('[Web2nd]auth$B%(%i!<(J');
    $message[9] = &jcode::jis('[POP]auth$B%(%i!<(J');
    $message[12] = &jcode::jis('[EWS]auth$B%(%i!<(J');

    $from = 'mail@abc.com';

    $title = '['.$title.']cnct1 err';

#    $title = $title.'['.$MMDD.$HHMM.']';

    $body =<<END_OF_BODY;
To: $TO
Subject: $title
From: $from

ConnectOneCheck Error
Date       : $MMDD $HHMM
ErrorStatus: $err 
ErrorMsg   : $message[$err] 
END_OF_BODY

open(ML,"| $sendmail") || &error("Can't execute sendmail : $sendmail\n");
    print ML $body;
    close(ML);
}

所以现在我尝试添加代理使用的部分。除最后一项功能外,大部分零件都已注释掉:

!在/ usr / bin中/ perl的

TO='list of email adresses here';

require "/usr/local/SCRIPTS/www-tools/service-name/jcode.pl";
use LWP::UserAgent;
$sendmail = '/usr/lib/sendmail -t -oi';
######################
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = $year+1900;
$mon  = $mon+1;

$MMDD = sprintf("%02d/%02d",$mon,$mday);
$HHMM = sprintf("%02d:%02d",$hour,$min);

$err = 0;

下一部分已被注释掉,因为它没有被使用。

@ret = &SiteAccess( 'http://IP:PORT/hostname/' ,
                    'http://IP:PORT/hostname/Login.do' );

if( $ret[0] != 200 || $ret[2] != 200 ){
    $err = 10;
}
$body  = &jcode::euc($ret[1]);
if( $body !‾ m#<title>ConnectONE</title># ){
    $err = 11;
}

$title = &jcode::euc('<title>TitleJapanese</title>');
$body  = &jcode::euc($ret[3]);
if( $body !‾ m#$title# ){
    $err = 12;
}

&MailSend('Exchange-EWS')  if( $err > 0);
$err=0;



sub SiteAccess{
    my $url1 = shift;
    my $url2 = shift;
    # $ua,$req,$res;i
    my @r;

    if($url1 ne ''){
        $ua = LWP::UserAgent->new;
        $ENV{HTTP_proxy} = "proxy fqdn here";                     
        $ua->env_proxy                                                      

        $req = HTTP::Request->new(GET => $url1);
        $res = $ua->request($req);

        $r[0] = $res->code;
    $r[1] = $res->content;
    }
    if($url2 ne ''){
        $ua = LWP::UserAgent->new;
        $ENV{HTTP_proxy} = "proxy fqdn here";                     
        $ua->env_proxy                                                      
           $res = $ua->post( $url2,
            {
                    "c" => "don't knwo what that is",
                    "u" => "users ldap",
                    "p" => "password"
                    }, 
                    "Content-Type" => "application/x-www-form-urlencoded",
              "User-Agent"   => "DoCoMo/2.0 N901iS(c100;TB;W24H12;ser123445654654645;icc898114564645667716666f)");

        $r[2] = $res->code;
        $r[3] = $res->content;
     }
    return @r;
}

sub MailSend{
    my $title = shift;
    my $body;
    my $from;
    my @message;

#the following lines need another file name jcode to display properly.    It     just means error code or auth error.

    #$message[1] = &jcode::jis('[Web]$B%(%i!<%3!<%I(J');
    #$message[4] = &jcode::jis('[Web2nd]$B%(%i!<%3!<%I(J');
    #$message[7] = &jcode::jis('[POP]$B%(%i!<%3!<%I(J');
    $message[10] = &jcode::jis('[EWS]$B%(%i!<%3!<%I(J');

    #$message[2] = &jcode::jis('[Web]$B%m%0%$%s2hLL(J');
    #$message[5] = &jcode::jis('[Web2nd]$B%m%0%$%s2hLL(J');
    #$message[8] = &jcode::jis('[POP]$B%m%0%$%s2hLL(J');
    $message[11] = &jcode::jis('[EWS]$B%m%0%$%s2hLL(J');

    #$message[3] = &jcode::jis('[Web]auth$B%(%i!<(J');
    #$message[6] = &jcode::jis('[Web2nd]auth$B%(%i!<(J');
    #$message[9] = &jcode::jis('[POP]auth$B%(%i!<(J');
    $message[12] = &jcode::jis('[EWS]auth$B%(%i!<(J');

    $from = 'mail@abc.com';

    $title = '['.$title.']cnct1 err';

#    $title = $title.'['.$MMDD.$HHMM.']';

    $body =<<END_OF_BODY;
To: $TO
Subject: $title
From: $from

ConnectOneCheck Error
Date       : $MMDD $HHMM
ErrorStatus: $err 
ErrorMsg   : $message[$err] 
END_OF_BODY

open(ML,"| $sendmail") || &error("Can't execute sendmail : $sendmail\n");
    print ML $body;
    close(ML);
}

好的,所以我使用此功能添加了代理

$ua = LWP::UserAgent->new;
$ENV{HTTP_proxy} = "here is the FQDN of the proxy";                     
$ua->env_proxy    

但它编译得不好。它给了我这个错误:

  

语法错误/usr/local/SCRIPTS/www-tools/connectone/ConnectOneCheck.pl第93行,靠近&#34; $ body!&#34;   无法识别的字符\ xE2;标记为&lt; - HERE在f之后($ body!&lt; - HERE在第12栏附近/usr/local/SCRIPTS/www-tools/connectone/ConnectOneCheck.pl第93行。

有人可以让这个脚本运行吗?

2 个答案:

答案 0 :(得分:0)

您的脚本无法编译,因为它在此处显示。在第3行,它说

  

TO='list of

需要更改为

$TO='list of

......然后它为我编译。没有错误,你将进入第93行。

从我有限的观点来看,这意味着你的代码行93中可能存在一些错误的印刷品,而这些印刷错误并没有达到这方面的代码。

如果您在脚本中删除第93行并将第93行复制回代码中,您可能会安顿下来。

答案 1 :(得分:0)

您的源代码中包含非ASCII字符OVERLINE (U+203E)),这就是所显示错误的原因。

U + 2000–206F "General Punctuation" block中Unicode字符的第一个代码点是\xE2。那是第一个线索。上划线为0xE2 0x80 0xBE(十六进制)。

也许有一些编码问题将原始源中少数几个地方的波浪号(~)转换为这些上划线。

当“ curly”引号,en(–)和em(-)破折号等已渗入源代码时,您可能还会看到类似的错误。有时,这是由于好用的软件(例如WordPressMS Word)会自动替换(例如,直接替换为弯引号),然后又将其复制粘贴到源代码中。

很难在视觉上发现这些字符(起初我没有注意到上划线),但是此shell单线剥离非ASCII范围内的字符,然后与左侧的原始文件并排比较:

# assumes Bash shell
sdiff --suppress-common-lines script.pl <(tr -cd '\11\12\15\40-\176' <script.pl)

this SO question中描述了Python上下文中的类似编码问题,并带有有关\xE2的错误消息。这使我有了在源代码中查找非ASCII字符的想法。