Actually,

Yes, it does work. I used it today at work to convert about 800 private ip's to their hostnames.

Just have to make sure that your file containing ip's is in the same directory as the perl program.

I'm very new to perl and was amazed myself that it actually worked! LoL.



Thanks,

-phil


On Nov 1, 2007, at 10:17 PM, Rob Dixon wrote:

Phillip Gonzalez wrote:
Hi,
I'm trying to print stdout to a file, then switch back to the default standard out so that it prints to the screen. This script takes a list of ip's and does a reverse lookup on them, it then saves the output to "reverse.txt".
Here's my code:
#!/usr/bin/perl
use strict;
use warnings;
print "Enter the file you want to perform a reverse lookup on:\n";
my $file_open = <STDIN>;
        open (STDOUT,">reverse.txt");
open(DEVFILE, "$file_open") or die "Can't open file dude!: $!";
        my @dev_ip=<DEVFILE>;
        foreach my $ip(@dev_ip) {
        system("host -W 1 $ip")
        }
        close(DEVFILE);
        close(STDOUT);
print "Your ouput has been saves as reverse.txt in the current directory\n"; ###### I want to print this line back to STDOUT but since I redirected it above with "open (STDOUT,">reverse.txt") I can't figure out how to.

Hi Phil

I don't think this program works as it should anyway does it? You haven't removed the trailing newlines from your input from STDIN (so the open on
DEVFILE will probably fail) or from DEVFILE (so the host command won't
work). Also the call to system() won't write anything to the file you've
opened as it's executed in a separate process: you need to capture the
output from host and print it from within the Perl program.

The way to avoid having to redirect back to STDOUT is not to use it in
the first place. I think the program below does something like what you
want, although it's untested as I don't have the necessary platform
available.

HTH,

Rob


use strict;
use warnings;

print "Enter the file you want to perform a reverse lookup on: ";

my $file_open = <STDIN>;
chomp $file_open;
open DEVFILE, '<', $file_open or die "Can't open file \"$file_open \" for input: $!";

open REVERSE, '>', 'reverse.txt' or die "Can't open output file: $!";

while (my $ip = <DEVFILE>) {
 chomp $ip;
 print REVERSE qx(host -W 1 $ip);
}

close REVERSE;

close DEVFILE;

print "Your ouput has been saved as reverse.txt in the current directory\n";


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to