[JAVA SNIPPET] A Simple Port Scanner - Solixious - 04-19-2013
Here is a snippet code which can perform Port Scanning of your localhost or the host you pass as argument.
Code: import java.net.*;
import java.io.*;
public class PortScanner
{
public static void main(String[] args)
{
String host = "localhost";
if (args.length > 0)
{
host = args[0];
}
for (int i = 1; i < 1024; i++)
{
try
{
Socket s = new Socket(host, i);
System.out.println("There is a server on port " + i + " of "+ host);
}
catch (UnknownHostException ex)
{
System.err.println(ex);
break;
}
catch (IOException ex)
{
ex.printStackTrace();
}
} // end for
} // end main
} // end PortScanner
[JAVA SNIPPET] A Simple Port Scanner - Solixious - 04-19-2013
Here is a snippet code which can perform Port Scanning of your localhost or the host you pass as argument.
Code: import java.net.*;
import java.io.*;
public class PortScanner
{
public static void main(String[] args)
{
String host = "localhost";
if (args.length > 0)
{
host = args[0];
}
for (int i = 1; i < 1024; i++)
{
try
{
Socket s = new Socket(host, i);
System.out.println("There is a server on port " + i + " of "+ host);
}
catch (UnknownHostException ex)
{
System.err.println(ex);
break;
}
catch (IOException ex)
{
ex.printStackTrace();
}
} // end for
} // end main
} // end PortScanner
RE: [JAVA SNIPPET] A Simple Port Scanner - the voice - 04-23-2013
Hi... this is a simple evolution of your scanner
Code: import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class PScan {
protected final static int MAX_PORTS = (int)Character.MAX_VALUE;
protected static InetAddress TARGET;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String host = (args.length == 1)? args[0] : "localhost";
try{
TARGET = Inet4Address.getByName(host);
}catch(UnknownHostException uh){
System.out.println("Unknow Host --> "+host);
System.exit(-1);
}
ExecutorService exs =
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);
System.out.println("Scanning host " + TARGET.getHostAddress());
for(int i = 1; i <= MAX_PORTS; i++){
exs.submit(new Scan(i));
}
exs.shutdown();
}
}
class Scan implements Runnable{
private int port_number;
public Scan(int pn){this.port_number = pn;}
public void run(){
Socket sock = null;
try{
sock = new Socket(PScan.TARGET, port_number);
System.out.println("The port "+port_number+" is open");
}catch(IOException ioe){
}
if(port_number == PScan.MAX_PORTS){
System.out.println("Last port reached");
}
}
}
It's a simple multithreading scanner.
|