#!/usr/bin/perl
#    { # Record IPs from lowest to highest to a file...

              $last_lowest=$lowest;

              $std_ip=Net::IP::ip_bintoip($last_lowest, 4);
              print "LAST_LOWEST_IPv4:$std_ip\n";

              # Find out if lower value is in list...
              for ( $counter = 0 ; $counter < $ips_listed ; ++$counter )
              {
                    $tmp=$ip_list[$counter];

                    if ( $tmp > $last_lowest )
                    {
                         $lowest=$tmp;
                    }
              } # END search for lower value...

              $std_ip=Net::IP::ip_bintoip($lowest, 4);
              print OUT "Lowest:$lowest\t$std_ip\n";

        } # End for loop...

} # END output_IP_list.

#-------------------------------------------------------------------------------

# spam_processor.pl: Extract from spam email the originating ip this garbage
#                    came from.

use Net::IP;  # Useful for converting IPv4 strings to binary numbers.
open ( SPAM, "spam") || die "Can't open spam!"; # mbox file containing  
the spam.
open ( OUT, ">ip_list") || die "Can't open ip_list!"; # This will contain the

$ips_listed=0;               # Total spam IP addresses listed.
$ip_list[0]=0b0;             # Start off IP list with 0d0.
$ip_addr="000.000.000.000";  # A default IPv4 string.

$MIN_IP=0b0;

&main;  # Program's first function called here ;-)

sub main
{ # This is the first routine that should be called.

      while ($origin=<SPAM>)
      {
             chomp;

             if ( $origin =~ /^X-Originating-IP\: \[.*\]$/ )
             {
                  chomp;

                  # Extract ip address from $origin and chop off garbage...
                  $ip_addr = substr ( $origin, 19, 16 );
                  while ( $ip_addr =~ /.*\].*/ )
                  {
                          chop ( $ip_addr );
                  }

                  $status=&no_dup_enter_into_list;

                  print "Status: $status \n";

             } # End of process acceptable line...

      } # End of while loop...

      &output_IP_list;

} # END main function.

#-------------------------------------------------------------------------------

sub no_dup_enter_into_list
{ # Check for duplicates before inserting candidate IP address.
   # Return a string indicating what happened.

         # Convert IP string representation to binary integer representation...
         $binip=Net::IP::ip_iptobin($ip_addr,4);

         for ( $count=0 ; $count < $ips_listed ; ++$count )
         {

                 if ( $binip eq $ip_list[$count] )
                 {
                      return ( "Sorry, duplicate found." );
                 }
         }

         &enter_into_list;

         return ( "Inserted new address!" );

} # END no_dup_enter_into_list function.



sub enter_into_list
{ # Unique spam source found, put IP address in the list...

     if ( $ips_listed == 0 )
     {
          $ip_list[0] = $binip;
     }
     else
     {
          $ip_list[$ips_listed] = $binip;
     }

     ++$ips_listed;

} # END enter_into_list function.

#-------------------------------------------------------------------------------

sub output_IP_list
{
        $lowest=$ip_list[0];  # Hold an IP value to be printed out.
   $last_lowest=$ip_list[0];  # Remeber the last IP chosen.
                     $tmp=0;  # Temporarily hold an IP from the list.
                 $counter=0;  # For searches...

        for ( $searcher1=0 ; $searcher1 < $ips_listed ; ++$searcher1 )
        { # Force starting out with the lowest value, must find it...

              $tmp=$ip_list[$searcher1];

              if ( $tmp < $lowest )
              {
                   $lowest=$tmp;
                   $last_lowest=$lowest;
              }
        }

        $std_ip=Net::IP::ip_bintoip($lowest, 4);
        print "LOWEST_IPv4:$std_ip\n";

        for ( $counter2=0 ; $counter2 < $ips_listed ; ++$counter2 )
     { # Record IPs from lowest to highest to a file...

              $last_lowest=$lowest;

              $std_ip=Net::IP::ip_bintoip($last_lowest, 4);
              print "LAST_LOWEST_IPv4:$std_ip\n";

              # Find out if lower value is in list...
              for ( $counter = 0 ; $counter < $ips_listed ; ++$counter )
              {
                    $tmp=$ip_list[$counter];

                    if ( $tmp > $last_lowest )
                    {
                         $lowest=$tmp;
                    }
              } # END search for lower value...

              $std_ip=Net::IP::ip_bintoip($lowest, 4);
              print OUT "Lowest:$lowest\t$std_ip\n";

        } # End for loop...

} # END output_IP_list.

#-------------------------------------------------------------------------------

The function output_IP_list makes 5 picks and then picks the same IP  
like 30 times.  This is clearly the wrong behavior.  Why is this  
happening and how do I
fix this?  I'm trying to list the IPs in order in a file so I can access them
from a C program that I will write later.  If there is a sort function  
in perl,
what is it, why does it work, and how do I use it?

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

_______________________________________________
PLUG mailing list
PLUG@lists.pdxlinux.org
http://lists.pdxlinux.org/mailman/listinfo/plug

Reply via email to