jsch通过SSH2执行linux命令
public class SSHUtils {
private Channel channel;
private Session session = null;
private int timeout = 60000;
public SSHUtils(final String ipAddress, final String username, final String password) throws Exception {
JSch jsch = new JSch();
this.session = jsch.getSession(username, ipAddress, 22);
this.session.setPassword(password);
this.session.setConfig("StrictHostKeyChecking", "no");
this.session.setTimeout(this.timeout);
this.session.connect();
this.channel = this.session.openChannel("shell");
this.channel.connect(1000);
}
public String runShell(String cmd, String charset) throws Exception {
String temp = null;
InputStream instream = null;
OutputStream outstream = null;
try {
instream = this.channel.getInputStream();
outstream = this.channel.getOutputStream();
outstream.write(cmd.getBytes());
outstream.flush();
Thread.sleep(2000);
if (instream.available() > 0) {
byte[] data = new byte[instream.available()];
int nLen = instream.read(data);
if (nLen < 0) {
throw new Exception("network error.");
}
temp = new String(data, 0, nLen, "UTF-8");
}
} finally {
outstream.close();
instream.close();
}
return temp;
}
public void close() {
this.channel.disconnect();
this.session.disconnect();
}
public static void main(final String[] args) throws Exception {
SSHUtils sshUtil = new SSHUtils("10.5.31.157", "root", "codyy#123456");
String res = sshUtil.runShell("/usr/local/baseframe/server.sh start ConfigServer-1.0.0.jar\n", "utf-8");
System.out.println(res);
sshUtil.close();
}
}
jsch通过SSH2执行linux命令的更多相关文章
- 利用java实现可远程执行linux命令的小工具
在linux的脚本中,如果不对机器做其他的处理,不能实现在linux的机器上执行命令.为了解决这个问题,写了个小工具来解决这个问题. 后面的代码是利用java实现的可远程执行linux命令的小工具,代 ...
- jsch ssh服务器调用Linux命令或脚本的小问题
代码如下: public static boolean execshell(String command, String user, String passwd, String host) throw ...
- 使用Android平板编程,执行linux命令
android有一些应用支持开发, AIDE 介绍http://www.wandoujia.com/apps/com.aide.ui https://play.google.com/store/app ...
- Java程序执行Linux命令
Java程序中要执行linux命令主要依赖2个类:Process和Runtime 首先看一下Process类: ProcessBuilder.start() 和 Runtime.exec 方法创建一个 ...
- php执行linux命令的6个函数
一般情况下,很少会用php去执行linux命令,不过特殊情况下,你也许会用到这些函数.以前我知道有二个函数可以执行linux命令,一个是exec,一个是shell_exec.其实有很多的,结合手册内容 ...
- python执行linux命令的两种方法
python执行linux命令有两种方法: 在此以Linux常用的ls命令为例: 方法一:使用os模块 1 2 3 shell# python >> import os >> ...
- Java程序执行Linux命令(JSP运行其他程序)
java程序中要执行linux命令主要依赖2个类:Process和Runtime 首先看一下Process类: ProcessBuilder.start() 和 Runtime.exec 方法创建一个 ...
- Java 执行linux命令(转)
转自 http://blog.csdn.net/a19881029/article/details/8063758 java程序中要执行linux命令主要依赖2个类:Process和Runtime 首 ...
- 后台执行linux命令
/** * * 方法说明:移植执行linux命令 * * @param cmdStr 需要执行的linux命令 * @return 执行命令后的输出(如果是启动一个进程,则可能一直无法返回) * @t ...
随机推荐
- tools.lombok
@Slf4j @Data @Accessors @Builder
- css的手机适配
在html篇里提到设置视口宽度和设备宽度,固定的meta配置就是写死的,==死记硬背== 应该清楚的是手机端的适配应该克服的难题就是宽度根据手机屏幕的大小变化,而高度却没有办法跟随比例变化,也就是宽高 ...
- 002、创建第一个Java程序HelloWord
代码如下: package TIANPAN; public class TestDemo { public static void main(String args[]) { System.out.p ...
- 验证试验 更改了从机CAN通信的MAC地址 从机新挂CAN网络 上电自检通过
更改前 该之后 主机程序 与 从机 程序 已经上传到网盘上 ,主机和从机程序基本一致, 唯一的区别是 从机更好了MAC地址 为0X10 主机的固定MAC地址为 0X1F 改程序的配置上设置的是双滤波 ...
- 51nod 1352:集合计数
1352 集合计数 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 给出N个固定集合{1,N},{2,N-1},{3,N-2},...,{N-1,2 ...
- 集成模拟温度传感器低成本2.4G CC2500RGPR 中文手册
CC2500是一种低成本真正单片的2.4GHz收发器,为低功耗无线应用而设计.电路定位2400-2483.5MHz的ISM(工业,科学和医学)和SRD(短距离设备)频率波段. RF收发器集成了一个数据 ...
- Java8系列 (四) 静态方法和默认方法(转载)
静态方法和默认方法 我们可以在 Comparator 接口的源码中, 看到大量类似下面这样的方法声明 //default关键字修饰的默认方法 default Comparator<T> t ...
- 使用git提交远程仓库
git pull 更新 git add 文件名 将文件添加到暂存区 git commit -m ‘注释’ 提交 git push origin master 提交到远程仓库
- 刷题49. Group Anagrams
一.题目说明 题目是49. Group Anagrams,给定一列字符串,求同源词(包含相同字母的此)的集合.题目难度是Medium. 二.我的做法 题目简单,就不多说,直接上代码: class So ...
- Vulkan SDK 之 DrawCube
Waiting for a Swapchain Buffer Beginning the Render Pass Bind the Pipeline Bind the Descriptor Sets ...