Thursday, April 26, 2007

Coding4Fun : Investigate your network with NetPing

Coding4Fun : Investigate your network with NetPing

Investigate your network with NetPing
Published 22 April 07 10:10 PM Coding4Fun

NetPing is an extensible utility that allows for custom display of networked Windows computers, as well as the ability to perform custom actions on these computers.
Jeff Key
Website
Difficulty: Intermediate
Time Required: Less than 1 hour
Cost: Free
Software: Visual Basic or Visual C# Express Editions
Hardware:
Download: Download
Many households, and most businesses, have more than one computer. Being able to remotely monitor and maintain these computers can save valuable minutes every day. NetPing makes these tasks a little easier by serving two purposes: Simple display of computer information and the ability to perform actions on these computers. What makes the application truly useful, though, is the ability to easily add to the application’s functionality with add-ins written in .NET. Add-ins come in two flavors: ColoumnProviders, which display information for each computer in a single column, and ContextMenuItemProviders that allow the user to execute an action via context menu.

How NetPing works
When NetPing is launched, it loads all add-ins in the AddIns directory. When the Start button is pressed, all IP addresses in the given range are pinged and ColumnProvider add-ins are executed for each that respond. Finally, ContextMenuItemProvider add-ins are executed when the user right-clicks a row and selects an item from the context menu.
In the screen shot below, ‘Name’, ‘Uptime’ and ‘Operating System’ are ColumnProvider add-ins. ‘Remote Deskop’ and ‘System Information’ are ContextMenuItemProviders.

The NetPing projects
NetPing consists of the following projects:
· NetPing: This contains the core application. MainForm and HostListViewItem are where the most of the action lives, but most of this action involves threading and is outside the scope of this article. The classes are fully commented, so the adventurous are welcome to dig around.
· NetPing.Common: Common contains what could be considered the NetPing ‘SDK’ (software development kit). It contains the classes that add-in authors use to integrate with NetPing.
· NetPing.AddIns: Finally, AddIns contains the ‘Name’, ‘Uptime’, ‘Operating System’, ‘Remote Desktop’ and ‘System Information’ add-ins. Note that any number of add-in assemblies may be put into the AddIns directory – this project need not be modified to extend NetPing.
Writing ColumnProvider add-ins
The IColumnProvider interface, shown below, must be implemented by all ColumnProvider add-ins:

The abstract ColumnProviderBase class is provided to make ColumnProvider creation a little easier. Classes that inherit ColumnProviderBase are only required to implement Execute, which returns a ColumnValue object.
The ColumnValue class represents three different values:
Text: This is the text that will be displayed.
SortKey: SortKey is a string that will be used when comparing values during a sort, since the text value of a field doesn’t necessarily represent its value. For example, the Uptime column displays uptime in English but its SortKey is a numeric representation of the boot date and time, which sorts chronologically.
ColumnIndex: Indicates which column the row should update with the value.
The simplest ColumnProvider, ComputerNameColumn, is shown below in both Visual Basic and C#.
First, the VB version:

Friend Class ComputerNameColumn
Inherits ColumnProviderBase
Public Sub New()
MyBase.New("Name", 125)
End Sub
Public Overrides Function Execute(ByVal ipAddress As IPAddress) As ColumnValue
Dim hostEntry As IPHostEntry
Try
hostEntry = Dns.GetHostEntry(ipAddress)
Catch
hostEntry = Nothing
End Try
Dim hostName As String
If hostEntry Is Nothing Then
hostName = ""
Else
hostName = hostEntry.HostName
End If
Return New ColumnValue(ColumnIndex, hostName)
End Function
End Class
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }