You can now execute a command by using the Commandhelper.execute method, which implements the
Copy Code code as follows:
Package Javaapplication3;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
/**
*
* @author Chenshu
*/
public class Commandhelper {
Default time Out, in Millseconds
public static int default_timeout;
public static final int default_interval = 1000;
public static long START;
public static Commandresult exec (String command) throws IOException, Interruptedexception {
Process process = Runtime.getruntime (). exec (command);
Commandresult Commandresult = Wait (process);
if (process!= null) {
Process.destroy ();
}
return commandresult;
}
private static Boolean isovertime () {
Return System.currenttimemillis ()-START >= default_timeout;
}
private static Commandresult Wait (process process) throws Interruptedexception, IOException {
BufferedReader errorstreamreader = null;
BufferedReader inputstreamreader = null;
try {
Errorstreamreader = new BufferedReader (New InputStreamReader (Process.geterrorstream ()));
InputStreamReader = new BufferedReader (New InputStreamReader (Process.getinputstream ()));
Timeout control
START = System.currenttimemillis ();
Boolean isfinished = false;
for (;;) {
if (Isovertime ()) {
Commandresult result = new Commandresult ();
Result.setexitvalue (commandresult.exit_value_timeout);
Result.setoutput ("Command process timeout");
return result;
}
if (isfinished) {
Commandresult result = new Commandresult ();
Result.setexitvalue (Process.waitfor ());
Parse Error Info
if (Errorstreamreader.ready ()) {
StringBuilder buffer = new StringBuilder ();
String Line;
while (line = Errorstreamreader.readline ())!= null) {
Buffer.append (line);
}
Result.seterror (Buffer.tostring ());
}
Parse Info
if (Inputstreamreader.ready ()) {
StringBuilder buffer = new StringBuilder ();
String Line;
while (line = Inputstreamreader.readline ())!= null) {
Buffer.append (line);
}
Result.setoutput (Buffer.tostring ());
}
return result;
}
try {
Isfinished = true;
Process.exitvalue ();
catch (Illegalthreadstateexception e) {
Process hasn ' t finished yet
Isfinished = false;
Thread.Sleep (Default_interval);
}
}
finally {
if (Errorstreamreader!= null) {
try {
Errorstreamreader.close ();
catch (IOException e) {
}
}
if (InputStreamReader!= null) {
try {
Inputstreamreader.close ();
catch (IOException e) {
}
}
}
}
}
The Commandhelper class uses the Commandresult object output error message. This class implements
Copy Code code as follows:
Package Javaapplication3;
/**
*
* @author Chenshu
*/
public class Commandresult {
public static final int exit_value_timeout=-1;
Private String output;
void Setoutput (String error) {
Output=error;
}
String GetOutput () {
return output;
}
int exitvalue;
void Setexitvalue (int value) {
Exitvalue=value;
}
int Getexitvalue () {
return exitvalue;
}
Private String error;
/**
* @return the error
*/
Public String GetError () {
return error;
}
/**
* @param error The error to set
*/
public void SetError (String error) {
This.error = error;
}
}
Now look at the demo of the calling code (the main function accepts a timeout parameter):
Copy Code code as follows:
public static void Main (string[] args) {
try {
int timeout = Integer.parseint (args[0]);
Commandhelper.default_timeout = TIMEOUT;
Commandresult result = Commandhelper.exec ("mkdir testdir");
if (result!= null) {
System.out.println ("Output:" + result.getoutput ());
System.out.println ("Error:" + result.geterror ());
}
catch (IOException ex) {
System.out.println ("IOException:" + ex.getlocalizedmessage ());
catch (Interruptedexception ex) {
System.out.println ("interruptedexception:" + ex.getlocalizedmessage ());
}
}
The
result creates a TestDir directory.
I tried to create an SSH login to a remote machine in this way and encountered two problems:
1 If you want to have no man-machine conversation, you need to use the command sshpass-p password ssh user@targetip ' command '
2) Working directly on NetBeans is not going to work because there is not enough permissions to run the Java Javaapplication3 in the terminal. Main
3) Many commands cannot be run, only commands such as PWD can be run, and it is not clear why it is best to switch to the Ganymed SSH-2 library or other similar Java libraries, which I will describe in the next article.