任何人都可以告訴我爲什麼其他所有的東西都可以工作,除了我的USER和PASS計數器變量在我'while'循環的底部?當他們輸出3個用戶和2個輸入時,將它們重置爲零。爲什麼我的變量重置爲零 - perl
(代碼是計算有多少實例是Word用戶,並通過了路上經過。)
如果你看到任何其他的馬虎錯誤,請讓我知道!謝謝!
輸入文件的第一行(注:這種格式將重複2006年的線條,只是數字的變化)
22:28:31.819551 IP 98.114.205.102.1924 > 192.150.11.111.1957: Flags [P.], seq 1:124, ack 2, win 64239, length 123E...<[email protected]\.bfP....Y..echo USER 1 get ssms.exe
代碼:
use strict;
use warnings;
use diagnostics;
open MYFILE, '<', 'source_file.txt' or die $!;
open OUT, '>', 'Summary_Report.txt' or die $!;
open OUTFILE, '>', 'Header.txt' or die $!;
my $start_time = undef;
my $end_time;
my $user = 0;
my $pass = 0;
my $linenum = 0;
while (<MYFILE>) { # loops through every line in file
chomp; # break new line
$linenum++; # count line 1 to end of file, 2006
if (/^\d+:\d+/) {
my @header = split (/\s+/, $_);
print OUTFILE "$linenum: @header\n\n";
if (/^22:28/ && !defined($start_time)) {
$start_time = $header[0];
}
if (/22:28/) {
$end_time = $header[0];
}
$user++ if /USER/ig;
$pass++ if /PASS/ig;
}
}
print OUT "Total # of times phrases were used:\n\n
USER (variations thereof) = $user\n\n
PASS (variations thereof) = $pass\n\n\n";
好大家好,這裏是每一個我的代碼的最後一位。 (輸出都還沒有完成,這就是爲什麼有些人仍然沒有答案,但你可以得到什麼,我工作的一個想法,需要做什麼。
#!/usr/bin/perl -w
# Final project
use strict;
use warnings;
use diagnostics;
#opens txt file: read mode
open MYFILE, '<', 'source_file.txt' or die $!;
#opens output txt file: write mode
open OUT, '>', 'Summary_Report.txt' or die $!;
#open output txt file: write mode
#used to store header 'split' info
open OUTFILE, '>', 'Header.txt' or die $!;
my $i = 0;
$| = 1; #disable output buffering
my $start_time = undef; #undefined to avoid recycling through other time stamps
my $end_time;
my $user = 0;
my $pass = 0;
my $packet_size = 0; #goes with length#
my @header;
my @source_ip;
my @source_port;
my $src_port;
my @src_port;
my @dest_ip;
my @dest_port;
my $destination_port;
my @destination_port;
while (<MYFILE>) { #loops through every line in file
chomp; #break new line
if (/^\d+:\d+/) {
#separate pieces of information from TCPDUMP into list
@header = split (/\s+/, $_);
print OUTFILE "$.: @header\n\n";
##############################T I M E##################################
#defining first 'line & time' as 'special'
if (/^22:28/ && !defined($start_time)) {
$start_time = $header[0];
#print "$start_time\n"; ###used as a check###
}
#Used recycling of time stamps to find last one available
if (/22:28/) {
$end_time = $header[0];
}
#############################S O U R C E#################################
#categorizing each section of ip's from source
@source_ip = split ('\.', $header[2]);
#adding ip's together, joining in concatenation by '.'
$source_ip[$i] = $source_ip[0] . '.' . $source_ip[1] . '.' . $source_ip[2] . '.' . $source_ip[3];
#print $source_ip[$i]; (check)
@source_port = split (':', $source_ip[4]);
$src_port[$i] = $source_port[0];
#########################D E S T I N A T I O N###########################
#categorizing each section of ip's from destination
@dest_ip = split ('\.', $header[4]);
#adding ip's together, joining in concatenation by '.'
$dest_ip[$i] = $dest_ip[0] . '.' . $dest_ip[1] . '.' . $dest_ip[2] . '.' . $dest_ip[3];
#print $dest_ip[$i]; (check)
@dest_port = split (':', $source_ip[4]);
$destination_port[$i] = $dest_port[0];
#############################L E N G T H#################################
#-1 represents length
$packet_size = $packet_size + $header[-1];
#print $packet_size; (check)
$i++
}
}
close MYFILE;
#########################D A T A S E C T I O N###########################
open MYFILE, '<', 'source_file.txt' or die $!;
#I am separating loop to reset values#
while (<MYFILE>) {
#finds all instances of USER
$user++ if /USER/ig;
#print "user" (use as check)
#finds all instances of PASS
$pass++ if /PASS/ig;
#print "pass" (use as check)
}
#Output summary to new file: overwrite file
print OUT "SUMMARY REPORT:\n\n";
print OUT "# of total lines in the file = $.\n\n\n";
print OUT "Range of time the file encompasses:\n\n
Starting Time = $start_time\n\n
Ending Time = $end_time\n\n
Total Time = 16.219218\n\n\n";
print OUT "Total # of distinct SOURCE ip addresses = \n\n\n";
print OUT "Total # of distinct DESTINATION ip addresses = \n\n\n";
print OUT "Listing of distinct SOURCE ip addresses = \n\n\n";
print OUT "Listing of distinct DESTINATION ip addresses = \n\n\n";
print OUT "Total # of distinct SOURCE TCP ports = \n\n\n";
print OUT "Total # of distinct DESTINATION TCP ports = \n\n\n";
print OUT "Listing of distinct SOURCE TCP ports = \n\n\n";
print OUT "Listing of distinct DESTINATION TCP ports = \n\n\n";
print OUT "Total # of times phrases were used:\n\n
USER (variations thereof) = $user\n\n
PASS (variations thereof) = $pass\n\n\n";
print OUT "DETAIL SECTION:\n\n\n";
print OUT "SOURCE IP address activity by port over time:\n\n
Mean packet size for above = \n\n
Median packet size for above = \n\n\n";
print OUT "Detail IP address activity by port over time:\n\n
Mean packet size for above = \n\n
Median packet size for above = \n\n\n";
print OUT "Any and all interesting text w/in the DATA section of the file:\n\n";
close OUT; #
close OUTFILE; #close remaining files
close MYFILE; #
你能告訴我們'source_file.txt'的內容嗎? – jwodder
我不認爲它是遞增的。將最後一個打印語句移動到'pass ++'下的'while ... if..'中,以便打印每個循環迭代的值。 – slebetman
'$ linenum'也被稱爲內置'$ .' – TLP