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.


In reply to (Ovid - not printing) Re: gethostbyaddr by Ovid
in thread gethostbyaddr question by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.