Enumerate All of the IP Addresses on the Network W/O ActiveDirectory






1.36/5 (15 votes)
May 7, 2004

62758
This will enumerate all of the IP Addresses on the network without using ActiveDirectory.
Introduction
This code gets all of the IP addresses on the network. The best part is, it doesn't use ActiveDirectory. It uses the default DOS commands for networking, specifically "net view". Also, it uses the System.Net.Dns
class to resolve the IPs.
Code
private ArrayList GetIPAddresses()
{
ArrayList Addresses = new ArrayList(); // Temp array to fill
//Get my address
string MyAdd =
Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString();
// Used to start the command
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo();
//prompt
psi.FileName = @"C:\WINDOWS\system32\cmd.exe"; // Path for the cmd prompt
psi.Arguments = "/c net view > boo.txt"; // Arguments for the command prompt
// "/c" tells it to run the following command which is "net view > boo.txt"
// "net view" lists all the computers on the network
// " > boo.txt" tells dos to put the results in a file called boo.txt
psi.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Hidden; // Hide the window
Application.DoEvents();
// So the computer doesn't bog down
System.Diagnostics.Process.Start(psi); // Run
System.IO.StreamReader sr = null; // used for boo.txt
bool run = false; // used to tell if dos is done yet
while(run == false) // while dos is still running
{
Application.DoEvents(); // let windows do it's stuff
try
{
System.Threading.Thread.Sleep(1000); // wait a second
sr = new System.IO.StreamReader(Application.StartupPath
+ "\\boo.txt"); // Try to open the file
// If dos is not done yet, this will throw an error
run = true; // If I get here, dos is done and we can use the file
}
catch
{
run = false; // Dos is not done yet
}
}
// Cycle through the file until the info starts
while(sr.ReadLine().StartsWith("--") != true)
{
Application.DoEvents();
}
string str = ""; // filled with each line of the file
string[] comp = new string[32]; // temp array of computer names
int i = 0; // indexer for comp[]
// cycle until we reach, "The command completed successfully."
while(str.StartsWith("The") != true)
{
Application.DoEvents();
str = sr.ReadLine(); // Read the next line
// Split the line after the first space
comp[i] = str.Split(char.Parse(" "))[0];
// Normally, in the file it lists it like this
// \\MyComputer My Computer's Description
// Take off the slashes, "\\MyComputer" to "MyComputer"
comp[i] = comp[i].Substring(2, comp[i].Length - 2);
if(comp[i] == "e")
// For some reason, I was getting an element
// in this array that has the value of "e", so I took
// it out! lol
{
Application.DoEvents();
comp[i] = null;
}
i++; // move to the next place in comp[]
}
sr.Close(); // close the file handle
sr = null; // free memory
foreach(string s in comp) // for every computer that we have...
{
if(s != null) // if there really is a computer...
{
if(s.ToUpper() != Dns.GetHostName().ToUpper())
// if the computer is not me...
{
Addresses.Add(Dns.GetHostByName(s).AddressList[0].ToString());
// ... add the address to the array
}
}
}
return Addresses; // return the array
}
All you have to do is call this function. E.g.:
ArrayList Adds = new ArrayList();
Adds = GetIPAddresses();