Fellow Monks, I'm again in the need of some help. I just can't figure out how to stop the following script from breaking. This script attempts to connect to a host via null session and then dumps out user information for that system. The problem that I'm running into is that when the null session cannot be established it breaks out of the loop, thus dies. I believe its the Win32::Lanman::NetUseAdd(\%Hash) thats causing the problem. I've tried using return in the subroutines several different ways without any luck. Any advise would be greatly appreciated.
     
--Dusty
#!/usr/bin/perl -w #----- # should get local users from a subnet.... # scan.pl xxx.xxx.xxx #---- use strict; use Net::NBName; use Win32::Lanman; my $server; my $subnet = $ARGV[0]; my $nb = Net::NBName->new; my @users; for my $hostbit (2..253) { $server = "$subnet\.$hostbit"; my $username = ""; my $password = ""; my $null = ""; my $ns = $nb->node_status($server); if ($ns) { if (connectipc($server, $password, $username, $null)) {
print "null session to $server successful.\n"; @users = getusers($server); if (@users) { foreach (@users) { my ($group,$user) = split(/:/,$_); print "$user\n"; } } else { print "Did not retrieve local users.\n"; } print "\n"; if (disconnect($server)) { print "Disconnected from $server.\n"; } else { print "Could not disconnect.\n"; } } else { print "failed to connect\n"; } } else { print "$server isn't running netbios\n"; } } #----- # connect to ipc share #---- sub connectipc { my($server,$password,$username,$null) = @_; my(%Hash) = ( remote => "\\\\$server\\ipc\$", asg_type => &USE_IPC, password => $password, username => $username, domainname => $null ); Win32::Lanman::NetUseAdd(\%Hash); } #----- # disconnect ipc connection #---- sub disconnect { my(@server) = @_; Win32::Lanman::NetUseDel("\\\\$server\\ipc\$",&USE_FORCE); } #----- # get local users #---- sub getusers { my($server) = @_; my($err,$group,$member); my(@groups,@members,@users) = (); if(Win32::Lanman::NetLocalGroupEnum("\\\\$server", \@groups)) { foreach $group (@groups) { if(Win32::Lanman::NetLocalGroupGetMembers("\\\\$server", ${$group}{ +'name'}, \@members)) { foreach $member (@members) { push(@users, "${$group}{'name'}:${$member}{'domainandname'}"); } } else { $err = Win32::FormatMessage Win32::Lanman::GetLastError(); $err = Win32::Lanman::GetLastError() if ($err eq ""); print "NetLocalGroupGetMembers error: $err\n"; } } } else { $err = Win32::FormatMessage Win32::Lanman::GetLastError(); $err = Win32::Lanman::GetLastError() if ($err eq ""); print "NetLocalGroupEnum error: $err\n"; } return @users; }

Edit by tye, add READMORE

Edit by jeffa, fixed font tag


In reply to Win32::Lanman Subroutine Hell by draper7

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.