Perl Help - Use of uninitialised values

Soldato
Joined
12 Jan 2006
Posts
2,547
I have no idea what to try, i have traced the problem to the like

process_log($line);

But have no idea how to fix it as i declare $line before hand :s

The error i get is:
Code:
Use of uninitialized value $sessionState in string eq at parser2.pl line 124.
 at parser2.pl line 124
    main::process_log('Dec 16 07:50:22 vpn-klb-prv 1765156 12/16/2007 07:50:21.110 S...') called at parser2.pl line 54

Use of uninitialized value in string eq at parser2.pl line 130.
 at parser2.pl line 130
    main::process_log('Dec 16 07:50:22 vpn-klb-prv 1765156 12/16/2007 07:50:21.110 S...') called at parser2.pl line 54
so on and so forth..

Code:
#!/usr/bin/perl -w
                  #-w use warnings
                  #-T use Taint to ensure program is secure

#use strict 'vars';                     #Force variable declarations
# Functions to locate errors
use Carp ();
local $SIG{__WARN__} = \&Carp::cluck;


#=== Delcare Varibles ===#
my $line = "empty";             #Line to be processed
my $file = 'testLog.txt';       #Path to log file
my @log = ();                        #Stores lines from log file

# Session Data Array as global (to avoid un-neccesary processing when passed into function)
our @sessions = (
    # Session Data Holding Variables
    {'clientIPAddress' => 'null',    #Address assigned from IP pool
     'currentCode' => 'Initialiser', #Current IKE code being processed
     'connectDate' => 'null',        #Date of connection to access point
     'connectTime' => 'null',        #Time of connection to access point
     'userID' => 'null',             #Username unique to user
     'authenticationTime' => 'null', #Time user's has been authenticated
     'groupID' => 'null',            #VPN group user belongs to (usually Roamnet)
     'platformType' => 'null',       #Platform user is running Windows, Unix etc..
     'clientVersion' => 'null',      #Version of VPN client
     'vpnPoolAddress' => 'null',     #IP address assigned from VPN pool
     'disconnectTime' => 'null',     #Time session was disconnected
     'duration' => 'null',           #Duration of session
     'bytesTx' => 'null',            #Bytes Transmitted
     'bytesRx' => 'null',            #Bytes Recieved
     'reasonForDisconnect' => 'null',#Reason for connection termination
     'errorMessages' => 'null',      #Additional error messages
     'state' => 'Disconnected'       #State of session
   }
);


#=== Get data from file ===
open(LOG, $file) or die "Log file not found";    #Open file under the handle LOG
@log=<LOG>;          #Read file into @log
close(LOG);          #Close the log file

foreach (@log) {
 $line = $_;
  if($line eq 'empty'){                            #Check to see if line is empty
      print 'Warning Line is Empty <BR>\n';
  }else{
  #===Process line ===
  chomp($line);       #Removes new line character from entry


  process_log($line);                    #Call function to precess log line <--------- line 54
  }

}
line 124
Code:
sub process_log { #Script to parse logs

 #== Parameters passed into function ==
 #$line (@_) line being processed

 #== Declare Variables ==
 my $currentCode = 'null';                          #Code being processed
 my $currentIP = 'null';                            #IP of line being processed
 my $currentUserID = 'null';                        #UserID of line being processed
 my $numberOfSessions = 'null';                     #Number of sessions in the array
 my $data = 'null';                                 #Stores result of regex expressions
 my $currentLine = 'null';                          #Whole Line currently being processed
 my @sepearatedLine = ();                                #Seperated line
 my $sessionExists = 'false';                       #Boolean check for Does session exist

 #=== Store elements from line ===
 $currentLine = shift;                              #Get line passed into function
 @seperatedLine = split(/\s/, $currentLine);        #Split line based on empty space delimitation

 $currentLine =~ m/SEV=\d (.*) RPT/;                #Extract current code from line
 $currentCode = $1;                                 #Get current code
 $currentIP = $seperatedLine[10];                   #Get IP from line
 $numberOfSessions = scalar (@sessions);            #Get number of elements sessions array



 # IKE/DBG64
 if ($currentCode eq 'IKEDBG/64'){
      for (my $i=1; $i <= $numberOfSessions; $i++){               #Check to see if session already exists
        my $sessionIP = $sessions[$i]{"clientIPAddress"};         #Get IP for selected session

        if ($currentIP eq $sessionIP){                            #Compare IP
        #-Session Exists-
          $sessionExists = 'true';
          #Check for illegal connections (session not terminated correctly)
          my $sessionState = $sessions[$i]{"state"};              #Get current state of session
                    if ($sessionState eq 'Disconnected'){         <---------------------------------- line 124
           #--Legal Session--
           $sessions[$i]{"state"} = 'Connecting';                 #Progress state to connecting
           last;                                                  #Break out of loop
           }else{
           #--Illegal Session--
           if ($sessions[$i]{"errorMessages"} eq 'null'){            #Check if there are any error messages
             $sessions[$i]{"errorMessages"} = 'Errors: ';            #Add title
           }
           #Error Message
           $sessions[$i]{"reasonForDisconnect"} = 'Session was not teminated correctly and has been termintated by parser';
           save_session($i);                                       #Call save_session function

           splice(@sessions,$i,1);                                 #Delete the current session
           #Create new session
           create_new_session($i);
           $sessions[$i]{"clientIPAddress"} = $seperatedLine[10];  #Used as key for session
           $sessions[$i]{'currentCode'} = $currentCode;            #Store current code for debugging
           $sessions[$i]{"connectDate"} = $seperatedLine[5];
           $sessions[$i]{"connectTime"} = $seperatedLine[6];
           last;                                                                  #Break out of loop
          }
        }
      }
      if ($sessionExists eq 'false') {
        #-Session doesn't exist-
          #Create new session
          create_new_session($numberOfSessions);                                 #As the array starts from 0, $numberOfSessions will
                                                                                 #always be greater than the index for the last session.
          $sessions[$numberOfSessions]{"clientIPAddress"} =  $seperatedLine[10]; #Used as key for session
          $sessions[$numberOfSessions]{'currentCode'} = $currentCode;            #Store current code
          $sessions[$numberOfSessions]{"connectDate"} = $seperatedLine[5];
          $sessions[$numberOfSessions]{"connectTime"} = $seperatedLine[6];
      }

 }

function create_new_session
Code:
sub create_new_session {            #Script to create a blank session

 my $sessionID = shift;                                  #Session ID
 # Create empty session
 $sessions[$sessionID]{'clientIPAddress'} = 'null';    #Address assigned from IP pool
 $sessions[$sessionID]{'currentCode'} = 'null';        #Current IKE code being processed
 $sessions[$sessionID]{'connectDate'} = 'null';        #Date of connection to access point
 $sessions[$sessionID]{'connectTime'} = 'null';        #Time of connection to access point
 $sessions[$sessionID]{'userID'} = 'null';             #Username unique to user
 $sessions[$sessionID]{'authenticationDate'} = 'null'; #Date user's has been authenticated
 $sessions[$sessionID]{'authenticationTime'} = 'null'; #Time user's has been authenticated
 $sessions[$sessionID]{'groupID'} = 'null';            #VPN group user belongs to (usually Roamnet)
 $sessions[$sessionID]{'platformType'} = 'null';       #Platform user is running Windows, Unix etc..
 $sessions[$sessionID]{'clientVersion'} = 'null';      #Version of VPN client
 $sessions[$sessionID]{'vpnPoolAddress'} = 'null';     #IP address assigned from VPN pool
 $sessions[$sessionID]{'disconnectTime'} = 'null';     #Time session was disconnected
 $sessions[$sessionID]{'duration'} = 'null';           #Duration of session
 $sessions[$sessionID]{'bytesTx'} = 'null';            #Bytes Transmitted
 $sessions[$sessionID]{'bytesRx'} = 'null';            #Bytes Recieved
 $sessions[$sessionID]{'reasonForDisconnect'} = 'null';#Reason for connection termination
 $sessions[$sessionID]{'errorMessages'} = 'null';      #Additional error messages
 $sessions[$sessionID]{'state'} = 'Disconnected';      #State of session
} #End Create empty session

Any help would be much appreciated as i am out of ideas
 
Last edited:
Hi,

From what I remember (it's been a while since I did Perl) lists in Perl are zero-based. In your loop, you're starting $i off at 1 and progressing while it is less than or equal to the number of items in the list:

Code:
for (my $i=1; $i <= $numberOfSessions; $i++){

I'm wondering if this means that the last time round the loop the session it's that is being referred to will be undefined so you'll get "undefined" messages each time you try to access anything using that element in the array.

Try starting the for loop at 0 and looping while $i<$numberOfSessions and see if that works. Hope it does!

Jim
 
OK. But I think the loop should only execute whilst $i<$numberOfSessions.

As $numberOfSessions is equal to the length of the @sessions list the index of the last element in the array will be $numberOfSessions - 1.

Jim
 
Back
Top Bottom