Jump to content

NetView API


SumTingWong
 Share

Recommended Posts

Inspired by Archival's DomainView script, I wrote this NetView API with the intention of adding it to Larry's AU3Xtra.dll if Larry will have it and people find it useful.

It's basically just a wrapper for NetServerEnum but it's a lot easier to use than spawning the net view command.

EDIT1: OK, DllCall string buffers are limited to 64K so if you have a lot of machines in your domain then the data returned may not fit. I have therefore split the single returned buffer into 3 separate buffers, one for names, one for versions and one for types. See revised example.

I have also added a second method to circumvent this 64K limit. This involves finding all machines using NetViewFind. Enumerating each entry by calling NetViewEnum in a loop. Finally calling NetViewClose to free allocated memory and clean up. Again, see Method 2 example.

Attached the example script for you lazy people.

Anyway, here's an example of how to use this API:

Global Const $SV_TYPE_WORKSTATION = 0x00000001
Global Const $SV_TYPE_SERVER = 0x00000002
Global Const $SV_TYPE_SQLSERVER = 0x00000004
Global Const $SV_TYPE_DOMAIN_CTRL = 0x00000008
Global Const $SV_TYPE_DOMAIN_BAKCTRL = 0x00000010
Global Const $SV_TYPE_TIME_SOURCE = 0x00000020
Global Const $SV_TYPE_AFP = 0x00000040
Global Const $SV_TYPE_NOVELL = 0x00000080
Global Const $SV_TYPE_DOMAIN_MEMBER = 0x00000100
Global Const $SV_TYPE_PRINTQ_SERVER = 0x00000200
Global Const $SV_TYPE_DIALIN_SERVER = 0x00000400
Global Const $SV_TYPE_XENIX_SERVER = 0x00000800
Global Const $SV_TYPE_SERVER_UNIX = $SV_TYPE_XENIX_SERVER
Global Const $SV_TYPE_NT = 0x00001000
Global Const $SV_TYPE_WFW = 0x00002000
Global Const $SV_TYPE_SERVER_MFPN = 0x00004000
Global Const $SV_TYPE_SERVER_NT = 0x00008000
Global Const $SV_TYPE_POTENTIAL_BROWSER = 0x00010000
Global Const $SV_TYPE_BACKUP_BROWSER = 0x00020000
Global Const $SV_TYPE_MASTER_BROWSER = 0x00040000
Global Const $SV_TYPE_DOMAIN_MASTER = 0x00080000
Global Const $SV_TYPE_SERVER_OSF = 0x00100000
Global Const $SV_TYPE_SERVER_VMS = 0x00200000
; Windows95 and above
Global Const $SV_TYPE_WINDOWS = 0x00400000
; Root of a DFS tree
Global Const $SV_TYPE_DFS = 0x00800000
; NT Cluster
Global Const $SV_TYPE_CLUSTER_NT = 0x01000000
; Terminal Server(Hydra)
Global Const $SV_TYPE_TERMINALSERVER = 0x02000000
; NT Cluster Virtual Server Name
Global Const $SV_TYPE_CLUSTER_VS_NT = 0x04000000
; Return local list only
Global Const $SV_TYPE_LOCAL_LIST_ONLY = 0x40000000
Global Const $SV_TYPE_DOMAIN_ENUM = 0x80000000
Global Const $SV_TYPE_ALL = 0xFFFFFFFF

Dim $aDllRet, $aServers, $aVersions, $aTypes, $i, $x

; Method 1 find all machines and return their info as 3 separate strings with each entry
; separated by a @LF character. DllCall string buffers are limited to 64K only

; Function Name:    NetView
; Description:        Enumerate all machines of the specified type that are visible in a domain
; Parameter(s):      Type (long) - machine type
;                    Domain (str) - domain name. NULL for current domain
;                    Servers (str) - returned buffer containing machine names
;                    Versions (str) - returned buffer containing machine versions
;                    Types (str)    - returned buffer containing machine types
; Requirement(s):    None
; Return Value(s):    Number of machines found (long)
; Note:                Buffers are limited to 64K in size by DllCall. 
;                    This roughly equates to a maximum of about 3110 machines 
;                    Each value in the returned buffers is separated by a linefeed (@LF)

$x = TimerInit()
$aDllRet = DllCall("au3xtratest.dll", "long", "NetView", _
                   "long", $SV_TYPE_SERVER_NT, _
                   "int", 0, _ 
                   "str", "", _ 
                   "str", "", _ 
                   "str", "")
If Not @error And $aDllRet[0] > 0 Then
    $aServers = StringSplit($aDllRet[3], @LF)
    $aVersions = StringSplit($aDllRet[4], @LF)
    $aTypes = StringSplit($aDllRet[5], @LF)
    ConsoleWrite("****************" & @LF)
    ConsoleWrite("*** METHOD 1 ***" & @LF)
    ConsoleWrite("****************" & @LF)
    For $i = 1 To $aServers[0]
        ConsoleWrite("Machine Name: " & $aServers[$i] & @LF)
        ConsoleWrite("Machine OS: " & $aVersions[$i] & @LF)
        ConsoleWrite("Machine Type: " & _GetMachineType($aTypes[$i]) & @LF)
    Next
    ConsoleWrite("TOTAL ENTRIES: " & $aDllRet[0] & @LF)
    $x = TimerDiff($x)
    ConsoleWrite("ELAPSED TIME: " & $x & @LF)
    ConsoleWrite("****************" & @LF & @LF)
