Saturday 11 June 2016

Let's SSH




Click here to download Let's SSH android app.
 

       Years ago, we used to work on large machines and then we switched to Desktops and then to laptops. Now, even laptop seems like a burden to us. Now is an era for mobile devices. So it's time for us to bring our work to mobile devices.

Let's SSH helps us in achieving this.

Let's SSH is an android app that lets us remotely, yet securely, login to our workstation from anywhere using our mobile devices. 

No overhead of carrying your workstation with you. Your workstation can rest at your workplace, yet you can issue commands to it from wherever you are.



HOW TO USE Let's SSH?

  

Establishing a connection

 

 Establish a connection with remote server using the LOGIN activity.



To login to the remote server, following informations are required : 
  • Name of the account in the server
  • Password of the account in the server
  • IP address of the server


Save login

 

Once you successfully login to your SSH server,
Let's SSH will ask you to save your login along with your password (optional)

Saving your login facilitates : 
  • Faster login henceforth
  • Access to previous command issued (exclusive to the account)


If the password is not saved at login time, it is required every time you login. An option to save the password is given every time you are asked to enter the password.

If the user chooses not to save his login, he is treated as a guest user and his commands are saved for that session and once he logout, all the issued commands are cleared.


Working with the SHELL

 

Let's SSH provides you with a SHELL like environment as shown below,



 I have tried to make the UI of the shell activity similar to command line interface yet maintaining the design integrity of an android app.


Many a times, the user has to feed sensitive information like password for some operations.
Let's SSH allows the user to hide such information by using a simple menu icon (eye).
It does the following : 
  • Transform the input text field into password field
  • Commands/password fed in hidden phase will not be saved
 


Let's SSH works flawlessly with programs involving shell like mysql, python, mongodb and many more.

A screenshot while working with mysql shell is given below,




The overflow menu option allows the user to :
  • clear the screen anytime
  • exit the shell


Faster login 

 

All saved login are displayed in the saved login activity for faster login.



Always a check is made on selecting a saved login whether the server to which the user is trying to connect is the intended host?

Remove saved login with its saved commands with a single touch. 

For a new login he can just tap the floating add button and he will be directed to the login activity.


 IMPORTANT : WHY CONNECTION MAY FAIL?

 

Incorrect IP address

 

If the server and the mobile device are in the same LOCAL NETWORK, say your home network, then you can simply enter the local IP address of the server.

If you want to connect the server which is behind another router then you have to configure the router at the server side to port forward to your server's local IP address using port number 22.


SSH not installed on server

 

For the connection to be successful, SSH must be installed on the server. A simple issue of command on the server can install it :

                        sudo apt-get install ssh

  This is a common issue which gets overlooked by many, including me.




You can download the app from Google Play Store.

Saturday 23 January 2016

ECHO SERVER

  Echo server is a simple server with only one job to send back whatever is sent to it.

  This post is about creating a simple Echo server on your local machine using JAVA programming language.


STEPS INVOLVED

  

Step 1 : create a socket for the echo server.

Step 2 : take ip address and port number as inputs from the user.

Step 3 : bind the ip address and the port number to the echo server socket created in step 1.

Step 4 : wait for the client's request.

Step 5 : once the client is connected to the echo server, assign it an independent thread for further execution.
[Note : Each client will be assigned a different independent thread]

Step 6 : the work of each thread is to simply send back the information received from the client.


PROGRAM :-


Programs are quite self explanatory as comments are added when and where necessary.

First is the Echo Server program that actually creates the echo server for the clients to connect.

Second is an optional program to connect to Echo Server.
If you are familiar with telnet or similar applications, you can connect to echo server using them and skip the Client program provided.

[Note : Program seems to be in plain text here, I am working on it. Till then copy it to your editor so that keywords are highlighted]
 

1. Echo Server Program 

import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Scanner;

public class EchoServer {

    public static void main(String args[]){
   
        try{
   
            // echo server socket
            ServerSocket serverSocket;
   
            // total number of clients
            int clientCount = 0;
       
            // echo server socket address
            SocketAddress serverSocketAddress;
   
            // to obtain input from standard input i.e. keyboard
            Scanner scanner = new Scanner(System.in);
   
            // getting socket address from user  to create echo server socket
            System.out.println("Socket Address :- \n");
           
            System.out.print("\tIP Address :\t");
            String serverIP = scanner.next();
       
            System.out.print("\tPort number :\t");
            short serverPort = Short.parseShort(scanner.next());
       
            serverSocketAddress = new InetSocketAddress(InetAddress.getByName(serverIP), serverPort);
   
            // creation & binding of echo server socket
       
            // echo server socket creation
            serverSocket = new ServerSocket();
       
            // echo server binding
            serverSocket.bind(serverSocketAddress);
       
            if (serverSocket.isBound()){
                System.out.println("Echo Server successfully bound to : " + serverSocketAddress);
            }
            else{
                System.out.println("Echo Server binding unsuccessful");
                System.exit(0);
            }
       
            while (true){
       
                // listening and then accepting client's request to connect
                Socket clientSocket = serverSocket.accept();
               
                if (clientSocket.isConnected()){
                    System.out.println("\nConnected to "+clientSocket.getInetAddress() + '\n');
                    clientCount++;
           
                    // client count as thread name
                    String threadName = Integer.toString(clientCount);
                   
                    Clients client = new Clients(threadName, clientSocket);                   
                }
            }
        }catch(Exception e){
            System.out.println("Exception : " + e.getMessage());
        }
    }
}

class Clients implements Runnable{
   
    // client thread
    private Thread clientThread;
   
    // client socket to communicate
    private Socket clientSocket;
   
    // constructor to initialize the constructor class
    Clients(String clientId,  Socket clientSocket){
       
        // thread name as client id
        clientThread = new Thread(this, clientId);

        this.clientSocket = clientSocket;
       
        // calls the run method
        clientThread.start();
    }
   
    public void run(){
   
        // Scanner object to read data from the standard input
        Scanner scanner = new Scanner(System.in);
   
        // TODO : for the time being only one msg
        try{
            while (true){
               
                // to map the received bytes from client to characters
                InputStreamReader inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
               
                // to store charset mapped received data
                char[] recvData = new char[100];

                // reading data from the input stream
                inputStreamReader.read(recvData, 0, recvData.length);
                System.out.println("Client "+clientThread.getName()+ " : " + new String(recvData));
       
                // sending data to the client
                clientSocket.getOutputStream().write(new String(recvData).getBytes());
            }   
        } catch(Exception e){
                System.out.println("Exception : " + e.getMessage());
        }
    }
}


2. Client Program


import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class Client{
   
    public static void main(String args[]){
   
        // object of Scanner class to handle standard input
        Scanner scanner = new Scanner(System.in);
       
        // server socket to communicate with the server
        Socket serverSocket;
   
        try{
            // getting server socket address from the user to connect
            System.out.println("Server Socket Address :- \n");
           
            System.out.print("\tIP Address :\t");
            String serverIP = scanner.next();
           
            System.out.print("\tPort number :\t");
            short serverPort = Short.parseShort(scanner.next());
           
            // connecting to the specified server
            serverSocket = new Socket(InetAddress.getByName(serverIP), serverPort);
            if (serverSocket.isConnected()){
                System.out.println("\nConnected to "+ serverSocket.getInetAddress() + "\n");
            } else{
                System.out.println("\nConnection FAILED ");
                return;
            }
           
            while (true){
           
                // data to send to the Echo Server
                String dataToSend="";
               
                // to store charset mapped received data
                char[] recvData = new char[100];
               
                // to map the received bytes from the echo server to characters
                InputStreamReader reader = new InputStreamReader(serverSocket.getInputStream());
           
                System.out.print("Me : ");
                // nextLine() must read my input and not the pre existing \n in buffer
                while(dataToSend.length()==0)
                     dataToSend = scanner.nextLine();
           
                // sending data to the server
                serverSocket.getOutputStream().write(dataToSend.getBytes());
           
                // receiving data from the server
                reader.read(recvData, 0, recvData.length);
                System.out.println("Server : " + new String(recvData));
                           
                System.out.println();
            }
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
}


HOW TO RUN THE PROGRAM? 

 

First Echo Server program must be executed
  • Compile and run the Echo Server Program.
  • Enter the ip address assigned to your PC (type 127.0.0.1 if client program is also executed from the same machine).
  • Enter any valid non-reserved port number.
  • Echo Server is created. Now the program will wait for the clients to connect.

Once Echo Server is created, execute the client program

  • Compile and run the Client Program. 
  • Enter the ip address and the port number of the echo server when prompted.
  • Now you will be connected to the echo server.
  • Whatever you send will be echoed back by the Echo Server.  

OUTPUT 


Output from the client's side :-


Client 1 :



Client 2 :



 Output from the echo server's side :-




This is a basic program which can become a foundation for the advanced program that you are going to make ;)