Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^11: Lanman Netshare

by bulk88 (Priest)
on Oct 03, 2012 at 05:43 UTC ( #996983=note: print w/ replies, xml ) Need Help??


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

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.


Comment on Re^11: Lanman Netshare
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (12)
As of 2014-10-21 09:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (99 votes), past polls