Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is this perl code doing, 2

Status
Not open for further replies.
Nov 24, 2004
159
GB
What is this line of perl doing?
Code:
 print "$who -> $1\n" if $lookup=~m{Name:\s+(.+)}i;

I need to modify the script below to replace any error with the original IP

Code:
#!/usr/bin/perl
use strict;
use warnings;
my ( $base, $subnet );
sub probe {
    my ($who) = @_;
    my $lookup = `nslookup $who`;
    print "$who -> $1\n" if $lookup=~m{Name:\s+(.+)}i; 
}
while (<>) {
    chomp;
    s/\|/ /g;
    my @F = split /]/;
    for my $range (@F) {
        $range =~ s/^\s+//;
        $range =~ s/\s+$//;
        if ( $range =~ m{ (\d+\.\d+\.\d+\.\d+) }x ) {
            probe($range);
            next;
        }
        ( $base, $subnet ) = $range =~ m{ (\d+\.\d+\.\d+\.) \[ (.+) }x;
        my @FF = split / /, $subnet;
        for my $node (@FF) {
            probe( $base . $node );
        }
    }
}
1;
 
ok why will this not work

Code:
print "$who -> $1\n" if $lookup=~m{Name:\s+(.+)}i; elsif $lookup=$who;
 
Hi

coopermarsh said:
ok why will this not work
Code:
print "$who -> $1\n" if $lookup=~m{Name:\s+(.+)}i; elsif $lookup=$who;
Invalid syntax.
man perlsyn said:
Any simple statement may optionally be followed by a SINGLE modifier, just before the terminating semicolon (or block ending). The possible modifiers are:

[tt] 1. if EXPR
2. unless EXPR
3. while EXPR
4. until EXPR
5. foreach LIST[/tt]
( man perlsyn | StatementModifiers )

Feherke.
 
Maybe
Perl:
sub probe {
    my ($who) = @_;
    my $lookup = `nslookup $who`;
    my $resolve = $lookup =~ /Name:\s+(.+)/i ? $1 : $lookup;
    print "$who -> $resolve\n";
}
? This either prints the resolved name from the nslookup, or the original unresolved address, which looks like the kind of thing you want...


Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks for all your help.

Working exactly as i want it know.

Changed the

$1 : $lookup;

to

$1 : $who;

here is the final code

Code:
#!/usr/bin/perl
use strict;
use warnings;
my ( $base, $subnet );
sub probe {    my ($who) = @_;    
my $lookup = `nslookup $who`;    
my $resolve = $lookup =~ /Name:\s+(.+)/i ? $1 : $who;    
print "$resolve $who\n";
}
while (<>) {
chomp;
s/\|/ /g;
my @F = split /]/;
for my $range (@F) {
$range =~ s/^\s+//;
$range =~ s/\s+$//;
if ( $range =~ m{ (\d+\.\d+\.\d+\.\d+) }x ) {
probe($range);
next;
}
( $base, $subnet ) = $range =~ m{ (\d+\.\d+\.\d+\.) \[ (.+) }x;
my @FF = split / /, $subnet;
for my $node (@FF) {
probe( $base . $node );
}
}
}
1;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top