The code is as follows:
Public Static BooleanExecshell (String command, string user, String passwd, String host)throwsjschexception, IOException {Connect (user, passwd, host); BufferedReader Reader=NULL; BooleanFlag =true; Channel Channel=NULL; String CharSet= "UTF-8"; Try { //execute the linux commandChannel = Session.openchannel ("exec"); ((channelexec) channel). SetCommand (command); ( (CHANNELEXEC) channel). Seterrstream (System.err); Channel.connect (); InputStream in=Channel.getinputstream (); Reader=NewBufferedReader (NewInputStreamReader (In, Charset.forname (Charset))); String buf=NULL; while((buf = Reader.readline ())! =NULL) {log.info (BUF); } }Catch(Exception e) {e.printstacktrace (); }finally{reader.close (); Channel.disconnect (); Session.disconnect (); } returnFlag; }
Here is a problem, if you use the method in red font to output the system error when executing the linux command or SH file, this will cause a problem, that is, the method of using log4j is invalid, Subsequent log.info and log in other classes will be invalidated, and eventually Tomcat's catalina.out log is stopped forever .... So when using, be sure to comment out Oh ~ or do not OH ~ Use the following reader read Linux output information on the line Oh ~
Jsch SSH server calling a Linux command or script minor issue