packagecn.virgo.audio.utils;importch.ethz.ssh2.ChannelCondition;importch.ethz.ssh2.Connection;importch.ethz.ssh2.Session;importch.ethz.ssh2.StreamGobbler;importcom.sshtools.j2ssh.SshClient;importcom.sshtools.j2ssh.authentication.AuthenticationProtocolState;importcom.sshtools.j2ssh.authentication.PasswordAuthenticationClient;importcom.sshtools.j2ssh.sftp.SftpFile;importorg.apache.commons.io.IOUtils;import java.io.*;importjava.nio.charset.Charset;importjava.util.ArrayList;importjava.util.List;public classRemoteShellExecutor {privateConnection conn;privateString ip;privateString userName;privateString password;private String charset =Charset.defaultCharset().toString();private static final int TIME_OUT = 1000 * 5 * 60;/*** 构造函数
*
*@paramip
*@paramuserName
*@parampassword*/
publicRemoteShellExecutor(String ip, String userName, String password) {this.ip =ip;this.userName =userName;this.password =password;
}/*** 链接远程桌面
*
*@return*@throwsIOException*/
private boolean login() throwsIOException {
conn= newch.ethz.ssh2.Connection(ip);
conn.connect();returnconn.authenticateWithPassword(userName, password);
}/*** 执行shell
*
*@paramcmds
*@return*@throwsException*/
public int exec(String cmds) throwsException {
InputStream stdOut= null;
InputStream stdErr= null;int ret = -1;try{if(login()) {
Session session=conn.openSession();
session.execCommand(cmds);
stdOut= newStreamGobbler(session.getStdout());
processStream(stdOut, charset);
stdErr= newStreamGobbler(session.getStderr());
processStream(stdErr, charset);
session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
ret=session.getExitStatus();
}else{throw new Exception("远程链接失败:" +ip);
}
}catch(Exception e) {
e.printStackTrace();
}finally{if (conn != null) {
conn.close();
}
IOUtils.closeQuietly(stdOut);
IOUtils.closeQuietly(stdErr);
}returnret;
}/*** 获取执行过程输出
*
*@paramin
*@paramcharset
*@return*@throwsIOException*/
private void processStream(InputStream in, String charset) throwsIOException {byte[] buf = new byte[1024];while (in.read(buf) != -1) {
System.out.println(newString(buf, charset));
}
}/*** 获取Linux下某个文件数据,将其拷贝到本地tmpPath下*/
public ListgetCaleResByFileFromSSH(String filePath, String filename, String tmpPath) {
List resList = new ArrayList<>();
SshClient client= newSshClient();try{
client.connect(this.ip);//设置用户名和密码
PasswordAuthenticationClient pwd = newPasswordAuthenticationClient();
pwd.setUsername(this.userName);
pwd.setPassword(this.password);int result =client.authenticate(pwd);if (result == AuthenticationProtocolState.COMPLETE) {//如果连接完成
List list =client.openSftpClient().ls(filePath);for(SftpFile f : list) {if(f.getFilename().equals(filename)) {
OutputStream os= new FileOutputStream(tmpPath +f.getFilename());
client.openSftpClient().get(f.getAbsolutePath(), os);//以行为单位读取文件start
File file = new File(tmpPath +f.getFilename());
BufferedReader reader= null;try{
reader= new BufferedReader(newFileReader(file));
String tempString= null;int line = 1;//行号//一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {//显示行号
System.out.println("line " + line + ": " +tempString);
resList.add(tempString);
line++;
}
reader.close();
}catch(IOException e) {
e.printStackTrace();
}finally{if (reader != null) {try{
reader.close();
}catch(IOException e1) {
}
}
}//以行为单位读取文件end
}
}
}
}catch(IOException e) {
e.printStackTrace();
}returnresList;
}
}