How Get List of Local Network Computers

How get list of local network computers?

I found solution using interface IShellItem with CSIDL_NETWORK. I get all network PC.

C++: use method IShellFolder::EnumObjects

C#: you can use Gong Solutions Shell Library

using System.Collections;
using System.Collections.Generic;
using GongSolutions.Shell;
using GongSolutions.Shell.Interop;

public sealed class ShellNetworkComputers : IEnumerable<string>
{
public IEnumerator<string> GetEnumerator()
{
ShellItem folder = new ShellItem((Environment.SpecialFolder)CSIDL.NETWORK);
IEnumerator<ShellItem> e = folder.GetEnumerator(SHCONTF.FOLDERS);

while (e.MoveNext())
{
Debug.Print(e.Current.ParsingName);
yield return e.Current.ParsingName;
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}

In C# how do I get the list of local computer names like what one gets viewing the Network in windows explorer

You can try using the System.DirectoryServices namespace.

var root = new DirectoryEntry("WinNT:");
foreach (var dom in root.Children) {
foreach (var entry in dom.Children) {
if (entry.Name != "Schema") {
Console.WriteLine(entry.Name);
}
}
}

List ALL devices on local network?

arp -a will show you only MAC addresses that are stored in local ARP cache and your computer was connected to.
When I'm trying to see every device in my local network I have to scan it.
For example if you have 192.168.1.0/24 network you can do:

$ for i in `seq 1 254`; do
ping -c 1 -q 192.168.1.$i &
done

You will try to ping every computer in your network. Of course not every computer will answer for ping. This is why you can't rely on ping. Now you need to check ARP cache.

$ arp -a | grep 192.168.1. | grep ether

This command will show you ARP cache filtered only with computers that are in this network and that answered on ARP requests (in 99% cases it will be full list of devices in your network - just remember that ARP entry is not removed immediately when the device disconnects).

How to get a list with all computers from the local network?

See the documentation for WNetEnumResource, 'lpcCount' ('Entries' parameter in your code') on return receives the number of items enumerated. You're terminating the enumeration if it is greater than 0, but this is expected. You're requesting one item to be enumerated and the function does that and sets it to 1. Just remove that condition from loop termination:

..
until (Res <> NO_ERROR) or (Limit > 100);
..

You may also want to review the code, f.i. you don't need StrPas.

How can I get a list of computers connected to the network?

I don't know where I got this code from anymore.
The last piece of code is from me (getIPAddress())

It reads your ip to get the base ip of the network.

Credits go to the author:

static void Main(string[] args)
{
string ipBase = getIPAddress();
string [] ipParts = ipBase.Split('.');
ipBase = ipParts[0] + "." + ipParts[1] + "." + ipParts[2] + ".";
for (int i = 1; i < 255; i++)
{
string ip = ipBase + i.ToString();

Ping p = new Ping();
p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
p.SendAsync(ip, 100, ip);
}
Console.ReadLine();
}

static void p_PingCompleted(object sender, PingCompletedEventArgs e)
{
string ip = (string)e.UserState;
if (e.Reply != null && e.Reply.Status == IPStatus.Success)
{
if (resolveNames)
{
string name;
try
{
IPHostEntry hostEntry = Dns.GetHostEntry(ip);
name = hostEntry.HostName;
}
catch (SocketException ex)
{
name = "?";
}
Console.WriteLine("{0} ({1}) is up: ({2} ms)", ip, name, e.Reply.RoundtripTime);
}
else
{
Console.WriteLine("{0} is up: ({1} ms)", ip, e.Reply.RoundtripTime);
}
lock (lockObj)
{
upCount++;
}
}
else if (e.Reply == null)
{
Console.WriteLine("Pinging {0} failed. (Null Reply object?)", ip);
}
}

public static string getIPAddress()
{
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
}
}
return localIP;
}

Getting computer names from my network places

You will want to use the NetServerEnum() API. I dont believe there is a managed wrapper for this in the base .NET libraries but I was able to find this with a quick google search: http://www.codeproject.com/Articles/16113/Retreiving-a-list-of-network-computer-names-using

NOTE: I haven't tested or thoroughly reviewed the codeproject code but it should be enough of a starting point for what you need if there are any issues.

EDIT: Do not use DirectoryServices unless your sure of a domain environment. The System.DirectoryServices class is an ADSI wrapper that dosent work without an Active Directory to query against. NetServerEnum() works on workgroups and domains but dosen't guarantee the most reliable data (not all machines may show up). It relies on the Computer Browser service.

The best solution would probably be a class that wraps both possibilities and merges the results :/

List of IP addresses/hostnames from local network in Python

If by "local" you mean on the same network segment, then you have to perform the following steps:

  1. Determine your own IP address
  2. Determine your own netmask
  3. Determine the network range
  4. Scan all the addresses (except the lowest, which is your network address and the highest, which is your broadcast address).
  5. Use your DNS's reverse lookup to determine the hostname for IP addresses which respond to your scan.

Or you can just let Python execute nmap externally and pipe the results back into your program.



Related Topics



Leave a reply



Submit