Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Getting a computer name from the IP address(C#)

Getting a computer name from the IP address(C#)

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpsysadminhelptutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    WvdW
    wrote on last edited by
    #1

    Hi, I'm activating software installations on a network but in a very manual way. First pinging the network for active computers, compare actives computers with an installation list then executing a bat on the computer. I thought it should be easy to automate the process and started reading up on the System.Net.NetworkInformation library but the problem is that I don't know how to extract the computer name from the pingReply object. It only returns the IP address and since the DNS lookup table isn't up to date I can't use that. Is there another way to get a computers name from the IP address

    P R 2 Replies Last reply
    0
    • W WvdW

      Hi, I'm activating software installations on a network but in a very manual way. First pinging the network for active computers, compare actives computers with an installation list then executing a bat on the computer. I thought it should be easy to automate the process and started reading up on the System.Net.NetworkInformation library but the problem is that I don't know how to extract the computer name from the pingReply object. It only returns the IP address and since the DNS lookup table isn't up to date I can't use that. Is there another way to get a computers name from the IP address

      P Offline
      P Offline
      pbraun
      wrote on last edited by
      #2

      The short answer is "no".

      Phil

      1 Reply Last reply
      0
      • W WvdW

        Hi, I'm activating software installations on a network but in a very manual way. First pinging the network for active computers, compare actives computers with an installation list then executing a bat on the computer. I thought it should be easy to automate the process and started reading up on the System.Net.NetworkInformation library but the problem is that I don't know how to extract the computer name from the pingReply object. It only returns the IP address and since the DNS lookup table isn't up to date I can't use that. Is there another way to get a computers name from the IP address

        R Offline
        R Offline
        Reto Ravasio
        wrote on last edited by
        #3

        the longer answer is _Yes:).
        I had once a similar problem an found that NetWkstaGetInfo[^] worked quite well. You can provide the IP address as input and get the (BIOS-)name of the machine (and other stuff) back.
        If I remember correctly I've had had some security issues with workstation configurations (domain worked well). The following c# 2.0 code is all that's needed.

        using System;
        using System.Collections.Generic;
        using System.Text;
        using System.Runtime.InteropServices;
        using System.Globalization;
        using NetScan.Contract;
        using System.Diagnostics;

        namespace NetScan.Service
        {
        static class NetBios
        {
        #region public

          public class Info
          {
             private Info()
             {
             }
             internal Info(PlatformId platformId, Version version, string computerName, string lanGroup)
             {
                this.PlatformId = platformId;
                this.Version = version;
                this.ComputerName = computerName;
                this.LanGroup = lanGroup;
             }
             public readonly PlatformId PlatformId;
             public readonly Version Version;
             public readonly string ComputerName;
             public readonly string LanGroup;
        
             public string Comment;
             public ServerType ServerType;
        
             public override string ToString()
             {
                return string.Format(CultureInfo.CurrentCulture, "{0} - {1} - {2}", Version, ComputerName, LanGroup);
             }
          }
        
          public static Info GetInfo(string serverName)
          {
             Info retVal = null;
             System.IntPtr x;
             uint lret = NativeMethods.NetWkstaGetInfo(serverName, 100, out x);
             if (lret != 0 || x == System.IntPtr.Zero)
             {
                return null;
             }
             NativeMethods.WKSTA\_INFO\_100 wi = (NativeMethods.WKSTA\_INFO\_100)Marshal.PtrToStructure(x, typeof(NativeMethods.WKSTA\_INFO\_100));
             retVal = new Info((PlatformId)wi.wki100\_platform\_id, new Version(wi.wki100\_ver\_major, wi.wki100\_ver\_minor), Marshal.PtrToStringUni(wi.wki100\_computername), Marshal.PtrToStringUni(wi.wki100\_langroup));
             NativeMethods.NetApiBufferFree(x);
        
             lret = NativeMethods.NetServerGetInfo(serverName, 101, out x);
             if (lret == 0 && x != System.IntPtr.Zero)
             {
                NativeMetho
        

        _

        R 1 Reply Last reply
        0
        • R Reto Ravasio

          the longer answer is _Yes:).
          I had once a similar problem an found that NetWkstaGetInfo[^] worked quite well. You can provide the IP address as input and get the (BIOS-)name of the machine (and other stuff) back.
          If I remember correctly I've had had some security issues with workstation configurations (domain worked well). The following c# 2.0 code is all that's needed.

          using System;
          using System.Collections.Generic;
          using System.Text;
          using System.Runtime.InteropServices;
          using System.Globalization;
          using NetScan.Contract;
          using System.Diagnostics;

          namespace NetScan.Service
          {
          static class NetBios
          {
          #region public

            public class Info
            {
               private Info()
               {
               }
               internal Info(PlatformId platformId, Version version, string computerName, string lanGroup)
               {
                  this.PlatformId = platformId;
                  this.Version = version;
                  this.ComputerName = computerName;
                  this.LanGroup = lanGroup;
               }
               public readonly PlatformId PlatformId;
               public readonly Version Version;
               public readonly string ComputerName;
               public readonly string LanGroup;
          
               public string Comment;
               public ServerType ServerType;
          
               public override string ToString()
               {
                  return string.Format(CultureInfo.CurrentCulture, "{0} - {1} - {2}", Version, ComputerName, LanGroup);
               }
            }
          
            public static Info GetInfo(string serverName)
            {
               Info retVal = null;
               System.IntPtr x;
               uint lret = NativeMethods.NetWkstaGetInfo(serverName, 100, out x);
               if (lret != 0 || x == System.IntPtr.Zero)
               {
                  return null;
               }
               NativeMethods.WKSTA\_INFO\_100 wi = (NativeMethods.WKSTA\_INFO\_100)Marshal.PtrToStructure(x, typeof(NativeMethods.WKSTA\_INFO\_100));
               retVal = new Info((PlatformId)wi.wki100\_platform\_id, new Version(wi.wki100\_ver\_major, wi.wki100\_ver\_minor), Marshal.PtrToStringUni(wi.wki100\_computername), Marshal.PtrToStringUni(wi.wki100\_langroup));
               NativeMethods.NetApiBufferFree(x);
          
               lret = NativeMethods.NetServerGetInfo(serverName, 101, out x);
               if (lret == 0 && x != System.IntPtr.Zero)
               {
                  NativeMetho
          

          _

          R Offline
          R Offline
          Reto Ravasio
          wrote on last edited by
          #4

          ups :-O
          just realized that the enums PlatformId and ServerType are defined in another file.

          public enum PlatformId
          {
          Unknown = 0,
          DOS = 300,
          OS2 = 400,
          NT = 500,
          OSF = 600,
          VMS = 700,
          }

          [Flags]
          public enum ServerType
          {
          Workstation = 0x00000001, // A LAN Manager workstation
          Server = 0x00000002, // A LAN Manager server
          SqlServer = 0x00000004, // Any server running with Microsoft SQL Server
          DomainController = 0x00000008, // Primary domain controller
          DomainBackupController = 0x00000010, // Backup domain controller
          TimeSource = 0x00000020, // Server running the Timesource service
          Afp = 0x00000040, // Apple File Protocol server
          Novell = 0x00000080, // Novell server
          DomainMember = 0x00000100, // LAN Manager 2.x domain member
          PrintQueueServer = 0x00000200, // Server sharing print queue
          DialinServer = 0x00000400, // Server running dial-in service
          XenixServer = 0x00000800, // Xenix server
          ServerUnix = XenixServer,
          NT = 0x00001000, // Windows Server 2003, Windows XP, Windows 2000, or Windows NT
          Wfw = 0x00002000, // Server running Windows for Workgroups
          ServerMFPN = 0x00004000, // Microsoft File and Print for NetWare
          ServerNT = 0x00008000, // Windows Server 2003, Windows 2000 server, or Windows NT server that is not a domain controller
          PotentialBrowser = 0x00010000, // Server that can run the browser service
          BackupBrowser = 0x00020000, // Server running a browser service as backup
          MasterBrowser = 0x00040000, // Server running the master browser service
          DomainMaster = 0x00080000, // Server running the domain master browser
          ServerOSF = 0x00100000,
          ServerVMS = 0x00200000,
          Windows = 0x00400000, // Windows Me, Windows 98, or Windows 95
          DFS = 0x00800000, // Root of a DFS tree
          ClusterNT = 0x01000000, // NT Cluster
          TerminalServer = 0x02000000, // Terminal Server
          ClusterVsNT = 0x04000000, // Cluster virtual servers available in the domain
          Dce = 0x10000000, // IBM DSS (Directory and Security Services) or equivalent
          }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups