Primer Java kode za ping skeniranje vseh mašin v LAN-u. Koda bo sestavljena iz dveh razredov (PingLan, MojThread). Program lahko napišemo tudi brez “threadov”, vendar je potem zadeva dosti bolj počasna.

Class pingLan:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
 
public class pingLan {
 
	/**
	 * author: damir mesic 
         * http://izascita.si
	 */
	public static void main(String[] args) throws InterruptedException {
 
		try {
			String subnet = "192.168.50.0"; //naš subnet // our subnet
			InetAddress inet1 = InetAddress.getByName(subnet);
		        byte[] bytes = inet1.getAddress();  //IP prenesemo v byte array // add IP to byte array
 
		        // while zanka, koda se izvrši 254-krat  //while loop, code executes 254 times
			while((bytes[3]& 0xFF) < 255){
 
			bytes[3]++; // za vsak loop povečamo vrednost za ena //increase value for one each loop
		        String lan = (bytes[0]& 0xFF)+"."+(bytes[1]& 0xFF)+"."+(bytes[2]& 0xFF)+"."+(bytes[3]& 0xFF);		
		    // začnemo naš thread // start thread
		    Runnable task = new MojThread(lan);
		    Thread worker = new Thread(task);
		    worker.sleep(20); //sleep time nastavljen na 20ms, drugače CPU porabi preveč resorsov //sleep time 20ms,  otherwise CPU is overloaded
		    worker.start();
 
			}						
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
 
}

*Del kode se napačno prikaže v zgornjem oknu. Pravilen zapis je na tej sliki:

kodaslika

Class MojThread:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class MojThread implements Runnable {
 
         /**
	 * author: damir mesic 
         * http://izascita.si
	 */
 
	private String lan;
 
	MojThread(String lan){
		this.lan = lan;	
	}
 
	@Override
	public void run() {
 
	    Runtime rt = Runtime.getRuntime();
	    Process process = null;
		try {
			process = rt.exec("ping -n 1 -w 50 "+lan); // (-n) stevilo posalnih echo zahtev (-w) je max timeout  //(-n) number of send echo requests, (-w) max timeout
 		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
 
    // read the output from the command // preberi izhod ukaza
    String s;
	try {
		while ((s = stdInput.readLine()) != null)
		{
			if (s.equals("Request timed out.")){
				System.out.println(lan+"  NO PING");
			} else if (s.contains("Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),")){
				System.out.println(lan+ "PING OK");
			}
		}
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}				
	}
}

Rezultat:
javaoutput

 

Rezultat ni v vrstnem redu, ker se nekateri threadi končajo prej, kot drugi.

Leave a Reply

Your email address will not be published. Required fields are marked *

Internetna zaščita

Copyright © 2013. All Rights Reserved.