Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ExecutorService;
- // Multithreaded port scanner
- public class PortScanner {
- private int start_port, end_port; // range of ports to scan
- private int timeout = 0; // timeout for the probe
- private String host_name; // host name
- private InetAddress host_address; // host address
- private ExecutorService thread_pool; // thread pool
- // Constructor
- public PortScanner (int start_port, int end_port, String host_name) throws PortException, HostException {
- // checking ports
- if (start_port < 0 || end_port < 0 || start_port > end_port || end_port > 65535)
- throw new PortException("Ports must be integers greater than zero and start port must be lower than end port");
- // checking address
- try {
- host_address = InetAddress.getByName(host_name);
- } catch (UnknownHostException e) {
- throw new HostException("Host unreachable");
- }
- // setting properties
- this.setHostName(host_name);
- this.setStartPort(start_port);
- this.setEndPort(end_port);
- // creating thread pool
- thread_pool = Executors.newFixedThreadPool(100);
- }
- // Getters and setters
- public int getStartPort () { return start_port;}
- public int getEndPort () { return end_port;}
- public int getTimeout() { return timeout; }
- public String getHostName () { return host_name;}
- public void setStartPort (int v) { start_port = v; }
- public void setEndPort (int v) { end_port = v; }
- public void setTimeout (int v) { timeout = v; }
- public void setHostName (String v) { host_name = v; }
- // Starts the PortScanner
- public void startScan() throws PortException {
- int pt = getStartPort(), ep = getEndPort();
- while (pt <= ep) {
- thread_pool.execute(new Probe(host_address, pt, timeout));
- pt ++;
- }
- thread_pool.shutdown(); // shut down the pool
- }
- // Terminates the PortScanner
- public void stopScan() {
- // forcing termination
- thread_pool.shutdownNow();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement