The SSH protocol uses encryption to secure the connection between a client and a server. All user authentication, commands, output, and file transfers are encrypted to protect against attacks in the network. More often than not you would need to SSH into your cloud Virtual Machines or a remote shell. Usually, we need an SSH client to establish an SSH connection. For Windows, the free (libre) GUI client PuTTY is used for this purpose. Here is how it looks like accessing a remote Linux shell in PuTTY:
The following tutorial illustrates how the same can be achieved in android. Note that we are going to implement this project using the Java language. Library: Apache MINA SSHD
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Adding dependencies
Now we need to add the required dependencies in order to use the apache mina sshd library. Open app/src/build.gradle file in your project directory and add the following under dependencies:
// Adding implementations required for apache mina library
Step 3: Add permission to the internet in your AndroidManifest.xml file
Add below two lines inside your src/res/AndroidManifest.xml file inside <manifest></manifest> tags.
<uses-permission android:name=”android.permission.INTERNET” />
Step 4: Modify the strings.xml file
Open app > src > main > res > values > strings.xml and add the following code:
Step 5: Building the UI
We are going to have two activities in our app.
- MainActivity: Here, we’ll enter the login details
- sshActivity: Here, we’ll see the shell output.
Working with the activity_main.xml file:
Now, Navigate to the app > src > main > res > layout > activity_main.xml and add the below code to the file.
First, create a new activity sshActivity by right-clicking on the activity folder (com.example.gfg_ssh) and then select New > Activity > Empty Activity and Now add the below code to the activity_ssh.xml file for UI of this activity.
Step 6: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Step 7: Working with the sshActivity.java file
This is the activity where we import the Apache MINA SSHD library and connect to our remote machine via SSH. We obtain the login data from the intent. Below is the code for the sshActivity.java file. Comments are added inside the code to understand the code in more detail.
SSH command execution:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.