Guest User

Untitled

a guest
Jan 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. public void actionPerformed(ActionEvent e) {
  2. final FindGateWaysAndIps scanner = new FindGateWaysAndIps();
  3. if (e.getActionCommand()=="Start Scan"){
  4. scanner.start();
  5. }
  6. if (e.getActionCommand()=="Stop Scan"){
  7. scanner.interrupt();
  8. scanner.stopScans();
  9. }
  10. }
  11.  
  12. String ip = "192.168.";
  13. String sql =" ";
  14. static volatile boolean stop = false;
  15. PingResult AllResaults = new PingResult();
  16. int [] AllGateWays = new int [256];
  17.  
  18. final int NUM_THREADS = Runtime.getRuntime().availableProcessors();
  19. ExecutorService exec = Executors.newFixedThreadPool(NUM_THREADS*5);
  20.  
  21. public void run() {
  22. stop=true;
  23. while(stop){
  24. for (;GateWayKey<=GateWayKeyStop;GateWayKey++){
  25. if (!stop){
  26. exec.shutdownNow();
  27. Thread.currentThread().interrupt();
  28. break;
  29. }
  30. ip="192.168."+GateWayKey+".1";
  31. AllSQLs.add(exec.submit((new PingTask(ip,GateWayKey,true))));
  32. }
  33. if (!stop) {
  34. exec.shutdownNow();
  35. Thread.currentThread().interrupt();
  36. break;
  37. }
  38. AllGateWays=GetVectorData.GiveMeGateWays();
  39. for (int j=0; j<= AllGateWays.length;j++){
  40. System.out.println("stop je: "+stop);
  41. if (!stop){
  42. exec.shutdownNow();
  43. Thread.currentThread().interrupt();
  44. break;
  45. }
  46. removeDuplicateinVectors();
  47. //System.out.println("Sada je j"+j);
  48. for (;SubNetKey<=SubNetKeyStop;SubNetKey++){
  49. if (!stop){
  50. exec.shutdownNow();
  51. Thread.currentThread().interrupt();
  52. break;
  53. }
  54. ip="192.168."+AllGateWays[j]+"."+SubNetKey;
  55. AllSQLs.add (exec.submit((new PingTask(ip,AllGateWays[j],false))));
  56. }
  57.  
  58. // Process the result here (this is where you insert into the DB)
  59. //WriteAllDataIntoDataBase();
  60. }
  61. exec.shutdown();
  62. //WriteAllDataIntoDataBase();
  63. }
  64.  
  65. public void stopScans(){
  66. exec.shutdownNow();
  67. stop=false;
  68. }
  69.  
  70. public class PingTask implements Callable <String> {
  71. String ips;
  72. String sql;
  73. PingResult PassDataToExternalClass = new PingResult();
  74. //FindGateWaysAndIps DataProccesor = new FindGateWaysAndIps();
  75. int GateWay;
  76. ScanFrame MonitorData = new ScanFrame();
  77. boolean GateWayORSubNet;
  78. int [] AllGateWays = new int [256];
  79. int i=0;
  80. public int[] GiveMeGateWays(){
  81. return AllGateWays;
  82. }
  83. public PingTask (){
  84. }
  85. public PingTask (String ip, int GateWayKey, boolean GateWayORSubNets){
  86. ips=ip;
  87. GateWay=GateWayKey;
  88. GateWayORSubNet=GateWayORSubNets;
  89. }
  90.  
  91. public String call(){
  92. InetAddress address;
  93. try {
  94. address = InetAddress.getByName(ips);//ako nade gateway neka skoci u petlju u kojoj nade IP adrese pripadajuceg gatewaya
  95. System.out.println("PINGAM: "+ips);
  96. try {
  97. if (address.isReachable(2000)) { //pinga gatewaya s 1000ms (jeli je moguce ovo smanjiti da se ubrza proces)?
  98. System.out.println("Nasa sam IP: "+ips);
  99. AllGateWays[i]=GateWay;
  100. i++;
  101. MonitorData.WriteMonitorData(ips,address.getHostName().toString(),"2000","da");
  102.  
  103. if (GateWayORSubNet){
  104. sql="REPLACE INTO `gateways` (`ID_GATEWAY` , `GATEWAY_IP` , `GATEWAY_NAME`) VALUES ('"+GateWay+"', '"+ips+"', '"+address.getHostName().toString()+"');";
  105. return sql;
  106. }
  107. else{
  108. sql="REPLACE INTO `subnets` (`IP` , `COMPUTER_NAME` , `GATEWAY_KEY`) VALUES ('"+ips+"', '"+address.getHostName().toString()+"', '"+GateWay+"');";
  109. return sql;
  110. }
  111. } else {
  112. return ";";
  113. }
  114. } catch (IOException e) {
  115. return ";";
  116. }
  117. } catch (UnknownHostException e) {
  118. return ";";
  119. }
  120. }
  121. }
  122.  
  123. while(true){
  124. }
  125.  
  126. while(!Thread.currentThread.isInterrupted){
  127. }
  128. //do here what to do in order to exit and clean safely your job and used resources like open filed.
  129.  
  130. catch(InterruptedException e){
  131. //do nothing
  132. }
  133.  
  134. catch(InterruptedException e){
  135. Thread.currentThread.interrupt();
  136. }
Add Comment
Please, Sign In to add comment