Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

IP-Stored in file not working

by Rahul Gupta (Sexton)
on Jun 06, 2013 at 12:22 UTC ( #1037446=perlquestion: print w/ replies, xml ) Need Help??
Rahul Gupta has asked for the wisdom of the Perl Monks concerning the following question:


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 = UEH4_ip = UEH4_ip = UEH4_ip =
but it should be
UEH1_ip = UEH2_ip = UEH3_ip = UEH4_ip =
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

Comment on IP-Stored in file not working
Select or Download Code
Replies are listed 'Best First'.
Re: IP-Stored in file not working
by choroba (Canon) 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 (Prior) 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?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2015-11-27 11:18 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (724 votes), past polls