📜 ⬆️ ⬇️

Software visualization of the local network

A complex local network with different subnets is a branched structure that can be attributed to a graph . Computers and routers are represented as nodes of the graph, and the connections between them are the edges of the graph.

image

How does this knowledge help us map the local network?

QuickGraph

There is a great free library for working with graphs - QuickGraph. Working with graphs in this library is implemented very simply.
')
Import the namespace:
Imports QuickGraph

Announcing the graph:
Dim graph As New BidirectionalGraph(Of Object, IEdge(Of Object))

As previously mentioned, computers are the nodes of our graph. Add a node:
graph.AddVertex("")

We will take the list of computers from Active Directory:
Dim de As New DirectoryServices.DirectoryEntry
Dim search As New DirectorySearcher(de)
search.Filter = "(objectCategory=computer)"
Dim results As SearchResultCollection = search.FindAll()
For i As Integer = 0 To results.Count - 1
Dim de2 As DirectoryEntry = results(i).GetDirectoryEntry
Dim ComputerName As String = CType(de2.InvokeGet("cn"), String)
graph.AddVertex(ComputerName)
Next


Remarkably, but the result was a little like a graph. We have no communication between the nodes.

As you know, all computers have common points with which they are connected - routers (routers). It is possible that the local network does not have a router, but in this case, its visualization is quite simple.

Each computer on the network is directly connected to a router whose address is registered as a gateway in the properties of the computer's network card.

Walking through computers with WMI ...
Private Function GetGateway(ByVal ComputerName As String) As String
Dim s As String = String.Empty
Dim query As ManagementObjectSearcher
Dim queryCollection As ManagementObjectCollection
Dim msc As ManagementScope = New ManagementScope("\\" + ComputerName + "\root\cimv2")
query = New ManagementObjectSearcher(msc, New SelectQuery("SELECT * FROM Win32_NetworkAdapterConfiguration"))
queryCollection = query.Get()
For Each mObject As ManagementObject In queryCollection
Try
If mObject("DefaultIPGateway").ToString <> "" Then
s = mObject("DefaultIPGateway")(0).ToString
End If
Catch
End Try
Next
Return s
End Function


... get a list of all the gateways. Add them to the graph, as well as computers:
graph.AddVertex("")

Simultaneously with the addition of nodes, add the connection between the computer and the gateway:
graph.AddEdge(New Edge(Of Object)("", ""))

Now we have a graph, but not a visual one.

Graph #

QuickGraph supports MSAGL, GLEE and Graphviz to visualize graphs, but we will use Graph #. This framework contains various algorithms for visualizing graphs in WPF applications.

Place on the form GraphLayuot control from this framework and write a simple line to display the graph we created earlier:
GraphLayout.Graph = graph

And that's all we need. Everything else will take on Graph #.

image

References:
QuickGraph
Graph #

Source: https://habr.com/ru/post/111214/


All Articles