Java代码实现执行Linux服务器命令
通过jsch可以实现基于sftp协议的文件传输,使用的是ChannelSftp类,如果要在程序中实现启动服务器的一个脚本,执行某个系统命令的话,就需要用到另一个channel类了,就是ChannelExec类。
如果项目采用maven构建的话,引入
<dependency><groupId>com.jcraft</groupId><artifactId>jsch</artifactId><version>0.1.50</version>
</dependency>
代码示例:
package com.lipl;
import com.jcraft.jsch.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
/**
* @author lipenglong
* @ClassName:
* @Description: this is a class desc.
* @date 14-4-1 上午10:44
*/
public class JschCommand {
private Session session = null;
private Channel channel = null;
private String sftpHost = "localhost";
private int sftpPort = 22;
private String sftpUserName = "petric";
private String sftpPassword = "123";
private int timeout = 30000;
/**
* 获取连接
* @return
*/
private ChannelExec getChannelExec() {
try {
if (channel != null && channel.isConnected()) {
return (ChannelExec) channel;
}
JSch jSch = new JSch();
if (session == null || !session.isConnected()) {
session = jSch.getSession(sftpUserName, sftpHost, sftpPort);
session.setPassword(sftpPassword);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setTimeout(timeout);
session.connect();
}
channel = session.openChannel("exec");
} catch (Exception e) {
if (session != null) {
session.disconnect();
session = null;
}
channel = null;
}
return channel == null ? null : (ChannelExec) channel;
}
/**
* 关闭连接
*/
private void closeChannel() {
try {
if (channel != null) {
channel.disconnect();
channel = null;
}
if (session != null) {
session.disconnect();
session = null;
}
} catch (Exception e) {
System.out.println(e);
}
}
/**
* 执行服务器端命令
*/
public boolean executeCommand(String command) {
boolean flag = false;
ChannelExec channelExec = getChannelExec();
if (channelExec == null) {
return false;
}
try {
channelExec.setInputStream(null);
channelExec.setErrStream(System.err);
channelExec.setCommand(command);
InputStream in = channelExec.getInputStream();
channelExec.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
closeChannel();
flag = true;
} catch (Exception e) {
System.out.println(e);
flag = false;
}
return flag;
}
public static void main(String[] args) {
JschCommand jschCommand = new JschCommand();
System.out.println(jschCommand.executeCommand("date"));
System.out.println("----------------------------------");
System.out.println(jschCommand.executeCommand("ruby Demo.rb"));
System.out.println("----------------------------------");
System.out.println(jschCommand.executeCommand("touch abc.txt"));
System.out.println("----------------------------------");
System.out.println(jschCommand.executeCommand("rm abc.txt"));
}
}
通过ChannelExec类可以实现执行服务器命令,如date, touch, rm等,可以执行服务器上我们写好的某个脚本,如Demo.rb。
Java代码实现执行Linux服务器命令相关推荐
- 利用putty的pscp命令执行Linux服务器命令
语法:pscp -ls root@111.98.212.142:/home 结果: 要获取更多Jerry的原创文章,请关注公众号"汪子熙":
- 解决java使用Runtime.exec执行linux复杂命令不成功问题
解决java使用Runtime.exec执行linux复杂命令不成功问题 参考文章: (1)解决java使用Runtime.exec执行linux复杂命令不成功问题 (2)https://www.cn ...
- JAVA代码实现执行CMD命令的方法
用JAVA代码实现执行CMD命令的方法! Runtime rt = Runtime.getRuntime(); Process p = rt.exec(String[] cmdarray); ...
- Java Web项目在Linux服务器自动化部署续-整合Bamboo
前言 Java Web项目在Linux服务器自动化部署 在这一篇中, 完成的是一个自动化部署的脚本. Atlassian Bamboo 是一款持续集成构建服务器软件(Build Server) 软件地 ...
- 2021-07-08执行Linux的命令分析
在进行深度学习实验时,GPU 的实时状态监测十分有必要. 执行Linux的命令: # /bin/bash 1. 查看CPU情况: 输入: lscpu 2. 查看GPU情况: 输入: nvidia-sm ...
- Java实现windows,linux服务器word,excel转为PDF;aspose-words,Documents4j
Java实现windows,linux服务器word,excel转为PDF:aspose-words,Documents4j 一.通过aspose-words将word,Excel文档转为PDF 1. ...
- java代码块执行顺序_Java笔记 | Java代码块执行顺序测试
最近笔试常常遇到考察Java代码块执行顺序的题目,网上查看博客错漏百出,特地自己测试了一下. 如有错漏,希望路过的大佬指出来,以便我进行更改. 先上代码吧! public class ClassA { ...
- linux打开python3_Python3 执行Linux Bash命令的方法
和之前C++执行Linux Bash命令的方法 一样,Python依然支持system调用和popen()函数来执行linux bash命令. 方法一:system调用 #仅仅在一个子终端运行系统命令 ...
- python commands用法_python之commands模块(执行Linux Shell命令)
commands模块 用于执行Linux shell命令,要获得shell命令的输出只需要在后面参数写入('命令')就可以了. 需要得到命令执行的状态则需要判断$?的值, 在Python中有一个模块c ...
最新文章
- tomcat php模块化运行,Windows下 Tomcat 运行 PHP 的配置
- react 显示当前时间_react 日期
- 计算机网络(湖科大教书匠)
- linux单引号、双引号、特殊字符、换行符等用法
- leetcode python3 简单题203. Remove Linked List Elements
- python用户画像_新闻个性化推荐系统源码之构建离线用户和文章特征
- 【编译器】G++相关编译命令
- cocos2d-xFinalProject踩坑记录(cocosStudio控件获取,角色移动及动画,碰撞检测,背景音乐与场景)...
- IPFS是创建DWeb应用程序中基础技术的领先者
- word在html中预览,在网页中预览word和excel
- 基于Arduino、ESP8266的物联网项目开发方案整理、毕业设计(第二波)
- 计算机没有有效的ip配置,win7电脑没有有效的ip配置怎么办
- 计算机保研er历程分享(浙软、厦大、华师、东南网安、东北、西电、中南......)
- Pycharm 恢复到默认设置
- 基于FOC电路低次谐波抑制Simulink仿真
- WEB2.0下的金蛋 什么样的商业模式会成功?
- PPI_DPI 对比
- echarts堆叠图显示总数 tooltips处理
- leetcode 605 种花问题 (c++和python)
- 远程连接GPU服务器的Jupyter Notebook