EndIf

; Method 2 find all machines using NetViewFind. Enumerate each entry by
; calling NetViewEnum in a loop. Call NetViewClose to free allocated memory
; and clean up

; Function Name:    NetViewFind
; Description:        Find all machines of the specified type that are visible in a domain
; Parameter(s):        Type (long) - machine type
;                    Domain (str) - domain name. "int", 0 for current domain
; Return Value(s):    Number of machines found (long)

; Function Name:    NetViewEnum
; Description:        Enumerate machines found using NetViewFind
; Parameter(s):        Server (str) - machine name
;                    Version (str) - machine version
;                    Types (long_ptr) - machine type
; Return Value(s):    Number of machines left to enumerate (long)

; Function Name:    NetViewClose
; Description:        Free machine list buffer and clean up
; Parameter(s):        None
; Return Value(s):    None

Dim $hAU3Xtra

$x = TimerInit()
$hAU3Xtra = DllOpen("au3xtratest.dll")
If $hAU3Xtra > -1 Then
    $aDllRet = DllCall($hAU3Xtra, "long", "NetViewFind", _ 
                       "long", $SV_TYPE_SERVER_NT, _ 
                       "int", 0)
    If Not @error And $aDllRet[0] > 0 Then
        ConsoleWrite("****************" & @LF)
        ConsoleWrite("*** METHOD 2 ***" & @LF)
        ConsoleWrite("****************" & @LF)
        $i = 0
        While $aDllRet[0] > 0
            $aDllRet = DllCall($hAU3Xtra, "long", "NetViewEnum", _ 
                               "str", "", _ 
                               "str", "", _ 
                               "long_ptr", 0)
            If @error Then 
                MsgBox(262144,'debug line ~89' , '@error:' & @lf & @error);### Debug MSGBOX                
                ExitLoop
            EndIf
            ConsoleWrite("Machine Name: " & $aDllRet[1] & @LF)
            ConsoleWrite("Machine OS: " & $aDllRet[2] & @LF)
            ConsoleWrite("Machine Type: " & _GetMachineType($aDllRet[3]) & @LF)
            $i = $i+1
        WEnd
        ConsoleWrite("TOTAL ENTRIES: " & $i & @LF)
        $x = TimerDiff($x)
        ConsoleWrite("ELAPSED TIME: " & $x & @LF)
        ConsoleWrite("****************" & @LF)
    EndIf
    DllCall($hAU3Xtra, "none", "NetViewClose")
    DllClose($hAU3Xtra)
EndIf

