Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.isga.isi.reseaux;
- import java.net.InetAddress;
- import java.net.InetSocketAddress;
- import java.net.Socket;
- import java.net.UnknownHostException;
- public class PortScanner {
- public static boolean portIsOpen(String ip, int port, int timeout) {
- try {
- Socket socket = new Socket();
- InetSocketAddress hote=new InetSocketAddress(ip, port);
- socket.connect(hote, timeout);
- socket.close();
- return true;
- } catch (Exception ex) {
- return false;
- }
- }
- public static void main(String[] args) {
- final int DEBUT=0;
- final int FIN=10_000;
- final int TIMEOUT=10;
- if(args.length==0) {
- System.out.println("Argument absent");
- System.out.println("Il faut fournir l'adresse du site à scanner");
- System.exit(-1);
- }
- InetAddress hote=null;
- try {
- hote=InetAddress.getByName(args[0]);
- } catch (UnknownHostException e) {
- System.out.println("Argument incorrect");
- System.out.println("Il faut fournir une adresse valable");
- System.exit(-1);
- }
- int i;
- for(i=DEBUT; i<=FIN; i++) {
- if(portIsOpen(hote.getHostName(), i, TIMEOUT)) {
- System.out.println("Port "+i+ " ouvert.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment