Multi-thread program scanning the entire network segment

Source: Internet
Author: User
Scan the multi-thread program of the entire network segment-Linux general technology-Linux programming and kernel information. The following is a detailed description. Steeven
Email: phpme@citiz.net

Scanning 500 IPS takes about 10 seconds, and one IP address waits for 0.3 seconds.
To put it bluntly, I think it is a process programming written in java. It is only for reference:
Package steeven;

Import java. SQL .*;
Import java. io .*;
Import java. util .*;
Import java. text .*;
Import javax. servlet. http .*;

Public class Ip extends Common implements Runnable {

Public String ip; // IP address, user name, Host Name

ResultSet list; // record set displayed by PAGE
Public Ip cur; // The current record displayed by PAGE

Static public Hashtable ping = new Hashtable (); // result set after ping
Static int threadCount = 0; // Number of threads in the current thread to prevent computers from being destroyed by multiple threads


Public Ip (){}
Public Ip (String ip ){
This. ip = ip;
Thread r = new Thread (this );
R. start ();
}

Public static void Ping (String ip) throws Exception {
// A maximum of 30 threads
While (threadCount> 30)
Thread. sleep (50 );
ThreadCount + = 1;
Ip p = new Ip (ip );
}
Public void PingAll () throws Exception {
ThreadCount = 0;
Ping = new Hashtable ();
While (next () // next () Place all LAN Ip addresses in cur
Ping (cur. ip );
// Wait until all Ping ends
While (threadCount> 0)
Thread. sleep (50 );
}
Public void run (){
Try {
Process p = runtime.getruntime(cmd.exe c ("ping" + ip + "-w 300-n 1 ");
InputStreamReader ir = new InputStreamReader (p. getInputStream ());
LineNumberReader input = new LineNumberReader (ir );
// Read the result line
For (int I = 1; I <7; I ++)
Input. readLine ();
String line = input. readLine ();

If (line. length () <17 | line. substring (8, 17). equals ("timed out "))
Ping. put (ip, new Boolean (false ));
Else
Ping. put (ip, new Boolean (true ));
// End the thread
ThreadCount-= 1;
} Catch (IOException e ){}
}
Public static void main (String [] args) throws Exception {
Ip ip = new Ip ();
Ip. PingAll ();
Java. util. Enumeration key = ping. keys ();
String k;
While (k = (String) key. nextElement ())! = Null)
System. out. println (k + ":" + ping. get (k ));
}
}

1. Ping is relatively stupid, but it is relatively simple.
2. If the Ping succeeds, you can use nbtstat to get the host name, current user name, and MAC address... everything is under control :)
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.