Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.util.*;
- import java.io.*;
- import static java.lang.System.out;
- public class PortScanner
- {
- public static void main(String args[])throws IOException
- {
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- out.print("HostName: ");
- String host=br.readLine();
- InetAddress address=null;
- try
- {
- address = InetAddress.getByName(host);
- }
- catch(UnknownHostException uhe)
- {
- out.print("[-] Could not find host.");
- System.exit(0);
- }
- out.print("Ports (separated by commas): ");
- String portstring = br.readLine();
- StringTokenizer ports = new StringTokenizer(portstring,",");
- out.println("[*] Scan results for "+host+" ("+address.getHostAddress()+"):");
- while(ports.hasMoreTokens())
- {
- int port = 0;
- try{
- port = Integer.parseInt(ports.nextToken());
- Socket conn=new Socket();
- conn.connect(new InetSocketAddress(host,port),1000);
- conn.close();
- out.println("\t[+] "+port+"/tcp open");
- }
- catch(SocketTimeoutException ste)
- {
- out.println("\t[-] "+port+"/tcp closed");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement