[Win32utils-devel] Networking functionality?

Park Heesob phasis at nownuri.net
Thu May 26 09:58:30 EDT 2005


Hi,
----- Original Message ----- 
From: "Berger, Daniel" <Daniel.Berger at qwest.com>
To: "Development and ideas for win32utils projects" 
<win32utils-devel at rubyforge.org>
Sent: Thursday, May 19, 2005 11:25 PM
Subject: RE: [Win32utils-devel] Networking functionality?


>> -----Original Message-----
>> From: win32utils-devel-bounces at rubyforge.org
>> [mailto:win32utils-devel-bounces at rubyforge.org] On Behalf Of
>> Zach Dennis
>> Sent: Wednesday, May 18, 2005 5:16 PM
>> To: Development and ideas for win32utils projects
>> Subject: [Win32utils-devel] Networking functionality?
>>
>>
>> Is there (if not, can we get) the functionality to query for
>> workstations in the local workgroup or domain?
>>
>> Zach
>
> There's probably a way using win32ole + WMI.  I'm not sure which of the
> classes you would use, though.  I can see how to get domain information
> and host information, but I'm not sure how to query for hosts on a given
> domain.
>
I made up simple code for network browsing with Win32API.

Try and make it more useful.

Regards,

Park Heesob

===================================================

require "Win32API"

NetServerEnum = Win32API.new('netapi32', 'NetServerEnum', 'PLPLPPLPP', 'L')
Mbstowcs      = Win32API.new('msvcrt', 'mbstowcs','PPL','L')
Wcstombs      = Win32API.new('msvcrt', 'wcstombs','PPL','L')
Memcpy        = Win32API.new('msvcrt', 'memcpy', 'PLL', 'P')
Wcslen        = Win32API.new('msvcrt', 'wcslen', 'P', 'L')
WideCharToMultiByte = Win32API.new('kernel32', 'WideCharToMultiByte', 
'LLPLPLPP', 'L')

ServerInfo = Struct.new( "ServerInfo", :platform_id, :name, :version_major, 
:version_minor, :type, :comment )

SERVER_INFO_101_SIZE = 24
CP_ACP   = 0
MAX_PREFERRED_LENGTH = -1
SV_TYPE_WORKSTATION        = 0x00000001
SV_TYPE_SERVER             = 0x00000002
SV_TYPE_SQLSERVER          = 0x00000004
SV_TYPE_DOMAIN_CTRL        = 0x00000008
SV_TYPE_DOMAIN_BAKCTRL     = 0x00000010
SV_TYPE_TIME_SOURCE        = 0x00000020
SV_TYPE_AFP                = 0x00000040
SV_TYPE_NOVELL             = 0x00000080
SV_TYPE_DOMAIN_MEMBER      = 0x00000100
SV_TYPE_PRINTQ_SERVER      = 0x00000200
SV_TYPE_DIALIN_SERVER      = 0x00000400
SV_TYPE_XENIX_SERVER       = 0x00000800
SV_TYPE_SERVER_UNIX        = SV_TYPE_XENIX_SERVER
SV_TYPE_NT                 = 0x00001000
SV_TYPE_WFW                = 0x00002000
SV_TYPE_SERVER_MFPN        = 0x00004000
SV_TYPE_SERVER_NT          = 0x00008000
SV_TYPE_POTENTIAL_BROWSER  = 0x00010000
SV_TYPE_BACKUP_BROWSER     = 0x00020000
SV_TYPE_MASTER_BROWSER     = 0x00040000
SV_TYPE_DOMAIN_MASTER      = 0x00080000
SV_TYPE_SERVER_OSF         = 0x00100000
SV_TYPE_SERVER_VMS         = 0x00200000
SV_TYPE_WINDOWS            = 0x00400000
SV_TYPE_DFS                = 0x00800000
SV_TYPE_CLUSTER_NT         = 0x01000000
SV_TYPE_DCE                = 0x10000000
SV_TYPE_ALTERNATE_XPORT    = 0x20000000
SV_TYPE_LOCAL_LIST_ONLY    = 0x40000000
SV_TYPE_DOMAIN_ENUM        = 0x80000000
SV_TYPE_ALL                = 0xFFFFFFFF
PLATFORM_ID_DOS = 300
PLATFORM_ID_OS2 = 400
PLATFORM_ID_NT  = 500
PLATFORM_ID_OSF = 600
PLATFORM_ID_VMS = 700

def getserverinfo(domain=nil)

    if domain
        pdomain = "\0" * 512
        Mbstowcs.Call(pdomain, domain + "\0", 512)
        domain = pdomain.strip + "\0\0"
    end

    pbufptr = "\0" * 4
    pentriesread = "\0" * 4
    ptotalentries = "\0" * 4

    type = domain ? SV_TYPE_SERVER : SV_TYPE_DOMAIN_ENUM

    ret = 