Func _GetMachineType($nTypeID)
    Local $sMachineType = ""
    
    If BitAND($nTypeID, $SV_TYPE_WORKSTATION) Then _ 
        $sMachineType = $sMachineType & "A LAN Manager workstation/"
    If BitAND($nTypeID, $SV_TYPE_SERVER) Then _ 
        $sMachineType = $sMachineType & "A LAN Manager server/"
    If BitAND($nTypeID, $SV_TYPE_SQLSERVER) Then _ 
        $sMachineType = $sMachineType & "Any server running with Microsoft SQL Server/"
    If BitAND($nTypeID, $SV_TYPE_DOMAIN_CTRL) Then _ 
        $sMachineType = $sMachineType & "Primary domain controller/"
    If BitAND($nTypeID, $SV_TYPE_DOMAIN_BAKCTRL) Then _ 
        $sMachineType = $sMachineType & "Backup domain controller/" 
    If BitAND($nTypeID, $SV_TYPE_TIME_SOURCE) Then _ 
        $sMachineType = $sMachineType & "Server running the Timesource service/"
    If BitAND($nTypeID, $SV_TYPE_AFP) Then _ 
        $sMachineType = $sMachineType & "Apple File Protocol server/"
    If BitAND($nTypeID, $SV_TYPE_NOVELL) Then _ 
        $sMachineType = $sMachineType & "Novell server/"
    If BitAND($nTypeID, $SV_TYPE_DOMAIN_MEMBER) Then _ 
        $sMachineType = $sMachineType & "LAN Manager 2.x domain member/"
    If BitAND($nTypeID, $SV_TYPE_LOCAL_LIST_ONLY) Then _ 
        $sMachineType = $sMachineType & "Servers maintained by the browser/"
    If BitAND($nTypeID, $SV_TYPE_PRINTQ_SERVER) Then _ 
        $sMachineType = $sMachineType & "Server sharing print queue/"
    If BitAND($nTypeID, $SV_TYPE_DIALIN_SERVER) Then _ 
        $sMachineType = $sMachineType & "Server running dial-in service/"
    If BitAND($nTypeID, $SV_TYPE_XENIX_SERVER) Then _ 
        $sMachineType = $sMachineType & "Xenix server/"
    If BitAND($nTypeID, $SV_TYPE_SERVER_MFPN) Then _ 
        $sMachineType = $sMachineType & "Microsoft File and Print for NetWare/"
    If BitAND($nTypeID, $SV_TYPE_NT) Then _ 
        $sMachineType = $sMachineType & "Windows Server 2003, Windows XP, Windows 2000, or Windows NT/"
    If BitAND($nTypeID, $SV_TYPE_WFW) Then _ 
        $sMachineType = $sMachineType & "Server running Windows for Workgroups/"
    If BitAND($nTypeID, $SV_TYPE_SERVER_NT) Then _ 
        $sMachineType = $sMachineType & "Windows Server 2003, Windows 2000 server, or Windows NT server that is not a domain controller/"
    If BitAND($nTypeID, $SV_TYPE_POTENTIAL_BROWSER) Then _ 
        $sMachineType = $sMachineType & "Server that can run the browser service/"
    If BitAND($nTypeID, $SV_TYPE_BACKUP_BROWSER) Then _ 
        $sMachineType = $sMachineType & "Server running a browser service as backup/"
    If BitAND($nTypeID, $SV_TYPE_MASTER_BROWSER) Then _ 
        $sMachineType = $sMachineType & "Server running the master browser service/"
    If BitAND($nTypeID, $SV_TYPE_DOMAIN_MASTER) Then _ 
        $sMachineType = $sMachineType & "Server running the domain master browser/"
    If BitAND($nTypeID, $SV_TYPE_DOMAIN_ENUM) Then _ 
        $sMachineType = $sMachineType & "Primary domain/"
    If BitAND($nTypeID, $SV_TYPE_WINDOWS) Then _ 
        $sMachineType = $sMachineType & "Windows Me, Windows 98, or Windows 95/"
    If BitAND($nTypeID, $SV_TYPE_ALL) Then _ 
        $sMachineType = $sMachineType & "All servers/"
    If BitAND($nTypeID, $SV_TYPE_TERMINALSERVER) Then _ 
        $sMachineType = $sMachineType & "Terminal Server/"
    If BitAND($nTypeID, $SV_TYPE_CLUSTER_NT) Then _ 
        $sMachineType = $sMachineType & "Server clusters available in the domain/"
    If BitAND($nTypeID, $SV_TYPE_CLUSTER_VS_NT) Then _ 
        $sMachineType = $sMachineType & "Cluster virtual servers available in the domain/"
    If StringRight($sMachineType, 1) = "/" Then
        Return StringTrimRight($sMachineType, 1)
    Else
        Return $sMachineType
    EndIf
EndFunc

AU3XtraTest.dll

test2.au3

Edited by pacman
Link to comment
Share on other sites

After testing this, all computers end in an odd number, to me that means they are ALL workstations, even the servers. Is that true? For example: One server is 33722883, another is 69635, a workstation is 4611. How do you truly determine if the machine is not a true Server (2000 Server, Advanced Server, etc.)? I'm not an expert on bitwise operators, so maybe I'm missing something obvious.

Edited by archrival
Link to comment
Share on other sites

After testing this, all computers end in an odd number, to me that means they are ALL workstations, even the servers. Is that true?

<{POST_SNAPBACK}>

If you are referring to the returned Machine Type then it could have any of the machine type values. For example, a machine can be $SV_TYPE_SERVER, $SV_TYPE_DOMAIN_CTRL and $SV_TYPE_DOMAIN_MASTER.

To correctly test for each type, you need to use BitAND.

Example:

If BitAND($aList[$i+2], $SV_TYPE_SERVER) Then
    $sType = "Server"
EndIf
Edited by pacman
Link to comment
Share on other sites

If you are referring to the returned Machine Type then it could have any of the machine type values. For example, a machine can be $SV_TYPE_SERVER, $SV_TYPE_DOMAIN_CTRL and $SV_TYPE_DOMAIN_MASTER.

To correctly test for each type, you need to use BitAND.

Example:

If BitAND($aList[$i+2], $SV_TYPE_SERVER) Then
    $sType = "Server"
EndIf

<{POST_SNAPBACK}>

I guess i really don't understand what's going on, I guess trying to determine server/workstation is useless, it's trying to determine terminal services or something like that, because all machines in my domain show up both as a server and a workstation.
Link to comment
Share on other sites

I guess i really don't understand what's going on, I guess trying to determine server/workstation is useless, it's trying to determine terminal services or something like that, because all machines in my domain show up both as a server and a workstation.

<{POST_SNAPBACK}>

Have a look at my revised example above. Hopefully it will make things clearer for you.

The type descriptions are from this MSDN article.

I also updated the DLL with a small bug fix.

@Larry, can I piggy back yet another function in your DLL please? I will upload the project into my file area once I have tested it a bit more.

Edited by pacman
Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...