Perl script for IE automation

来源:互联网 发布:python社交数据可视化 编辑:程序博客网 时间:2024/04/27 13:44

# Here is a little script that I wrote to do some automatic URL browsing using IE
# what it does is to read proxy server and port from a text named "proxylist.txt", under the same dir,
# then test if that port is active, if it is, then set it as IE's proxy, then it fires IE and browse the url picked #randomly from an array @url; then close IE.
#
# This script runs on Winodws with activestate perl installed. Also you need to install #Win32::IEAutomation module.
#
# proxylist.txt formate should be like the following:
#145.123.202.56 3128
#234.80.225.37 8080

    use
 Win32::IEAutomation::WinClicker;
use Win32::IEAutomation;
use strict;
use Win32::TieRegistry ( Delimiter=>"/", ArrayValues=>1 );
use Net::Ping;
my $path = "proxylist.txt";
my $ie = Win32::IEAutomation->new( visible => 1, maximize => 1);
my @url = (
    
"http://blog.csdn.net/bsdplus/",
    
"http://blog.csdn.net/bsdplus/archive/2008/04/16/2296812.aspx",
    
"http://alan.blogcn.com",
    
"http://alan0203.blogspot.com",
    
"http://blog.csdn.net/bsdplus/archive/2007/10/22/1836987.aspx",
    
"http://blog.csdn.net/bsdplus/archive/2008/04/13/2289361.aspx",
    
"http://blog.csdn.net/bsdplus/archive/2008/04/14/2290446.aspx",
    
"http://alan0203.blogspot.com/2008/04/use-proxy-authentication-with-perl.html",
    
"http://alan0203.blogspot.com/2007/05/pcbsd-intel.html",
    
"http://alan0203.blogspot.com/2007/05/fcitx-pcbsd.html",
    
"http://alan0203.blogspot.com/2007/05/pcbsd_08.html",
    
"http://alan0203.blogspot.com/2007/05/windowswinxppcbsd.html",
    
"http://alan0203.blogspot.com/2007/05/winxppcbsd.html",
    
"http://alan0203.blogspot.com/2007/05/blog-post.html"
    ); 
# end of url array


open(P_SOURCE, "<", $path)
            or 
die "Couldn't open $path for reading: $! "#open txt file to read proxy list;
our @proxys = <P_SOURCE>;
    
my $waitSeconds = 2;
my $proxy_title = "Connect to myproxy.org";
my $wait_time = 1;

&set_IE_Proxy();

# start Site navigation
       my $myurl = @url[int(rand(9))]; #pick a url at random;
       sleep(int(rand(120)));
        
$ie->gotoURL($myurl,$waitSeconds);

       # uncomment the following if your proxy requires username/password and if ALREADY has them cached.

    
# my $clicker = Win32::IEAutomation::WinClicker->new( warnings => 1);
    # $clicker->push_confirm_button_ok($proxy_title, $wait_time);


    $ie->WaitforDone();
    
print " URL is: $myurl  ";
    
sleep(15);
    
$ie->closeIE();

# close(P_SOURCE);


sub set_IE_Proxy() {

        
my $proxy_pick = 1 + int(rand(@proxys));
        
print " $proxy_pick is $proxy_pick ";
        
my @SvrPtCn = split(" ", $proxys[$proxy_pick]); # split line into words;
        print "Proxy server pick is : $SvrPtCn[0]:$SvrPtCn[1] ";    
            
my $server = $SvrPtCn[0];
           
my $port = $SvrPtCn[1];
           
my $enable= 1 ;
            
my $override= "127.0.0.1;<local>";
        
        
################################################################
        # start TCP PING to see if the proxy is active on specified port


        
my $test = Net::Ping->new('tcp', 5);
        
$test->{port_num} =$port;

        
if (! $test->ping($server)) {
              
print "$server:$port isn't serving HTTP! ";
             
&set_IE_Proxy();#next;
        } else {
        
print "TCP Ping test ok! Setting IE proxy to $server:$port...... ";
        
my $rkey=$Registry->{"CUser/Software/Microsoft/Windows/CurrentVersion/Internet Settings"};
            
$rkey->SetValue("ProxyServer", "$server:$port", "REG_SZ");
           
$rkey->SetValue("ProxyEnable",pack("L",$enable), "REG_DWORD");
           
$rkey->SetValue("ProxyOverride", $override, "REG_SZ");
        
print "IE proxy set.  ";
    };
    }; 
# end of sub set_IE_proxy();