#!perl
# The Pattern is the string to check for from the
# ipconfig/all output (note if you wanted to retain that
# extra space at the beginning of the line like in your
# example code, then remove the space after the : below
$pattern = "IP Address. . . . . . . . . . . . : ";
# The CRflag is used to ensure that no CR happens at the
# end of the file. The logic goes... print the IP address
# set the flag. Net time you print an IP address if the
# flag is set, then print a CR first.
$CRflag = "FALSE";
# The command is obviously to create the first text file
# that gets parsed to find the IP address. There may be a
# Perl function to do this directly but I haven't gone
# hunting for it. This works fine. Note you could change
# temp.txt to c:\temp\temp.txt if you wanted.
@command = ("ipconfig/all > temp.txt");
system(@command) == 0 or die "Couldn't find IP information";
# IPList.txt is the file produced containing the IP addresses.
open (FILE1, "< temp.txt") or die "Cant open temporary IP Config file";
open (FILE2, "> IPList.txt") or die "Cant open temporary IP output file";
# This loop reads the file in looking for the pattern above and
# when it finds it, spits the result out to the output file
# The CRflag logic is described above and ensures no final
# carriage return at the end of the file.
while () {
if (/($pattern)(.*)(\n)/) {
if ($CRflag eq "FALSE") {
$CRflag = "TRUE";
} else {
print FILE2 "\n";
} print FILE2 "$2";
}
}
# Close up to finish off.
close FILE1;
close FILE2;