Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

The following code should print to hosts.txt but it is empty. When I comment out the line with gethostbyaddr call then works ok. What could be wrong here. Thanks.
###### use Socket; open (HOST, ">>hosts.txt") || die "can't open hosts\n"; for ($i = 0; $i< 256; $i++) { for ($j = 0; $j< 256; $j++) { for ($k = 0; $k< 256; $k++) { for ($l = 0; $l< 256; $l++) { $iaddr = inet_aton("$i.$j.$k.$l"); # or whatever addre +ss $name = gethostbyaddr($iaddr, AF_INET); chomp $name; print "$i.$j.$k.$l\t$name\n"; print HOST "$i.$j.$k.$l\t$name\n"; } } } } close HOST;
Edited 2001-05-04 by tilly to make the title unique

Replies are listed 'Best First'.
(Ovid - not printing) Re: gethostbyaddr
by Ovid (Cardinal) on May 04, 2001 at 19:43 UTC

    Actually, your code works fine for me. Here's what I suspect is happening: it's taking a long time to run, so you hit control-C or something similar to interrupt. When you do, you discover that the file is empty! I had the same problem. By setting the ranges to smaller values, I was able to let the program finish running. Then, the file was written out just fine!

    Perl is buffering the filehandle. Data does not get written to the file until the buffer is full. What you need to do is either wait for the buffer to be full and the data to be written or unbuffer the handle. I rewrote the snippet and fixed your problem. This could still be improved, but it's a bit cleaner:

    #!/usr/bin/perl -w use strict; use Socket; my $logfile = 'hosts.txt'; open HOST, ">> $logfile" or die "Can't open $logfile: $!\n"; # The following snippet will unbuffer your filehandle # so that data is written out immediately. { my $ofh = select HOST; $| = 1; select $ofh; } for my $i ( 0 .. 255 ) { for my $j ( 0 .. 255 ) { for my $k ( 0 .. 255 ) { for my $l ( 0 .. 255 ) { my $address = "$i.$j.$k.$l"; my $iaddr = inet_aton( $address ); my $name = gethostbyaddr($iaddr, AF_INET) || ''; chomp $name if $name; print "$address\t$name\n"; print HOST "$address\t$name\n" or die "Can't print to +$logfile: $!"; } } } } close HOST or die "Can't close $logfile: $!";

    By the way, if you want to understand what is actually causing the problem, read this.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Thanks a lot, I was hitting Ctrl-c. How long is the buffer supposed to be anyway? Kaushal Shah
        you might want to set the autoflush to true, this will force a buffer flush after your print statements.. Just add a--

        $|=1;

        Under your use Socket

        Good luck!

        UPDATE: Sorry about that ovid I didnt even notice that you had already put a $|=1; in your code.


        .mincus
        telnet://bbs.mincus.com

Re: gethostbyaddr question
by mattr (Curate) on May 07, 2001 at 16:47 UTC
    Something like this conceivably could also happen if you don't have permission to use the gethost commands on your server.. this happened to me recently :(

    checking for null before chomp might be okay too? wouldn't hang though, crash more like it I think.

Re: gethostbyaddr question
by petdance (Parson) on May 07, 2001 at 02:51 UTC
    You're writing in Perl now, not C. Use the Perl way of doing the loops you're using...
    for my $i ( 0..255 ) { for my $j ( 0..255 ) { for my $k ( 0..255 ) { for my $l ( 0..255 ) { } } } }

    xoxo,
    Andy

    %_=split/;/,".;;n;u;e;ot;t;her;c; ".   #   Andy Lester
    'Perl ;@; a;a;j;m;er;y;t;p;n;d;s;o;'.  #   http://petdance.com
    "hack";print map delete$_{$_},split//,q<   andy@petdance.com   >