网站首页 > 教程文章 正文
1、加入jcraft的包
com.jcraft
jsch
0.1.54
2、代码实现类
public class SSHCommandExecutor {
private String ipAddress;
private String username;
private String password;
public static final int DEFAULT_SSH_PORT = 22;
private Vector stdout;
public SSHCommandExecutor(final String ipAddress, final String username, final String password) {
this.ipAddress = ipAddress;
this.username = username;
this.password = password;
stdout = new Vector();
}
public int execute(final String command) {
int returnCode = 0;
JSch jsch = new JSch();
try {
// Create and connect session.
Session session = jsch.getSession(username, ipAddress, DEFAULT_SSH_PORT);
session.setPassword(password);
//session.setUserInfo(userInfo);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
// Create and connect channel.
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
BufferedReader input = new BufferedReader(new InputStreamReader(channel
.getInputStream()));
channel.connect();
System.out.println("The remote command is: " + command);
// Get the output of remote command.
String line;
while ((line = input.readLine()) != null) {
stdout.add(line);
}
input.close();
// Get the return code only after the channel is closed.
if (channel.isClosed()) {
returnCode = channel.getExitStatus();
}
// Disconnect the channel and session.
channel.disconnect();
session.disconnect();
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return returnCode;
}
public Vector getStandardOutput() {
return stdout;
}
3、执行示例(重启远程机器的tomcat)
public static void main(final String[] args) {
SSHCommandExecutor sshExecutor = new SSHCommandExecutor("服务器IP", "用户名", "密码");
//1 停止tomcat
sshExecutor.execute("/usr/java/apache-tomcat-8.5.32/bin/shutdown.sh");
//2 开启tomcat
sshExecutor.execute("/usr/java/apache-tomcat-8.5.32/bin/startup.sh");
Vector stdout = sshExecutor.getStandardOutput();
for (String str : stdout) {
System.out.println(str);
}
}
常用的远程执行通道
常用的有三种通道,即ChannelShell、ChannelExec、ChannelSftp,前两类用于执行命令(命令可以是shell语句,也可以是python xxx.py),后一种是用于上传下载文件。
ChannelShell和ChannelExec的区别: 前者是交互式的,在channel.connect()之前,需要获取outputStream和inputStream,然后outputstream发送命令,从instream中读取命令的结果(注意,发送命令之后,读取命令之前要等待一会儿,一般需要写个循环判断,每秒读一次,根据实际情况设置xx秒超时退出),但是有个坏处是,它执行就像登陆到vm上打印的信息一样,无论执行命令后的任何信息,它都会通过instream返回到客户端来,而你可能仅仅需要命令执行后的结果;于是就有了后者,非交互的,一次通道执行一条命令(当然如果你组合的好,也可以多条,反正就是一个字符串,一次交互,偷偷的告诉你,这一个python脚本,下发的命令去执行这个脚本,可以做好多好多事情哦),好处是,它返回的信息非常干净,只返回标准输出,标准错误是不返回的,这时你可以利用python的print,正确时你print正确信息,错误时print错误信息,客户端都能获取到结果啦(因为print是标准输出)。
使用步骤:
1、new一个JSch对象;
2、从JSch对象中获取Session,用于连接,并设置连接信息(根据SSH的连接原理,有两种方式,一是用户名+密码,二是用户名+privatekey+passphrase,第二种方式要在第1步设置,即jsch.addIdentity(xxx));
3、使用session对象调用opnChannel("xxx")打开通信信道,并连接;
4、后面就是不同的channel,不同的操作啦。
猜你喜欢
- 2025-03-28 马斯克创立的Boringcompany融资6.75亿美元,Vy Capital 和红杉资本领投
- 2025-03-28 马斯克的隧道公司C轮融6.75亿美元,估值达57亿美元
- 2025-03-28 Apache Commons VFS——让java访问资源效率超级加倍
- 2025-03-28 程序员的福音 - Apache Commons VFS(上)
- 2025-03-28 如何在Spring Boot中连接SSH请求?
- 2025-03-28 SFTP连接的重用(sftp connection reset)
- 2025-03-28 同事牛逼啊,写了个隐藏 bug,我排查了 3 天才解决问题
- 最近发表
-
- 一个可以用来练手的C++开源编译器!
- Linux开发工具使用指南(linux软件开发工具)
- Linux下Makefile文件的模式规则和自动化变量
- 程序员的副业秘籍!一款可以快速搭建各类系统的后台管理系统
- postgresql自定义函数实现,通过contrib模块进行扩展
- Linux GCC编译及Makefile使用(gcc makefile编写)
- wordpress独立站上线两周没收录?原来是robots.txt搞的鬼…
- make sure用法解析(make sure sth)
- 每天一个 Python 库:Django全能Web框架,一站式后台开发
- Makefile实践(makefile经典教程)
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- mybatis plus page (35)
- vue @scroll (38)
- 堆栈区别 (33)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)
- redis aof rdb 区别 (33)
- 302跳转 (33)
- http method (35)
- js array splice (33)