High-performance Java IP resource scanning and port Analysis

Source: Internet
Author: User

In the design of network management, IP resources are often scanned to determine IP resource usage and historical Active IP addresses. The traditional method is to analyze the FDB and ARP data of the route exchange, and then obtain the IP resource information. But in regular cases.

However, many data analysis on an IP segment sometimes cannot accurately obtain IP information due to the complexity of the network, resulting in IP resource scanning errors. Here we mainly introduce two algorithms, which scan IP resources of a network segment and port resources of a device through multiple threads.

Source code of the core analysis class:

Package COM. shine. netanalisys; import Java. util. list; import COM. shine. netanalisys. model. netanalyzecallback; import COM. shine. netanalisys. threadmodel. netportthreadmodel; import COM. shine. netanalisys. threadmodel. netscanthreadmodel; import COM. shine. netanalisys. util. networkutil; import COM. shine. framework. threadpoolutil. threadpoolmanager; import COM. shine. framework. threadpoolutil. util. superthread;/*** Network Analysis Class Library mainly includes IP scanning, port analysis ** @ author viruscodecn@gmail.com **/public class netanalisysmanager {Private Static netanalisysmanager manager = NULL; private int netscansize = 10; private int netportsize = 10; public static netanalisysmanager getmanager () {If (Manager = NULL) Manager = new netanalisysmanager (); Return manager;} public void initthreadmodel () {initnetscanthreadmodel (); initnetportthreadmodel ();} publi C void initnetscanthreadmodel () {for (INT I = 0; I <netscansize; I ++) {addnetscanthreadmodel (I);} threadpoolmanager. getmanager (). startthreadpool ();} public void initnetportthreadmodel () {for (INT I = 0; I <netportsize; I ++) {addnetportthreadmodel (I);} threadpoolmanager. getmanager (). startthreadpool ();} private void addnetscanthreadmodel (int I) {netscanthreadmodel threadmodel = new netscanthreadmodel (); Threadmodel. setthreadname ("netscanthreadmodel" + I); threadpoolmanager. getmanager (). addthread (threadmodel); threadmodel = NULL;} private void addnetportthreadmodel (int I) {netportthreadmodel threadmodel = new netportthreadmodel (); threadmodel. setthreadname ("netportthreadmodel" + I); threadpoolmanager. getmanager (). addthread (threadmodel); threadmodel = NULL;}/*** start to perform a network scan ** @ Param startip * @ Param en Dip * @ Param callback * @ Param methodname */Public void startnetscan (string startip, string endip, netanalyzecallback callback, string methodname) {list <string []> iprangelist = networkutil. seperateiprange (startip, endip, 2); int Index = 0; callback. setanalyzefinished (false); long firsttime = system. currenttimemillis (); While (index <iprangelist. size () {// obtain the idle thread. If the thread is busy, wait for superthread thethread = thread Poolmanager. getmanager (). getidlethread ("netscanthreadmodel"); If (thethread! = NULL) {netscanthreadmodel netscanmodel = (netscanthreadmodel) thethread. getthreadmodel (); If (netscanmodel. getObject () = NULL) {netscanmodel. setobject (callback);} netscanmodel. setmethodname (methodname); thethread. setvalues (iprangelist. get (INDEX); index ++;} else {try {thread. sleep (500);} catch (interruptedexception e) {}} while (threadpoolmanager. getmanager (). getidlethreadsize ("netscanthreadmodel ")! = This. netscansize) {try {thread. sleep (500);} catch (interruptedexception e) {}} callback. setanalyzefinished (true); long passtime = system. currenttimemillis ()-firsttime; // system. out. println ("execution time:" + passtime); // threadpoolmanager. getmanager (). stopthreadpool ();} /*** start Port Scan ** @ Param IP * @ Param minport * @ Param maxport * @ Param callback * @ Param methodname */Public void startnetport (string I P, int minport, int maxport, netanalyzecallback callback, string methodname) {list <string []> ipportlist = networkutil. seperateipport (IP, minport, maxport); int Index = 0; callback. setanalyzefinished (false); While (index <ipportlist. size () {// obtain the idle thread. If the thread is busy, wait for superthread thethread = threadpoolmanager. getmanager (). getidlethread ("netportthreadmodel"); If (thethread! = NULL) {netportthreadmodel netportmodel = (netportthreadmodel) thethread. getthreadmodel (); netportmodel. setobject (callback); netportmodel. setmethodname (methodname); thethread. setvalues (ipportlist. get (INDEX); index ++;} else {try {thread. sleep (1000);} catch (interruptedexception e) {}} callback. setanalyzefinished (true);} public void stopnetscanthread () {threadpoolmanager. getmanager (). stopthreadpool ("netscanthreadmodel");} public void stopnetportthread () {threadpoolmanager. getmanager (). stopthreadpool ("netportthreadmodel");} public int getnetscansize () {return netscansize;} public void setnetscansize (INT netscansize) {This. netscansize = netscansize;} public int getnetportsize () {return netportsize;} public void setnetportsize (INT netportsize) {This. netportsize = netportsize ;}}

For example, IP segment scan:

Package COM. shine. netanalisys; import COM. shine. netanalisys. model. netportcallback; import COM. shine. netanalisys. model. netscancallback; public class example {public static void main (string ARGs []) {netanalisysmanager. getmanager (). setnetscansize (10); netanalisysmanager. getmanager (). initthreadmodel (); long curtime = system. currenttimemillis (); netscancallback callback = new netscancallback (); netanalisysmanager. getmanager (). startnetscan ("192.168.11.1", "192.168.11.255", callback, "Callback"); For (string IP: callback. getiplist () {system. out. println ("receive IP" + IP + "Ping allowed");} system. out. println ("execution time" + (system. currenttimemillis ()-curtime ));}}

Code for calling port scan:

Package COM. shine. netanalisys; import COM. shine. netanalisys. model. netportcallback; import COM. shine. netanalisys. model. netscancallback; public class example {public static void main (string ARGs []) {netanalisysmanager. getmanager (). setnetscansize (10); netanalisysmanager. getmanager (). initthreadmodel (); long curtime = system. currenttimemillis (); netscancallback callback = new netscancallback (); netanalisysmanager. getmanager (). startnetscan ("192.168.11.1", "192.168.11.255", callback, "Callback"); For (string IP: callback. getiplist () {system. out. println ("receive IP" + IP + "Ping allowed");} system. out. println ("execution time" + (system. currenttimemillis ()-curtime ));}}

Download address (jar with source code ):

Http://ken-javaframeword.googlecode.com/files/javaFramework2_5_2.jar

SVN address: http://code.google.com/p/ken-javaframeword/

For all the code, see SVN. The above code can only be imported into the jar package and then executed

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.