C # obtain lan ip segments

Source: Internet
Author: User
Static VoidMain ()
{
Arraylist list = getalllocalmachines ();
For(IntI =0; I <list. Count; I ++)
{
Console. writeline (list [I]);
}
Console. readkey ();
}
 

  //  First, ping the host of the network segment cyclically.
// Next, use the following function to obtain the list of IP addresses with responses in all 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;// Read IP address. Mac ....
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 ();

List. Add (IP );
}
}
Return List;
}

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.