Automate putty using java

Hello everyone,

Today i am going to talk about how to automate putty using java, most of the Unix users will be dealing with putty through which they login to the specified server.

Some of the testers have some repeated tasks to be performed on different putty sessions, for e.g a tester has to login to a server, do (sudo su – superuser)  so as to change the user as super user who has executing rights on the specified server, then they go to a certain path and execute a script. In Regression testing, testers have to execute many scripts on the same server and for that they have to open different putty session and do the repetitive tasks.

However, we can automate these repeated tasks and let the java program do these tasks while you sit down and have your coffee.

We will make use of Robot and Runtime classes to do these tasks.

What is Robot class in Java?
The Robot class makes it possible for your Java program to temporarily take control of the mouse and keyboard input functionality at the operating-system level.

What is Runtime class in java?
Runtime class is used to interact with java runtime environment. Java Runtime class provides methods to execute a process, invoke GC, get total and free memory etc.

The above code will open putty session then it will login to the mentioned server with your username and password and type the following “sudo su -”
For typing every letter we can use “robot.keyPress(KeyEvent.VK_S);” here KeyEvent.VK_S means we are typing S on the screen, similarly we can use the javascript keycode as well for the same, i.e for S the keycode is 83, you can check the code here
Also if we want to type any uppercase letter then we can use the below code:-
robot.keyPress(16);// Pressing the SHIFT key
robot.keyRelease(16);// Releasing the SHIFT key
Here keycode 16 is for SHIFT key.
By using the same technique you can add the further code and type the path and script which you want to execute.

Alternative way to do the above task.
There is always an alternative for everything,so an alternative for the above code also exists.

Also please make sure that while the script is running you don’t open any other other application and loose focus on your target application for example here is our target application is Putty, if you loose focus the script will execute but will not type on putty application as the focus is gone.

Thanks guys, Enjoy automating.
Automate anything, Automate everything…!!