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.

Python3 HTTPS

Create a certificate

openssl req -new -x509 -keyout python-cert.pem -out python-cert.pem -days 365 -nodes

Code:

import http.server, ssl

server_address = ('0.0.0.0', 443)
httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket,
server_side=True,
certfile='python-cert.pem',
ssl_version=ssl.PROTOCOL_TLSv1_2)
httpd.serve_forever()

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

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.