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

I googled and got jim@watersheep.org's nbtstat.pl. I am running perl 5.8.5-7 and it hangs when it gets to '$sock->recv($data,$buffsize);' when I pass the script a non-windows IP, as if it is ignoring the timeout. Does anyone have an idea why?
use IO::Socket; . . . $sock = new IO::Socket::INET ( PeerAddr => $host, PeerPort => $port, Proto => $proto, Timeout => $timeout); if (! $sock) { die "Socket not opened: $!\n"; } $sock->send($request); $sock->recv($data,$buffsize); $sock->close;
Thanks, Steve

Replies are listed 'Best First'.
Re: nbtstat.pl fails for non-windows IP
by ikegami (Patriarch) on Oct 05, 2005 at 21:08 UTC

    I tracked it down to an attempt to put the socket in non-blocking mode in IO::Socket:

    $blocking = $sock->blocking(0) if $timeout;

    blocking returns undef. (But $! is not set. Does that mean that switching to non-blocking didn't fail? The docs define failure as returning undef and setting $!.) The timeout mechanism relies on using non-blocking mode, and the results are consistent on this method failing or the following connect ignoring the non-blocking mode.

    I wish I could go deeper -- does Perl simply ignore non-blocking on sockets in Windows, or does Windows not support it -- but I'm not sure how.

    Update: Here's a cleaned up version of the program I used to test my assertions:

    use strict; use warnings; use IO::Socket; sub show_blocking_result { my ($blocking) = @_; die("Error switching to non-blocking: $!\n") if not defined $blocking; printf("Was previously: %s\n", ($blocking ? "blocking" : "non-blocking"), ); } my $sock = new IO::Socket::INET( PeerAddr => "www.perlmonks.org", PeerPort => 80, Proto => 'tcp', ); die("Error connecting: $!\n") if not defined $sock; # We didn't turn off blocking mode, # so the following should return true. show_blocking_result($sock->blocking(0)); # Should return false, but presumably defined. show_blocking_result($sock->blocking(1)); __END__ Windows ------- Error switching to non-blocking: FreeBSD ------- Was previously: blocking Was previously: non-blocking
Re: nbtstat.pl fails for non-windows IP
by NetWallah (Canon) on Oct 06, 2005 at 04:58 UTC
    Have you considered using Net::NBName ?

    I have a version with minor modifications that enables Timeouts as in:

    my $NBnameOBJ = Net::NBName->new(.5); # For Netbios Name resolution, 5 +00ms Timeout
    If interested in this version, /msg me.

         "Man cannot live by bread alone...
             He'd better have some goat cheese and wine to go with it!"

      I gave Net::NBName a whirl and it works great, thanks for the tip. Steve
Re: nbtstat.pl fails for non-windows IP
by doctor_moron (Scribe) on Oct 06, 2005 at 03:56 UTC
    Hi, honestly i dont really understand your question, and im afraid if your question's misinterpreted..

    Firstly, i think there's no such a thing like non windows-IP, maybe you mean non-windows (microsoft) OS ?

    I tried that script from my client( 192.168.0.2,linux SuSe) and my destination host is 192.168.0.1 (win xp sp2), the first try, script looks like got hang, so i look closer at my ethereal, and i can say that 'the packet netbios query' is sent successfully, but no response from 192.168.0.1.
    Then i turn off my xp sp2 firewall, try the script again, and i got the response (success !), here's the log (only the summary) :

    Got something back: Total of 6 replies LJWT-1C2E2BF3 <0x00> <0x04> UNIQUE Workstation Service WORKGROUP <0x00> <0x84> UNIQUE Workstation Service LJWT-1C2E2BF3 <0x20> <0x04> UNIQUE File Server Service WORKGROUP <0x1E> <0x84> UNIQUE WORKGROUP <0x1D> <0x04> UNIQUE Master Browser __MSBROWSE__ <0x01> <0x84> UNIQUE Messenger Service Mac address: 00:xx:xx:xx

    But, i am just curious, are you trying to nbtstat non windows OS, like linux box ? i mean are you trying to do something like this :

    perl nbtstat.pl <linux box> ?

    As far as i know NETBIOS is only (designed) for microsoft windows, but i am just not sure, maybe this answer is just another question..
    But i am sure that this script is designed for microsoft windows as destination address