NetServerEnum.Call(nil,101,pbufptr,MAX_PREFERRED_LENGTH,pentriesread,ptotalentries,type,domain,nil)
    if 0 != ret
          puts "Failed:#{ret}"
          nil
    end

    entriesread = pentriesread.unpack("L")[0]

    bufptr = "\0" * (SERVER_INFO_101_SIZE*entriesread)

    Memcpy.Call(bufptr,pbufptr.unpack("L")[0],SERVER_INFO_101_SIZE*entriesread)

    server_info = []
    entriesread.times {|i|
        serverinfo = ServerInfo.new(*bufptr[i*SERVER_INFO_101_SIZE .. 
(i+1)*SERVER_INFO_101_SIZE].unpack("LLLLLL"))
        case serverinfo.platform_id
            when PLATFORM_ID_DOS
                serverinfo.platform_id = "DOS"
            when PLATFORM_ID_OS2
                serverinfo.platform_id = "OS2"
            when PLATFORM_ID_NT
                serverinfo.platform_id = "NT"
            when PLATFORM_ID_OSF
                serverinfo.platform_id = "OSF"
            when PLATFORM_ID_VMS
                serverinfo.platform_id = "VMS"
            else
                serverinfo.platform_id = "UNKNOWN"
        end

        type = []
        type.push("WORKSTATION"      ) if serverinfo.type & 
SV_TYPE_WORKSTATION       != 0
        type.push("SERVER"           ) if serverinfo.type & SV_TYPE_SERVER 
!= 0
        type.push("SQLSERVER"        ) if serverinfo.type & 
SV_TYPE_SQLSERVER         != 0
        type.push("DOMAIN_CTRL"      ) if serverinfo.type & 
SV_TYPE_DOMAIN_CTRL       != 0
        type.push("DOMAIN_BAKCTRL"   ) if serverinfo.type & 
SV_TYPE_DOMAIN_BAKCTRL    != 0
        type.push("TIME_SOURCE"      ) if serverinfo.type & 
SV_TYPE_TIME_SOURCE       != 0
        type.push("AFP"              ) if serverinfo.type & SV_TYPE_AFP 
!= 0
        type.push("NOVELL"           ) if serverinfo.type & SV_TYPE_NOVELL 
!= 0
        type.push("DOMAIN_MEMBER"    ) if serverinfo.type & 
SV_TYPE_DOMAIN_MEMBER     != 0
        type.push("PRINTQ_SERVER"    ) if serverinfo.type & 
SV_TYPE_PRINTQ_SERVER     != 0
        type.push("DIALIN_SERVER"    ) if serverinfo.type & 
SV_TYPE_DIALIN_SERVER     != 0
        type.push("XENIX_SERVER"     ) if serverinfo.type & 
SV_TYPE_XENIX_SERVER      != 0
        type.push("SERVER_UNIX"      ) if serverinfo.type & 
SV_TYPE_SERVER_UNIX       != 0
        type.push("NT"               ) if serverinfo.type & SV_TYPE_NT 
!= 0
        type.push("WFW"              ) if serverinfo.type & SV_TYPE_WFW 
!= 0
        type.push("SERVER_MFPN"      ) if serverinfo.type & 
SV_TYPE_SERVER_MFPN       != 0
        type.push("SERVER_NT"        ) if serverinfo.type & 
SV_TYPE_SERVER_NT         != 0
        type.push("POTENTIAL_BROWSER") if serverinfo.type & 
SV_TYPE_POTENTIAL_BROWSER != 0
        type.push("BACKUP_BROWSER"   ) if serverinfo.type & 
SV_TYPE_BACKUP_BROWSER    != 0
        type.push("MASTER_BROWSER"   ) if serverinfo.type & 
SV_TYPE_MASTER_BROWSER    != 0
        type.push("DOMAIN_MASTER"    ) if serverinfo.type & 
SV_TYPE_DOMAIN_MASTER     != 0
        type.push("SERVER_OSF"       ) if serverinfo.type & 
SV_TYPE_SERVER_OSF        != 0
        type.push("SERVER_VMS"       ) if serverinfo.type & 
SV_TYPE_SERVER_VMS        != 0
        type.push("WINDOWS"          ) if serverinfo.type & SV_TYPE_WINDOWS 
!= 0
        type.push("DFS"              ) if serverinfo.type & SV_TYPE_DFS 
!= 0
        type.push("CLUSTER_NT"       ) if serverinfo.type & 
SV_TYPE_CLUSTER_NT        != 0
        type.push("DCE"              ) if serverinfo.type & SV_TYPE_DCE 
!= 0
        type.push("ALTERNATE_XPORT"  ) if serverinfo.type & 
SV_TYPE_ALTERNATE_XPORT   != 0
        serverinfo.type = type.join(",")

        name = "\0" * 512
        WideCharToMultiByte.Call(CP_ACP,0,serverinfo.name,-1,name,512,nil,nil)
        serverinfo.name = name.strip

        comment = "\0" * 512
        WideCharToMultiByte.Call(CP_ACP,0,serverinfo.comment,-1,comment,512,nil,nil)
        serverinfo.comment = comment.strip

        server_info.push(serverinfo)

    }
    server_info
end

puts "\nDOMAIN LIST"
getserverinfo.each {|s|
    puts 
"PlatForm:#{s.platform_id}\tName:#{s.name}\tVersion:#{s.version_major}.#{s.version_minor}\n\tType:#{s.type}"
}

puts "\nCOMPUTER LIST"
getserverinfo("WORKGROUP").each {|s|
    puts 
"PlatForm:#{s.platform_id}\tName:#{s.name}\tVersion:#{s.version_major}.#{s.version_minor}\n\tType:#{s.type}\tComment:#{s.comment}"
}


puts "\nDOMAIN-COMPUTER LIST"  # net view style output
getserverinfo.each {|ss|
    puts "\nDOMAIN: #{ss.name}"
    getserverinfo(ss.name).each {|s|
        puts "\t\\\\" + s.name + "\t" + s.comment
    }
}










More information about the win32utils-devel mailing list