1、脚本信息如下:

2、代码如下:
public class SSHTool {
private Connection conn;
private String ipAddr;
private Charset charset = StandardCharsets.UTF_8;
private String userName;
private String password;
public SSHTool(String ipAddr, String userName, String password, Charset charset) {
this.ipAddr = ipAddr;
this.userName = userName;
this.password = password;
if (charset != null) {
this.charset = charset;
}
}
/**
* 登录远程Linux主机
*
* @return 是否登录成功
*/
private boolean login() {
conn = new Connection(ipAddr);
try {
// 连接
conn.connect();
// 认证
return conn.authenticateWithPassword(userName, password);
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 执行Shell脚本或命令
*
* @param cmds 命令行序列
* @return 脚本输出结果
*/
public StringBuilder exec(String cmds) throws IOException {
InputStream in = null;
StringBuilder result = new StringBuilder();
try {
if (this.login()) {
// 打开一个会话
Session session = conn.openSession();
session.execCommand(cmds);
in = session.getStdout();
result = this.processStdout(in, this.charset);
conn.close();
}
} finally {
if (null != in) {
in.close();
}
}
return result;
}
/**
* 解析流获取字符串信息
*
* @param in 输入流对象
* @param charset 字符集
* @return 脚本输出结果
*/
public StringBuilder processStdout(InputStream in, Charset charset) throws FileNotFoundException {
byte[] buf = new byte[1024];
StringBuilder sb = new StringBuilder();
try {
// 此方法是休息10秒后最后一次性输出2行数据
int length;
while ((length = in.read(buf)) != -1) {
sb.append(new String(buf, 0, length));
}
// 这个会按照脚本一步一步执行,中途有休息10秒。
BufferedReader reader = null;
String result = null;
reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
while ((result = reader.readLine()) != null) {
System.out.println(result);
}
} catch (IOException e) {
e.printStackTrace();
}
return sb;
}
// 脚本执行结束返回
public static void main(String[] args) throws IOException {
SSHTool tool = new SSHTool("192.168.80.202", "root", "123456", StandardCharsets.UTF_8);
System.out.println(tool.exec("bash /opt/module/test.sh"));
}
}
1万+



被折叠的 条评论
为什么被折叠?



