Port testing on Linux or AIX

Sometimes, I need to test, if firewall order is OK and port is properly open and service is not installed yet.
On Linux, it’s not a big deal, because there is installed
Python by default, so you can use this command:

python -m SimpleHTTPServer 80

But on AIX or different platform Python is not installed by default, so I found on internet simple Java server and my colleague (thanks Lukas) did some tuning there.. save following code as TCPServer.java (or you can find source here):


import java.io.*;
import java.net.*;
public class TCPServer
{
public static void main(String argv[]) throws Exception
{
ServerSocket welcomeSocket = new ServerSocket(3001);
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
new Thread(new TCPServer(). new Client(connectionSocket)).start();
}
}
public class Client implements Runnable{
Socket client;
String clientSentence;
String capitalizedSentence;
public Client(Socket s){
client = s;
}
@Override
public void run(){
try{
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(client.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(client.getOutputStream());

while((clientSentence = inFromClient.readLine())!=null){
System.out.println("Received: " + clientSentence);
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}catch(Exception e){e.printStackTrace();}
}
}
}

Then type following command to compile it:
javac TCPServer.java

And run it:
java TCPServer

That’s all.

Update: If you are Perl-like guy, you can use this code from Petr.

Napsat komentář