Oracle Ports Scanner (Java Version)

Today I like post how to implement a very simple scanner for oracle ports, for this task we use the tnsping utility and automate it by use multiple iterations over a range of ports. In this example we going to use java but you can use a lots of languages or shells.

First of all, let me to define a core operation like a simple tnsping agaisnt a port, and with the next format;

 
tnsping (ADDRESS=(PROTOCOL=tcp)(HOST=jupiter.domain.com)(port=1523))

Okay, we have the core operation, now we need execute it 1,2,3 … and so on. This task is performed with a loop. Look below, this java code has 3 input parameters, machine, low port and upper port.

 

 
import java.io.*;

public class ListenerScanner {

  public static void main(String argv[]) {

    String s             = null;
    String Maquina       = argv [0];
    int LimiteInferior   = Integer.parseInt (argv [1]);
    int LimiteSuperior   = Integer.parseInt (argv [2]);
    String BinaryPath    = "/app/oracle/database/finalcial/bin/tnsping";

    try {

         System.out.println("Starting scan from port " + argv [1] + " to port " + argv [2]);

         /*** We'll try from lower port to upper port ***/    
         for (int i = LimiteInferior; i <= LimiteSuperior; i++) {

          Process myprocess = Runtime.getRuntime().exec (BinaryPath + " (ADDRESS=(PROTOCOL=tcp)(HOST=" + Maquina + ")(port=" + Integer.toString(i) + "))" );

           BufferedReader stdInput = new BufferedReader(new
                   InputStreamReader(myprocess.getInputStream()));     

           /*** Read the output command  ***/
           while ((s = stdInput.readLine()) != null) {
                if (s.indexOf("OK") != -1) {  /*** if OK substring is include into the string we have a hit ***/
                  System.out.println(" Reply OK for port number:   " + Integer.toString(i));
                }
           } /*** End of while ***/

         } /*** End of for ***/

         System.out.println("Terminated "); 

    } catch (Exception e) {     /*** stack trace if error ***/
         e.printStackTrace();
    }
  }

}

For compile it execute;

 
javac ListenerScanner.java

And execute in this way;

 
$ java ListenerScanner thor-1 1500 1600
Starting scan from port 1500 to port 1600
 Reply OK for port number:   1521
 Reply OK for port number:   1581
 Reply OK for port number:   1582
 Reply OK for port number:   1585
 Reply OK for port number:   1586
 Reply OK for port number:   1587
 Reply OK for port number:   1588
 Reply OK for port number:   1589
 Reply OK for port number:   1592
 Reply OK for port number:   1593
 Reply OK for port number:   1594
Terminated

HTH – Antonio NAVARRO

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s