Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^9: Lanman Netshare

by Anonymous Monk
on Oct 01, 2012 at 11:39 UTC ( #996625=note: print w/ replies, xml ) Need Help??


in reply to Re^8: Lanman Netshare
in thread Lanman Netshare

But is'nt there any module in perl which can help in checking connection to a share? The net view command gives me a lot of information about the share which i do not want ..


Comment on Re^9: Lanman Netshare
Re^10: Lanman Netshare
by Anonymous Monk on Oct 01, 2012 at 18:19 UTC
      Here you go. Tested on 32 bit Windows. Should work on x64 but untested.
      use Data::Dumper; use Win32::API 0.71 qw( SafeReadWideCString ); use Encode; use Carp; use Config; BEGIN{ sub NERR_Success () { 0 } sub MAX_PREFERRED_LENGTH () { 0xFFFFFFFF } eval ' sub POINTER () { "' .($Config{ptrsize} == 8 ? 'Q' : 'L'). '" }'; } sub NetStatusOK { if($_[0] != NERR_Success){ $Carp::CarpLevel = 1; croak("NETAPI call failed with $_[0]"); } return $_[0]; } Win32::API::Type->typedef('NET_API_STATUS' , 'DWORD'); my $NetShareEnum = Win32::API::More->new('Netapi32.dll', 'NET_API_STAT +US NetShareEnum( LPWSTR servername, DWORD level, LPHANDLE bufptr, DWORD prefmaxlen, LPDWORD entriesread, LPDWORD totalentries, LPDWORD resume_handle )'); die "NetShareEnum couldn't be created: $^E" if !$NetShareEnum; { my $NetApiBufferFree = Win32::API::More->new('Netapi32.dll', 'NET_API_STATUS NetApiBufferFree(HANDLE Bu +ffer)'); die "NetApiBufferFree couldn't be created: $^E" if !$NetApiBufferF +ree; sub NetApiBufferFree { NetStatusOK($NetApiBufferFree->Call($_[0])); } } my($bufferptr, $entriesread, $totalentries) = (0,0,0); NetStatusOK($NetShareEnum->Call(Encode::encode('UTF-16LE','\\\\127.0.0 +.1'."\x00"), 1, $bufferptr, MAX_PREFERRED_LENGTH, $entriesread, $totalentries, undef)); die "no entries from NetShareEnum " if ! $entriesread; my @PACKED_SHARE_INFO_1 = unpack('(a['.POINTER.'Lx!['.POINTER.']'.POIN +TER.'])['.$entriesread.']', unpack('P[('.POINTER.'Lx!['.POINTER.']'.PO +INTER.')['.$entriesread.']]', pack(POINTER, $bufferptr))); my @SHARE_INFO; for(@PACKED_SHARE_INFO_1){ my %SHARE_INFO_1; ($SHARE_INFO_1{'shi1_netname'}, $SHARE_INFO_1{'shi1_type'}, $SHARE +_INFO_1{'shi1_remark'}) = unpack(POINTER.'Lx!['.POINTER.']'.POINTER, $_); $SHARE_INFO_1{'shi1_netname'} = SafeReadWideCString($SHARE_INFO_1{ +'shi1_netname'}); $SHARE_INFO_1{'shi1_remark'} = SafeReadWideCString($SHARE_INFO_1{' +shi1_remark'}); push(@SHARE_INFO, \%SHARE_INFO_1); } NetApiBufferFree($bufferptr); print Dumper([$buffer, $bufferptr, $entriesread, $totalentries]); print Dumper(\@SHARE_INFO);
      Output
      $VAR1 = [ undef, 1420904, 4, 4 ]; $VAR1 = [ { "shi1_type" => "2147483651", "shi1_remark" => "Remote IPC", "shi1_netname" => "IPC\$" }, { "shi1_type" => 0, "shi1_remark" => "C", "shi1_netname" => "C" }, { "shi1_type" => "2147483648", "shi1_remark" => "Remote Admin", "shi1_netname" => "ADMIN\$" }, { "shi1_type" => "2147483648", "shi1_remark" => "Default share", "shi1_netname" => "C\$" } ];
      This code does exactly what "net view" does BTW.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://996625]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2015-07-04 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls