Java中通过jsch来连接远程服务器执行linux命令
作者:孙琛斌 时间:2023-02-02 22:21:52
有时候你可能需要通过代码来控制执行linux命令实现某些功能。
针对这类问题可以使用JSCH来实现,具体代码如下:
public class CogradientImgFileManager{
private static final Logger log = LoggerFactory.getLogger(CogradientImgFileManager.class);
private static ChannelExec channelExec;
private static Session session = null;
private static int timeout = 60000;
// 测试代码
public static void main(String[] args){
try{
versouSshUtil("10.8.12.189","jmuser","root1234",22);
runCmd("java -version","UTF-8");
}catch (Exception e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 连接远程服务器
* @param host ip地址
* @param userName 登录名
* @param password 密码
* @param port 端口
* @throws Exception
*/
public static void versouSshUtil(String host,String userName,String password,int port) throws Exception{
log.info("尝试连接到....host:" + host + ",username:" + userName + ",password:" + password + ",port:"
+ port);
JSch jsch = new JSch(); // 创建JSch对象
session = jsch.getSession(userName, host, port); // 根据用户名,主机ip,端口获取一个Session对象
session.setPassword(password); // 设置密码
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config); // 为Session对象设置properties
session.setTimeout(timeout); // 设置timeout时间
session.connect(); // 通过Session建立链接
}
/**
* 在远程服务器上执行命令
* @param cmd 要执行的命令字符串
* @param charset 编码
* @throws Exception
*/
public static void runCmd(String cmd,String charset) throws Exception{
channelExec = (ChannelExec) session.openChannel("exec");
channelExec.setCommand(cmd);
channelExec.setInputStream(null);
channelExec.setErrStream(System.err);
channelExec.connect();
InputStream in = channelExec.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, Charset.forName(charset)));
String buf = null;
while ((buf = reader.readLine()) != null){
System.out.println(buf);
}
reader.close();
channelExec.disconnect();
}
}
标签:jsch,执行,命令
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
mybatis报错元素内容必须由格式正确的字符数据或标记组成异常的解决办法
2023-01-10 15:46:37
Java控制台输入数组并逆序输出的方法实例 <font color=red>原创</font>
2023-07-13 23:32:26
Kotlin的::符号怎么用
2022-07-13 22:59:37
详解C#编程获取资源文件中图片的方法
2023-06-17 23:58:51
基于Flutter制作一个心碎动画特效
2023-05-27 06:07:47
![](https://img.aspxhome.com/file/2023/9/139039_0s.png)
C#中使用资源的方法分析
2022-01-16 16:27:02
浅谈xml配置spring profiles的几个注意点
2022-07-20 15:20:09
Java实战入门之双色球彩票小游戏
2023-05-12 04:07:13
![](https://img.aspxhome.com/file/2023/6/63486_0s.png)
SpringMVC实现文件上传下载功能
2023-09-05 19:49:39
![](https://img.aspxhome.com/file/2023/5/106175_0s.png)
Handler实现倒计时功能
2021-06-01 15:24:31
![](https://img.aspxhome.com/file/2023/2/138222_0s.jpg)
C#实现学生成绩管理系统
2021-06-05 22:51:31
![](https://img.aspxhome.com/file/2023/4/98224_0s.jpg)
C#验证给定字符串形式日期是否合法的方法
2021-09-15 16:00:37
200行java代码实现2048小游戏
2023-11-29 12:03:57
![](https://img.aspxhome.com/file/2023/3/60693_0s.gif)
JAVA包装类及自动封包解包实例代码
2022-10-23 13:28:54
Android实现可点击展开的TextView
2022-04-02 04:58:01
![](https://img.aspxhome.com/file/2023/1/138771_0s.png)
C#实现多线程的同步方法实例分析
2021-11-01 11:56:42
C#中的三种定时计时器Timer用法介绍
2023-06-09 13:31:13
![](https://img.aspxhome.com/file/2023/6/111256_0s.jpg)
Java线程并发访问代码分析
2022-08-15 17:42:08
![](https://img.aspxhome.com/file/2023/6/113256_0s.jpg)
JAVA JDK8 List分组获取第一个元素的方法
2021-06-24 13:07:51
关于Mybatis-Plus Wrapper是否应该出现在Servcie类中
2023-11-28 22:04:56