Getting a computer name from the IP address(C#)
-
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
-
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
-
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
the longer answer is _Yes:).
I had once a similar problem an found thatNetWkstaGetInfo
[^] 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 publicpublic 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
_
-
the longer answer is _Yes:).
I had once a similar problem an found thatNetWkstaGetInfo
[^] 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 publicpublic 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
_
ups :-O
just realized that the enumsPlatformId
andServerType
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
}