##
#!/usr/bin/perl
print "Content-type: text/html\n\n";
#List14_09
#Read fixed length records from a file
#1 Verify the record length
$NL = "\n";
$DC = sprintf("%c", 236); #Special character
$TW = 63; #Line with
$DL = sprintf( "%63s", $NL . $DC x $TW); #top and bottom lines
$Record = "A3 A20 A20 A20";
$RecordLength = length(pack($Record));
print "$NL Record length: $RecordLength";
#2 Open the data file--read only
open(FILE, "lab8_tcpdump2.txt") || die "\n Cannot access file.";
#3 Set the file at the end (option 2)
seek(FILE, 0 , 2);
# Obtain the length
$FileLength = tell(FILE);
print "$NL The length of the file is $FileLength";
$NumOfRecords = int($FileLength/$RecordLength);
print("$NL $NumOfRecords records are in the file.");
print($DL);
print "$NL>>Press ";
#4 Use seek and read to obtain records by multiplying by length
for($Itr = 0; $Itr < $NumOfRecords; $Itr++){
$Count = $Itr + 1;
$AnyChar = ;
seek(FILE, $Itr * $RecordLength, 0);
read(FILE, $LineRead, $RecordLength, 0);
print(" >> $Count $LineRead");
}
print($DL);
#5 Use seek and record length to find a record for display
seek(FILE, 0, 0);
print("$NL $NL Enter a record number. $NL >>");
$LineNumber = ;
# print "$NL The record length: $RecordLength $NL";
$SeekPoint = ($LineNumber - 1)* $RecordLength;
seek(FILE, $SeekPoint, 0);
read(FILE, $LineRead, $RecordLength);
# print "$NL The line read is: $NL";
print "$LineRead\n";
close(FILE) || "Cannot close file: $!";
print "\n";
print "\n";
<>
## ##
19:39:44.765096 IP 10.195.32.212.49152 > 255.255.255.255.trivnet1: UDP, length 12
19:39:44.765572 IP 10.195.32.212.49152 > 239.0.82.11.trivnet1: UDP, length 12
19:39:45.202568 IP 10.195.32.96.61804 > 255.255.255.255.sentinelsrm: UDP, length 40
19:39:45.265116 IP 10.195.32.212.49152 > 255.255.255.255.trivnet1: UDP, length 12
19:39:45.265590 IP 10.195.32.212.49152 > 239.0.82.11.trivnet1: UDP, length 12
19:39:45.411153 IP 10.195.32.198.netbios-ns > 10.195.32.255.netbios-ns: NBT UDP PACKET(137): QUERY; REQUEST; BROADCAST
19:39:45.412136 IP 10.195.32.198.netbios-dgm > 10.195.32.255.netbios-dgm: NBT UDP PACKET(138)
19:39:45.620442 STP 802.1d, Config, Flags [none], bridge-id 8020.00:22:56:29:3a:00.8014, length 43
19:39:45.765086 IP 10.195.32.212.49152 > 255.255.255.255.trivnet1: UDP, length 12
Note: each lines above ends with LF CTRL char.