sindibadthesailor

elementary Port scanner

Mar 10th, 2023
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. package com.isga.isi.reseaux;
  2.  
  3. import java.net.InetAddress;
  4. import java.net.InetSocketAddress;
  5. import java.net.Socket;
  6. import java.net.UnknownHostException;
  7.  
  8. public class PortScanner {
  9.     public static boolean portIsOpen(String ip, int port, int timeout) {
  10.         try {
  11.             Socket socket = new Socket();
  12.             InetSocketAddress hote=new InetSocketAddress(ip, port);
  13.             socket.connect(hote, timeout);
  14.             socket.close();
  15.             return true;
  16.         } catch (Exception ex) {
  17.             return false;
  18.         }
  19.     }
  20.  
  21.     public static void main(String[] args) {
  22.         final int DEBUT=0;
  23.         final int FIN=10_000;
  24.         final int TIMEOUT=10;
  25.         if(args.length==0) {
  26.             System.out.println("Argument absent");
  27.             System.out.println("Il faut fournir l'adresse du site à scanner");
  28.             System.exit(-1);
  29.            
  30.         }
  31.         InetAddress hote=null;
  32.        
  33.         try {
  34.              hote=InetAddress.getByName(args[0]);
  35.            
  36.         } catch (UnknownHostException e) {
  37.             System.out.println("Argument incorrect");
  38.             System.out.println("Il faut fournir une adresse valable");
  39.             System.exit(-1);
  40.            
  41.         }
  42.         int i;
  43.         for(i=DEBUT; i<=FIN; i++) {
  44.              if(portIsOpen(hote.getHostName(), i, TIMEOUT)) {
  45.                  System.out.println("Port "+i+ " ouvert.");
  46.              }
  47.         }
  48.        
  49.  
  50.         }
  51.        
  52.        
  53.  
  54.     }
  55.  
  56.  
  57.  
Tags: port scanner
Advertisement
Add Comment
Please, Sign In to add comment