Use C # To retrieve all machines in the LAN

Source: Internet
Author: User

It is actually quite simple. ping a few machines under cmd.exe and run the arp-a command to check whether this method is simpler and more effective than the multi-thread round-robin scanning method.
// Ping the host of the network segment cyclically.
// Use the following functions to obtain the list of IP addresses with responses in the LAN.
Public static ArrayList GetAllLocalMachines ()
{
Process p = new Process ();
P. StartInfo. FileName = "cmd.exe ";
P. StartInfo. UseShellExecute = false;
P. StartInfo. RedirectStandardInput = true;
P. StartInfo. RedirectStandardOutput = true;
P. StartInfo. RedirectStandardError = true;
P. StartInfo. CreateNoWindow = true;
P. Start ();
P. StandardInput. WriteLine ("arp-");
P. StandardInput. WriteLine ("exit ");
ArrayList list = new ArrayList ();
StreamReader reader = p. StandardOutput;
String IPHead = Dns. GetHostByName (Dns. GetHostName (). AddressList [0]. ToString (). Substring (0, 3 );
For (string line = reader. ReadLine (); line! = Null; line = reader. ReadLine ())
{
Line = line. Trim ();
If (line. StartsWith (IPHead) & (line. IndexOf ("dynamic ")! =-1 ))
{
String IP = line. Substring (0, 15). Trim ();
String Mac = line. Substring (line. IndexOf ("-")-2, 0x11). Trim ();
LocalMachine localMachine = new LocalMachine ();
LocalMachine. MachineIP = IP;
LocalMachine. MachineMAC = Mac;
LocalMachine. MachineName = "";
List. Add (localMachine );
}
}
Return list;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.