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.
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

    And the C# version:

    class ComputerNameColumn : ColumnProviderBase
    {
        public ComputerNameColumn() 
            : base("Name", 125)
        {}
    
        public override ColumnValue Execute(IPAddress ipAddress)
        {
            IPHostEntry hostEntry;
    
            try
            {
                hostEntry = Dns.GetHostEntry(ipAddress);
            }
            catch 
            {
                hostEntry = null;
            }
    
            string hostName;
    
            if (hostEntry == null)
            {
                hostName = "";
            }
            else
            {
                hostName = hostEntry.HostName;
            }
    
            return new ColumnValue(ColumnIndex, hostName);
        }
    }
    

    Creating ContextMenuItemProviders add-ins

    Writing ContextMenuItemProviders is even easier. The IContextMenuItemProvider interface is shown below, again in C# and Visual Basic:

    Text is what is shown on the context menu and Execute is called when the user selects the menu item.

    The SystemInformationMenuItem is shown below in C#:

    class SystemInformationMenuItem : IContextMenuItemProvider
    {
        public void Execute(IPAddress ipAddress)
        {
            try
            {
                Process.Start("cmd.exe", "/k systeminfo /s " + ipAddress);
            }
            catch (Exception e)
            {
                MessageBox.Show("An error occurred while retrieving system information:\n\n" + e.Message, 
    "System information", MessageBoxButtons.OK, MessageBoxIcon.Error); } } public string Text { get { return "System Information"; } } }

    And in Visual Basic:

    Friend Class SystemInformationMenuItem
        Implements IContextMenuItemProvider
    
        Public Sub Execute(ByVal ipAddress As IPAddress) Implements IContextMenuItemProvider.Execute
            Try
                Process.Start("cmd.exe", "/k systeminfo /s " & ipAddress.ToString())
            Catch e As Exception
                MessageBox.Show("An error occurred while retrieving system information:" & _
    Constants.vbLf + Constants.vbLf + e.Message, "System information", _
    MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub Public ReadOnly Property Text() As String Implements IContextMenuItemProvider.Text Get Return "System Information" End Get End Property End Class

    What next?

    All kinds of information is available via WMI (Windows Management Instrumentation). Add-ins can be written to display a computer’s CPU, RAM, disk drive information and so on. The OperatingSystemColumn and UptimeColumn classes use WMI to retrieve their values and are a good place to start.

    Regarding ContextMenuItemProviders, anything that requires an IP address can be executed. For example, a reboot command could be initiated, ports scanned, Computer Management opened, etc. The options are endless!

    Using with Windows Vista

    Vista’s User Account Control may get between NetPing and Vista computers’ WMI, and behavior is different for workspace and domain compters. The following articles explain the issue and solutions:

    · Connecting to WMI Remotely Starting with Vista

    · User Account Control and WMI


    Jeff is the Technical Product Manager at InRule Technology and a Visual C# MVP. More stuff like this is available at his website.

    Filed under: ,

    Comment Notification

    If you would like to receive an email when updates are made to this post, please register here

    Subscribe to this post's comments using RSS

    Comments

    # Copyright Revewals » Coding4Fun : Investigate your network with NetPing said on March 29, 2008 9:47 PM:

    PingBack from http://copyrightrenewalsblog.info/coding4fun-investigate-your-network-with-netping/

    Leave a Comment

    (required) 
    (optional)
    (required) 
    Page view tracker