<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: perl code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Wed, 23 Jul 2008 21:32:34 GMT</pubDate>
    <description>DZone Snippets: perl code</description>
    <item>
      <title>Perl : scan a list of networks, looking for hosts responding on the port 80 (http)</title>
      <link>http://snippets.dzone.com/posts/show/5467</link>
      <description>// Input : a list of address of routers, in dotted decimal notation&lt;br /&gt;&lt;code&gt;&lt;br /&gt;use strict;&lt;br /&gt;use Net::Ping;&lt;br /&gt;use IO::Socket::INET;&lt;br /&gt;&lt;br /&gt;sub isodate() {&lt;br /&gt;        my ($day, $mon, $year, $hour, $min, $sec) = (localtime)[3, 4, 5, 2, 1, 0];&lt;br /&gt;        $mon++; # 0-based index&lt;br /&gt;        $year = $year + 1900;&lt;br /&gt;        my $date = sprintf ("%04i-%02i-%02i %02i\:%02i\:%02i", $year, $mon, $day, $hour, $min, $sec);&lt;br /&gt;        return $date;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;sub testhost {&lt;br /&gt;       my $p = new Net::Ping("tcp");&lt;br /&gt;       $p-&gt;{port_num}=80; &lt;br /&gt;       my @result = $p -&gt; ping($_[0],2);&lt;br /&gt;       return $result[0];&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;sub to_dot {&lt;br /&gt;	my $n = shift;&lt;br /&gt;	my @decimal;&lt;br /&gt;	for (1..4) {&lt;br /&gt;		unshift @decimal, $n &amp; 0xFF;&lt;br /&gt;		$n &gt;&gt;= 8;&lt;br /&gt;	}&lt;br /&gt;	return join(".",@decimal);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;my %dejavu;&lt;br /&gt;open FH,"liste.txt";&lt;br /&gt;while (&lt;FH&gt;) {&lt;br /&gt;	chomp;&lt;br /&gt;	my ($routeur,$mask)=split;&lt;br /&gt;	&lt;br /&gt;	next if $routeur !~ /\d+\.\d+\.\d+\.\d+$/ or $mask !~ /\d+\.\d+\.\d+\.\d+$/;&lt;br /&gt;	&lt;br /&gt;	next if defined($dejavu{$routeur});&lt;br /&gt;	$dejavu{$routeur}=1;&lt;br /&gt;	&lt;br /&gt;	my ($o1,$o2,$o3,$o4) = split /\./,$mask;&lt;br /&gt;	my $mask=$o1*256**3+$o2*256**2+$o3*256+$o4;&lt;br /&gt;	my $num = $mask ^ 0xFFFFFFFF;&lt;br /&gt;	$num--;&lt;br /&gt;&lt;br /&gt;	my ($o1,$o2,$o3,$o4) = split /\./,$routeur;&lt;br /&gt;	my $net=$o1*256**3+$o2*256**2+$o3*256+$o4 &amp; $mask;&lt;br /&gt;	&lt;br /&gt;	#print join("|",$routeur,&amp;to_dot($net),$num)."\n";&lt;br /&gt;	&lt;br /&gt;	print "Starting scanning network ".to_dot($net).", router = ".$routeur."\n";&lt;br /&gt;	print "Adresses demarrant de ".to_dot($net+1)." et finissant a ".to_dot($net+$num).".\n";&lt;br /&gt;	for my $i (1..$num) {&lt;br /&gt;		my $host=to_dot($net+$i);&lt;br /&gt;		if ( &amp;testhost($host) ) {&lt;br /&gt;			print "$host is alive\n";&lt;br /&gt;			my $port=80;&lt;br /&gt;			my $sock = new IO::Socket::INET (PeerAddr =&gt; $host,&lt;br /&gt;					     PeerPort =&gt; $port,&lt;br /&gt;					     Proto =&gt; 'tcp');&lt;br /&gt;			if ($sock){&lt;br /&gt;				close $sock;&lt;br /&gt;				print "$port -open on $host\n";&lt;br /&gt;				open OUT,"&gt;&gt;webservers.txt";&lt;br /&gt;				print OUT join("|",isodate(),$host,to_dot($net),$routeur)."\n";&lt;br /&gt;				close OUT;&lt;br /&gt;			}	else	{&lt;br /&gt;				print "$port -closed on $host\n";&lt;br /&gt;			}&lt;br /&gt;&lt;br /&gt;		} else {&lt;br /&gt;			print "$host is not responding\n";&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;close FH;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 06 May 2008 07:05:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5467</guid>
      <author>bouffon69 (Sylvain Le Courtois)</author>
    </item>
    <item>
      <title>Browser automation using perl LWP</title>
      <link>http://snippets.dzone.com/posts/show/4587</link>
      <description>// This is a sample code used to measure reports response times on a OAS application.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/perl&lt;br /&gt;#&lt;br /&gt;# LWP connection to the Datamart Portal&lt;br /&gt;# Timing of the main reports&lt;br /&gt;#&lt;br /&gt;&lt;br /&gt;use strict;&lt;br /&gt;require LWP::UserAgent;&lt;br /&gt;&lt;br /&gt;my $ua = LWP::UserAgent-&gt;new;&lt;br /&gt;&lt;br /&gt;sub isodate() {&lt;br /&gt;        my ($day, $mon, $year, $hour, $min, $sec) = (localtime)[3, 4, 5, 2, 1, 0];&lt;br /&gt;        $mon++; # 0-based index&lt;br /&gt;        $year = $year + 1900;&lt;br /&gt;        my $date = sprintf ("%04i-%02i-%02i %02i\:%02i\:%02i", $year, $mon, $day, $hour, $min, $sec);&lt;br /&gt;        return $date;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;sub datamart_login {&lt;br /&gt;        my ( $user, $pass ) = @_;&lt;br /&gt;        my $time_begin=time();&lt;br /&gt;        my $url='http://daprd:7782/portal/page?_pageid=37,134413,37_134422&amp;_dad=portal&amp;_schema=PORTAL';&lt;br /&gt;        my $req = HTTP::Request-&gt;new( GET =&gt; $url );&lt;br /&gt;        my $resp = $ua-&gt;request($req);&lt;br /&gt;        my $loginform = $resp-&gt;content ;&lt;br /&gt;        if ( $loginform !~ /Entrez votre nom utilisateur/ ) {&lt;br /&gt;                die isodate()." Failed to get the logon page of the Web Site\n";&lt;br /&gt;        } else {&lt;br /&gt;                my $locale="";&lt;br /&gt;                my ($v) = $loginform =~ /NAME=\"v\" value=\"(.+)\"/;&lt;br /&gt;                my ($site2pstoretoken) = $loginform =~ /NAME=\"site2pstoretoken\" value=\"(.+)\"/;&lt;br /&gt;                my ($submiturl) = $loginform =~ /form method=\"POST\" action=\"(.*?)\"/;&lt;br /&gt;                $resp = $ua-&gt;post( $submiturl,&lt;br /&gt;                   [&lt;br /&gt;                     ssousername =&gt; $user,&lt;br /&gt;                     password =&gt; $pass,&lt;br /&gt;                     v =&gt; $v,&lt;br /&gt;                     locale =&gt; $locale,&lt;br /&gt;                     site2pstoretoken =&gt; $site2pstoretoken&lt;br /&gt;                   ],&lt;br /&gt;                );&lt;br /&gt;                $resp = $ua-&gt;get($url);&lt;br /&gt;                $resp = $ua-&gt;get($url);&lt;br /&gt;                if ( $resp-&gt;content !~ /Crit..res de recherche/ ) {&lt;br /&gt;                        die isodate()." Failed to get the main page of Portal\n";&lt;br /&gt;                }&lt;br /&gt;        }&lt;br /&gt;        print join(";",isodate(),"Time to log on the Portal",time()-$time_begin,$url)."\n";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;sub datamart_testurl {&lt;br /&gt;        my ($label,$url,$expected)=@_;&lt;br /&gt;        my $time_begin=time();&lt;br /&gt;        my $resp;&lt;br /&gt;        $resp = $ua-&gt;get($url);&lt;br /&gt;        $resp = $ua-&gt;get($url) if $resp-&gt;content !~ /$expected/;        # We try two times !&lt;br /&gt;        if ( $resp-&gt;content !~ /$expected/ ) {&lt;br /&gt;                print STDERR isodate()." Test Failed on $label. $expected not found in response.\n";&lt;br /&gt;                print STDERR $resp-&gt;as_string;&lt;br /&gt;        } else {&lt;br /&gt;                print join(";",isodate(),$label,time()-$time_begin,$url)."\n";&lt;br /&gt;        }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;$ua-&gt;timeout(1200);&lt;br /&gt;$ua-&gt;cookie_jar({});&lt;br /&gt;$ua-&gt;agent( 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)' );&lt;br /&gt;#push @{ $ua-&gt;requests_redirectable }, 'POST';&lt;br /&gt;&lt;br /&gt;# &gt;&gt;&gt;&gt; Main code here&lt;br /&gt;&lt;br /&gt;datamart_login("xxxx","xxxx");&lt;br /&gt;&lt;br /&gt;open FH,"/exploit/scripts/appli/check_datamart.ini" or die "Unable to open check_datamart.ini";&lt;br /&gt;while (&lt;FH&gt;) {&lt;br /&gt;        chomp();&lt;br /&gt;        my ($report,$expected,$url) = split /;/;&lt;br /&gt;        datamart_testurl($report,$url,$expected);&lt;br /&gt;}&lt;br /&gt;close FH;&lt;br /&gt;&lt;br /&gt;# &lt;&lt;&lt;&lt; Main code here&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 28 Sep 2007 11:48:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4587</guid>
      <author>bouffon69 (Sylvain Le Courtois)</author>
    </item>
    <item>
      <title>Internet Explorer automation using win32::OLE</title>
      <link>http://snippets.dzone.com/posts/show/4586</link>
      <description>// Sample code used for one of my client&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/perl -w&lt;br /&gt;&lt;br /&gt;use strict;&lt;br /&gt;use Data::Dumper;&lt;br /&gt;use Win32::OLE qw( EVENTS );&lt;br /&gt;&lt;br /&gt;my ($day, $mon, $year, $hour, $min, $sec) = (localtime)[3, 4, 5, 2, 1, 0];&lt;br /&gt;$mon++; # 0-based index&lt;br /&gt;$year = $year + 1900;&lt;br /&gt;my $date = sprintf ("%04i-%02i-%02i %02i\:%02i\:%02i", $year, $mon, $day, $hour, $min, $sec);&lt;br /&gt;&lt;br /&gt;my $Disconnect;&lt;br /&gt;my $Menu;&lt;br /&gt;my $TreeView;&lt;br /&gt;my $WatchDog;&lt;br /&gt;my $MenuClicked=0;&lt;br /&gt;&lt;br /&gt;my $ScenarioCompleted=0;&lt;br /&gt;&lt;br /&gt;my @TreeViewLinks=("Appareillage du B","Branchement Comptage","Branchement individuel");&lt;br /&gt;my $Previouslink=$TreeViewLinks[0];&lt;br /&gt;&lt;br /&gt;my $ie = Win32::OLE-&gt;new( 'InternetExplorer.Application' ) or die "error starting IE";&lt;br /&gt;$ie-&gt;{visible} = 1;&lt;br /&gt;&lt;br /&gt;Win32::OLE-&gt;Option( Warn =&gt; 3 );&lt;br /&gt;&lt;br /&gt;$WatchDog=time();&lt;br /&gt;Win32::OLE-&gt;WithEvents( $ie, \&amp;Event, 'DWebBrowserEvents2' );&lt;br /&gt;$ie-&gt;navigate( 'http://www.xxx.fr' );&lt;br /&gt;Win32::OLE-&gt;MessageLoop();&lt;br /&gt;unlink("noemis.err") if -f "noemis.err";&lt;br /&gt;if ( ! $ScenarioCompleted ) {&lt;br /&gt;	open( ERR , "&gt;noemis.err" ); &lt;br /&gt;	print ERR "Problem executing Noemis scenario, please check www.xxx.fr.\n" ;&lt;br /&gt;	close(ERR);&lt;br /&gt;}&lt;br /&gt;$Disconnect-&gt;Click();&lt;br /&gt;Win32::OLE-&gt;SpinMessageLoop;&lt;br /&gt;&lt;br /&gt;# Maintenance du fichier historique&lt;br /&gt;open ( STATS , "noemis.txt" );&lt;br /&gt;my @lines=&lt;STATS&gt;;&lt;br /&gt;close (STATS);&lt;br /&gt;open( STATS , "&gt;noemis.txt" ); &lt;br /&gt;for my $line (@lines) {&lt;br /&gt;	my ($datetime) = split ( /;/ , $line );&lt;br /&gt;	my ($h_year,$h_mon) = $datetime =~ /^([0-9]{4})-([0-9]{2})/;&lt;br /&gt;	print STATS $line if ($year*12+$mon) - ($h_year*12+$h_mon) &lt; 2;&lt;br /&gt;}&lt;br /&gt;print STATS join(";",$date,"Noemis Scenario",( time() - $WatchDog ))."\n";&lt;br /&gt;close( STATS );&lt;br /&gt;&lt;br /&gt;sleep 2;&lt;br /&gt;Win32::OLE-&gt;SpinMessageLoop;&lt;br /&gt;sleep 1;&lt;br /&gt;$ie-&gt;Quit();&lt;br /&gt;exit 0;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;sub Event {&lt;br /&gt;	my ($Obj,$Event,@Args) = @_;&lt;br /&gt;	my $IEObject = shift @Args;&lt;br /&gt;	print " Event triggered: $Event\n";    &lt;br /&gt;&lt;br /&gt;	my ($i,$anchor);&lt;br /&gt;	my $anchors;&lt;br /&gt;    &lt;br /&gt;	# STEP 1 : Find the main menu, login to the web site, find the treeview&lt;br /&gt;	if ($Event eq "DocumentComplete") {    &lt;br /&gt;		print "URL: " . $IEObject-&gt;Document-&gt;URL . "\n";&lt;br /&gt;		if ( $IEObject-&gt;Document-&gt;URL eq "http://www.xxx.fr/ident.aspx" ) {&lt;br /&gt;			my $forms = $IEObject-&gt;Document-&gt;forms;&lt;br /&gt;			my $form = $forms-&gt;item(0);&lt;br /&gt;			if ( defined($form-&gt;elements("fldNumCli")) ) {&lt;br /&gt;				print "--------------------------------------------\n";&lt;br /&gt;				print "Found the login box, authenticating ...\n";&lt;br /&gt;				print "--------------------------------------------\n";&lt;br /&gt;			    $form-&gt;elements("fldNumCli")-&gt;{value} = "xxxx";&lt;br /&gt;			    $form-&gt;elements("fldUtil")-&gt;{value} = "xxx";&lt;br /&gt;			    $form-&gt;elements("fldPwd")-&gt;{value} = "xxx";&lt;br /&gt;		    	$form-&gt;elements("btIdent")-&gt;Click();&lt;br /&gt;	    		}&lt;br /&gt;		}&lt;br /&gt;		if ( $IEObject-&gt;Document-&gt;URL eq "http://www.xxx.fr/menu.aspx" ) {&lt;br /&gt;			print "Found the menu.\n";&lt;br /&gt;			$Menu = $IEObject-&gt;Document;&lt;br /&gt;			$anchors = $IEObject-&gt;Document-&gt;links;&lt;br /&gt;			for (my $i=0; $i &lt; $anchors-&gt;length; $i++) {&lt;br /&gt;				$anchor = $anchors-&gt;item($i);&lt;br /&gt;				print $anchor-&gt;href."\n";&lt;br /&gt;				$Disconnect = $anchor if $anchor-&gt;href eq "http://www.xxx.fr/ident.aspx?qs=deconnecter";&lt;br /&gt;			}&lt;br /&gt;	      	}	    &lt;br /&gt;		if ( $IEObject-&gt;Document-&gt;URL eq "http://www.xxx.fr/client/frameTreeview.aspx" ) {&lt;br /&gt;			print "Found the TreeView.\n";&lt;br /&gt;			$TreeView = $IEObject-&gt;Document;&lt;br /&gt;      		}		&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	# STEP 2 : Click on the Menu and TreeView links   &lt;br /&gt;	if ($Event eq "DocumentComplete") {    		&lt;br /&gt;	if ( ! $MenuClicked and defined($Menu) ) {&lt;br /&gt;		my $MenuItem = $Menu-&gt;getElementById("SM_CLIE_RECH");&lt;br /&gt;		if ( defined($MenuItem) ) { &lt;br /&gt;			print $MenuItem-&gt;ID."\n";&lt;br /&gt;			$MenuItem-&gt;Click;&lt;br /&gt;			$MenuClicked = 1;&lt;br /&gt;		}&lt;br /&gt;	}}&lt;br /&gt;&lt;br /&gt;	if ( $Event eq "CommandStateChange" or $Event eq "StatusTextChange" ) {&lt;br /&gt;		print Dumper($IEObject);&lt;br /&gt;	}&lt;br /&gt;	if ( @TreeViewLinks != 0 and &lt;br /&gt;	     defined($TreeView) and &lt;br /&gt;	     $Event eq "DocumentComplete" &lt;br /&gt;	) {&lt;br /&gt;		my $link = shift(@TreeViewLinks);&lt;br /&gt;		$anchors = $TreeView-&gt;links;&lt;br /&gt;		my $found=0;&lt;br /&gt;		print "Looking for '$link' in the TreeView ... \n";&lt;br /&gt;	        for (my $i=0; $i &lt; $anchors-&gt;length; $i++) {&lt;br /&gt;		       	$anchor = $anchors-&gt;item($i);&lt;br /&gt;	        	#print $anchor-&gt;innerHTML."\n";&lt;br /&gt;		       	if ( $anchor-&gt;innerHTML =~ /$link/ ) {&lt;br /&gt;				print "Clicking on '$link' ... \n";&lt;br /&gt;	                	$anchor-&gt;Click;&lt;br /&gt;				$found=1;&lt;br /&gt;				$Previouslink=$link;&lt;br /&gt;				last;&lt;br /&gt;			}&lt;br /&gt;	        }&lt;br /&gt;		if ( ! $found ) { &lt;br /&gt;			# Le TreeView a bugge, on reclique&lt;br /&gt;			sleep 1;&lt;br /&gt;			print "Looking for '$Previouslink' in the TreeView ... \n";&lt;br /&gt;		        for (my $i=0; $i &lt; $anchors-&gt;length; $i++) {&lt;br /&gt;			       	$anchor = $anchors-&gt;item($i);&lt;br /&gt;	        		#print $anchor-&gt;innerHTML."\n";&lt;br /&gt;		       		if ( $anchor-&gt;innerHTML =~ /$Previouslink/ ) {&lt;br /&gt;					print "Clicking on '$Previouslink' ... \n";&lt;br /&gt;	              		  	$anchor-&gt;Click;&lt;br /&gt;					last;&lt;br /&gt;				}&lt;br /&gt;		        }&lt;br /&gt;			unshift @TreeViewLinks,$link;&lt;br /&gt;		}&lt;br /&gt;	} &lt;br /&gt;   &lt;br /&gt;	# STEP 3 : Verify the list displayed &lt;br /&gt;		&lt;br /&gt;	if ($Event eq "DocumentComplete") {    &lt;br /&gt;   		if ( @TreeViewLinks == 0 and $IEObject-&gt;Document-&gt;URL =~ /listeRefPlof.aspx/ ) {&lt;br /&gt;			print "Scenario completed, exiting ...\n";&lt;br /&gt;			$ScenarioCompleted=1;&lt;br /&gt;	   		Win32::OLE-&gt;QuitMessageLoop;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;    &lt;br /&gt;&lt;br /&gt;	# Exit on errors&lt;br /&gt;	    &lt;br /&gt;	Win32::OLE-&gt;QuitMessageLoop() if $Event eq "OnQuit" or time() &gt; $WatchDog + 60;&lt;br /&gt;    &lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 28 Sep 2007 11:45:24 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4586</guid>
      <author>bouffon69 (Sylvain Le Courtois)</author>
    </item>
    <item>
      <title>Simple output of date in perl</title>
      <link>http://snippets.dzone.com/posts/show/4506</link>
      <description>// Simple output of current date in perl&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  my @day_name = ("Sun.","Mon.","Tue.","Wed.","Thu.","Fri.","Sat.");&lt;br /&gt;  my ($sec,$min,$hour,$mday,$mon,$year,$wday); &lt;br /&gt;  ($sec,$min,$hour,$mday,$mon,$year,$wday,undef,undef)=localtime(time()); $year+=1900;$mon++;&lt;br /&gt;  $report_date=sprintf("%s %04d.%02d.%02d %02d:%02d",$day_name[$wday],$year,$mon,$mday,$hour,$min);&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 07 Sep 2007 07:55:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4506</guid>
      <author>bouffon69 (Sylvain Le Courtois)</author>
    </item>
  </channel>
</rss>
