开发者

Network Discovery Program

I m trying to understan开发者_StackOverflowd and modifying a network discovery program..if somebody help me understand the following code i'd be obliged

Dim DomainEntry As New DirectoryEntry("WinNT://" & workGroup.Trim())
            DomainEntry.Children.SchemaFilter.Add("computer")
            X = 5 : Y = 5 : Count = 1
            For Each Machine As DirectoryEntry In DomainEntry.Children
                Dim CompNode As New TreeNode(), CompInfo(1) As String
                CompInfo(0) = Machine.Name
                Dim Tempaddr As System.Net.IPHostEntry = Nothing
                Try
                    Tempaddr = DirectCast(Dns.GetHostByName(Machine.Name), System.Net.IPHostEntry)
                    Dim TempAd As System.Net.IPAddress() = Tempaddr.AddressList, str As String = ""
                    For Each TempA As IPAddress In TempAd
                        CompInfo(1) = TempA.ToString()
                    Next

                Catch ex As Exception
                    CompInfo(1) = ""
                End Try


Hopefully this helps:

''//The variable "workGroup" holds your Active Directory domain name
''//The "DomainEntry" variable will represent the root of your Active Directory hierarchy
Dim DomainEntry As New DirectoryEntry("WinNT://" & workGroup.Trim())
''//Tell the "DomainEntry" variable to only look at "computer" objects
DomainEntry.Children.SchemaFilter.Add("computer")
''//These variables are not used
X = 5 : Y = 5 : Count = 1
''//Loop through all of the computers in the domain
For Each Machine As DirectoryEntry In DomainEntry.Children
    ''//First variable is not used, second is an array with two parts
    Dim CompNode As New TreeNode(), CompInfo(1) As String
    ''//Set the first part of the array to the machine name
    CompInfo(0) = Machine.Name
    ''//The next block tries to get the machine IP by looking it up in DNS. It can fail at several points so it gets wrapped in a try/catch just in case
    Dim Tempaddr As System.Net.IPHostEntry = Nothing
    Try
        ''//Try getting the machine IP
        Tempaddr = DirectCast(Dns.GetHostByName(Machine.Name), System.Net.IPHostEntry)
        ''//A machine can have several IP addresses so this gets a full list of them
        Dim TempAd As System.Net.IPAddress() = Tempaddr.AddressList, str As String = ""
        ''//Most machines will probably have just one IP address, but just in case this code takes the last one that it finds
        For Each TempA As IPAddress In TempAd
            ''//Set the second part of our array to the IP address
            CompInfo(1) = TempA.ToString()
        Next
    Catch ex As Exception
        ''//If this is hit then there was a problem getting the IP address so set it to blank
        CompInfo(1) = ""
    End Try
Next


This is a program for reading a local Active Directory. "WinNT://workgroup" is the address of the directory.

The next line

DomainEntry.Children.SchemaFilter.Add("computer")

Indicates that you're only interested in items of type "computer". From there on, you're creating a two dimensional array which represents the computers with the machine name in the zeroth element, and the IP address of the machine in the second. If there is more than one IP entry for the machine, the array will contain the last one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