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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
package app;
/**
*
* @author Nahla
*/
import java.net.*;
import java.io.*;
public class Main
{
static int startPort = -1, endPort = -1;
static void scan(String name)
{
try
{
InetAddress host = InetAddress.getByName(name);
System.out.println("Host Name: " + host.getHostName());
System.out.println("IP Address: " + toText(host.getAddress()));
if (endPort < startPort) {
int tmpPort = endPort;
endPort = startPort;
startPort = tmpPort;
}
for(int testPort = startPort; testPort <= endPort; testPort++)
{
try
{
Socket socket = new Socket(host, testPort);
System.out.println("Port: " + testPort + " is open");
socket.close();
} catch(IOException ex) {
System.out.println("Port " + testPort + " is closed");
}
}
} catch(UnknownHostException ex) {
System.out.println("Error: No such host");
}
}
static String toText(byte ip[])
{
StringBuffer result = new StringBuffer();
for(int i = 0; i < ip.length; i++)
{
if(i > 0)
result.append(".");
result.append(0xff & ip[i]);
}
return result.toString();
}
public static void main(String args[])
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.print("Enter Host Name: ");
String name = in.readLine();
System.out.print("Enter starting port: ");
try {
startPort = Integer.parseInt(in.readLine());
} catch (Exception ex) {
startPort = -1;
}
System.out.print("Enter ending port: ");
try {
endPort = Integer.parseInt(in.readLine());
} catch (Exception ex) {
endPort = -1;
}
System.out.flush();
if ((startPort != -1) && (endPort != -1)) {
scan(name);
} else {
System.out.println("Invalid ports");
}
} catch(IOException ex) {
System.out.println("Error");
}
}
} |
Partager