As its name indicates, the Sshxcute is a framework. It is designed to let engineers to the use Java call to execute command/script on remote Linux/unix system through SSH Connec tion Way, which make software testing or system deployment easier and specifically to make it easier to automate software Testing and system environment deployment.
Sshxcute is designed with the following points at Mind:minimum machine use SSH requirements–only to connect. Easily useable-engineers use Java code to execute command/script. Build-in executing command/script task type easily extendable-this means that it should is easy to create the other task Typ E to plug into Sshxcute.
Let's jump into Sshxcute Java API code directly, then later we'll explain that. Because it is so obvious this if you have OO programming experience, and you'll fell it is very easy.
CustomTask sampletask = new ExecCommand ("Echo 123");
Ssh.exec (Sampletask);
ExecCommand class extends CustomTask class, we create ExecCommand object that has a CustomTask class type reference. Below the shows the class diagram for ExecCommand, Execshellscript and CustomTask.
The only parameter to ExecCommand constructor is the command string. Note To execute multiple commands, you can separate them by delimiter ",". For example:
CustomTask sampletask = new ExecCommand ("Echo 123", "Echo 456," echo 789);
ExecCommand constructor is public execcommand (String...args)
Or If we want to upload single file on the local machine to/home/tsadmin on remote system, we can
ssh.uploadsingledatatoserver ("data/sshxcute_test.sh", "/home/tsadmin");
Note so we should put upload work before execution and after connection. For example,
customtask ct1 = new Execshellscript ("/home/tsadmin", "./sshxcute_test.sh", "Hello World"); Ssh.connect ();
//after connection ssh.uploadsingledatatoserver ("data/sshxcute_test.sh", "/home/tsadmin"); Ssh.exec (CT1); //before execution
Assume we want to run a shell script a Linux server (e.g. IP is 9.125.71.115). About sshxcute_test.sh, please refer to Appendix A.
Below is the Java code to finish this job.
Initialize a sshexec instance without referring any object.
Sshexec ssh = null; Wrap the whole execution jobs into Try-catch blocks try { //Initialize a Connbean object, Paramet
Er list is IP, username, password Connbean cb = new Connbean ("9.125.71.115", "username", "password"); //Put the Connbean instance as parameter for sshexec static method getinstance (Connbean) to retrieve a real Sshexec instance SSH = sshexec.getinstance (CB); //Create a execcommand, the reference class must be Custom
Task CustomTask ct1 = new ExecCommand ("chmod 755/home/tsadmin/sshxcute_test.sh"); //Create a execshellscript, the reference class must be CustomTask CustomTask ct2 = new execs
Hellscript ("/home/tsadmin", "./sshxcute_test.sh", "Hello World");
/Connect to server Ssh.connect (); //Upload Sshxcute_test.sh to/home/tsadmin on remote system Ssh.uploadsingledatatoserver ("data/sshxcute_test.sh", "
/home/tsadmin ");
//Execute task ssh.exec (CT1);
//Execute task and get the returned result object Result res = ssh.exec (CT2);
//Check result and print out messages. if (res.issuccess) { SYSTEM.OUT.PRINTLN ("Return code:"