Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

IP-Stored in file not working

by Rahul Gupta (Sexton)
on Jun 06, 2013 at 12:22 UTC ( [id://1037446]=perlquestion: print w/replies, xml ) Need Help??

Rahul Gupta has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

i am capturing ip-address from windows machine and writing in a file in loop. in every iteration a one new ip-address will be assigned to windows that i have to append in the same file and check the ip-address already in the file they are also available in windows system(using ipconfig command).

for that i have written a script in perl. i am able to write the all ip-address but they are coming in this manner.

UEH4_ip = 11.0.0.1 UEH4_ip = 11.0.0.2 UEH4_ip = 11.0.0.3 UEH4_ip = 11.0.0.4
but it should be
UEH1_ip = 11.0.0.1 UEH2_ip = 11.0.0.2 UEH3_ip = 11.0.0.3 UEH4_ip = 11.0.0.4
and the script is
for (my $ii = 1; $ii <= 4; $ii++) { my @FileContents = `ipconfig`; my @UEinfo_fileContents = ReadFile($ue_file); my $FH = WriteFile($ue_file); my $lte_ip_address; foreach (@FileContents) { if ( $_ =~ /Address(.*):\s+([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} +\.[0-9]{1,3})/i ) { my $ip_address = $2; $ip_address =~ s/\s+//gi; if ($ip_address =~ m/^11\.\d+\.\d+\.\d+/) { $lte_ip_address = $ip_address; foreach (@UEinfo_fileContents) { if ($_ =~ m/UEH\d+(.*)=\s+([0-9]{1,3}\.[0-9]{1,3}\ +.[0-9]{1,3}\.[0-9]{1,3})/i) { my $file_ip = $2; if ($file_ip =~ m/^11\.\d+\.\d+\.\d+/) { my $abc = "UEH".$ii . "_ip"; if ($ip_address =~ m/$file_ip/) { print $FH "$abc" . " = $lte_ip_address +\n"; last; } else{ print $FH "$abc" . " = $lte_ip_address +\n"; last; } } } } } } } undef @UEinfo_fileContents; @UEinfo_fileContents = ReadFile($ue_file); foreach (@UEinfo_fileContents) { if ($_ =~ m/UEH\d+(.*)=\s+([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\ +.[0-9]{1,3})/i) { my $file_ip = $2; if ($file_ip =~ m/^11\.\d+\.\d+\.\d+/) { my $temp = 0; foreach (@FileContents) { if ( $_ =~ /Address(.*):\s+([0-9]{1,3}\.[0-9]{1,3} +\.[0-9]{1,3}\.[0-9]{1,3})/i ) { my $IP_address = $2; $IP_address =~ s/\s+//gi; if ($IP_address =~ m/^11\.\d+\.\d+\.\d+/) { if ($file_ip =~ m/$IP_address/) { $temp = 1; last; } } } } if ($temp == 0){ print "$file_ip : Disconnect\n"; } } } } }
please suggest me the way.. thanks in adavnce

Replies are listed 'Best First'.
Re: IP-Stored in file not working
by choroba (Cardinal) on Jun 06, 2013 at 12:29 UTC
    I am not able to run your code, because the subs ReadFile and WriteFile are not provided (see Short, Self Contained, Correct Example). Therefore, I am just guessing: It seems you are overwriting the output file for each iteration of the $ii. The output you mention is the output of the last iteration. You probably do not want to iterate over all the arrays, for one of them you rather need to use its $array[$ii] value. I do not understand the code enough to tell you which one.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: IP-Stored in file not working
by hdb (Monsignor) on Jun 06, 2013 at 13:09 UTC

    I think Choroba is right but you can fix it.

    # capture number as well, additional group: if ($_ =~ m/UEH(\d+)(.*)=\s+([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0- +9]{1,3})/i) { my $number = $1; my $file_ip = $3; # ip now third group! if ($file_ip =~ m/^11\.\d+\.\d+\.\d+/) { my $abc = "UEH".$number . "_ip"; # use $number instead of $ +ii

    I cannot test it but you should be able to...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1037446]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found