ChannelShell、ChannelExec、ChannelSftp,前两类用于执行命令(命令可以是shell语句,也可以是python xxx.py),后一种是用于上传下载文件。
ChannelShell和ChannelExec的区别: 前者是交互式的,在channel.connect()之前,需要获取outputStream和inputStream,然后outputstream发送命令,从instream中读取命令的结果(注意,发送命令之后,读取命令之前要等待一会儿,一般需要写个循环判断,每秒读一次,根据实际情况设置xx秒超时退出),但是有个坏处是,它执行就像登陆到vm上打印的信息一样,无论执行命令后的任何信息,它都会通过instream返回到客户端来,而你可能仅仅需要命令执行后的结果;于是就有了后者,非交互的,一次通道执行一条命令(当然如果你组合的好,也可以多条,反正就是一个字符串,一次交互,偷偷的告诉你,这一个python脚本,下发的命令去执行这个脚本,可以做好多好多事情哦),好处是,它返回的信息非常干净,只返回标准输出,标准错误是不返回的,这时你可以利用python的print,正确时你print正确信息,错误时print错误信息,客户端都能获取到结果啦(因为print是标准输出)。
ChannelShell用法
String command = "nohup bash /home/aa.sh " + partm.toString();
try {
JSch jsch = new JSch();
com.jcraft.jsch.Session sshSession = jsch.getSession("服务器用户","服务器ip", 22);
sshSession.setPassword("服务器密码");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
sshSession.setConfig(config);
sshSession.connect();
ChannelShell shell = (ChannelShell) sshSession.openChannel("shell");
InputStream inputStream = shell.getInputStream();
OutputStream outputStream = shell.getOutputStream();
PrintWriter printWriter = new PrintWriter(outputStream);
shell.setPty(true);
shell.connect();
printWriter.println(command);
printWriter.println("exit");// 加上个就是为了,结束本次交互
printWriter.flush();
outputStream.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String msg = null;
while ((msg = in.readLine()) != null) {
System.out.println(msg);
}
in.close();
gen_cgsnpWMapper.updateByPrimaryKeySelective(gen_cgsnp);
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ChannelExec用法
String resultJson = null;
ChannelExec channelExec = null;
if (command != null) {
try {
JSch jsch = new JSch();
// 获取sshSession
Session session = jsch.getSession("服务器用户", "服务器ip", "服务器端口号");
// 添加密码
session.setPassword("服务器密码");
Properties sshConfig = new Properties();
// 严格主机密钥检查
sshConfig.put("StrictHostKeyChecking", "no");
session.setConfig(sshConfig);
// 开启sshSession连接
session.connect();
channelExec = (ChannelExec) session.openChannel("exec");
// 设置需要执行的shell命令
channelExec.setCommand(command);
lg.info("linux命令:" + command);
channelExec.setInputStream(null);
channelExec.setErrStream(System.err);
channelExec.connect();
} catch (JSchException e) {
e.printStackTrace();
} finally {
if (null != channelExec) {
channelExec.disconnect();
}
}
}
ChannelSftp用法
ChannelSftp sftp = null;
StringBuffer buffer = new StringBuffer();
try {
JSch jsch = new JSch();
// 获取sshSession
Session session = jsch.getSession("服务器用户", "服务器ip", "服务器端口号");
// 添加密码
session.setPassword("服务器密码");
Properties sshConfig = new Properties();
// 严格主机密钥检查
sshConfig.put("StrictHostKeyChecking", "no");
session.setConfig(sshConfig);
// 开启sshSession连接
session.connect();
// 获取sftp通道
Channel channel = session.openChannel("sftp");
// 开启
channel.connect();
sftp = (ChannelSftp) channel;
lg.info("Connected to " + host + ".");
// 获取生成文件流
InputStream inputStream = sftp.get("读取文件路径");
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line);
}
// 关闭流
inputStream.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
} finally {
if (null != sftp) {
sftp.quit();
}
closeSession();
}
return buffer.toString();