springboot 远程调用shell脚本,环境为windows
springboot 远程调用shell脚本,环境为windows
- pom.xml配置
- yml配置文件
- 实体类
- 配置服务器SSH
- 具体业务
- 测试
由于需要调用shell脚本实现一些自动化控制操作,特记录一下实现代码。
主要使用到Ganymed SSH-2 for Java包,是java实现SSH-2协议的一个包,利用它在Java程序中连接SSH服务器。
包地址:点我下载
maven仓库地址:点我打开
pom.xml配置
......<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!-- SSH-2协议的包 --><dependency><groupId>ch.ethz.ganymed</groupId><artifactId>ganymed-ssh2</artifactId><version>262</version></dependency></dependencies>
......
yml配置文件
ssh1:host: 192.168.227.3port: 22user: rootpassword: 123ssh2:host: 192.168.227.4port: 22user: rootpassword: 123
实体类
package com.bq8023.task.domain;import lombok.Data;import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;/*** @Author bq8023* @Date 21/5/9 19:36* @Version 1.0*/
@Data
public class SSHEntity {/*** 编码*/private Charset charset = StandardCharsets.UTF_8;/*** 主机(IP)*/private String host;/*** 连接端口*/private int port;/*** 账号*/private String user;/*** 密码*/private String password;}
配置服务器SSH
package com.bq8023.task.conf;import com.bq8023.task.domain.SSHEntity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 配置各服务器ssh连接信息** @Author bq8023* @Date 21/5/9 20:44* @Version 1.0*/@Configuration
public class SSHConfig {@Bean(name = "ssh1")@ConfigurationProperties(prefix = "ssh1")public SSHEntity sshEntity3() {return new SSHEntity();}@Bean(name = "ssh2")@ConfigurationProperties(prefix = "ssh2")public SSHEntity sshEntity4() {return new SSHEntity();}}
可以在此处配置多台服务器
具体业务
package com.bq8023.task.service;import com.bq8023.task.domain.SSHEntity;
import org.springframework.stereotype.Service;import java.io.IOException;/*** @Author bq8023* @Date 21/5/9 19:56* @Version 1.0*/
@Service
public interface SSHService {/*** 连接服务器并 执行shell脚本** @param sshEntity ssh连接信息* @param shell shell语句* @return shell执行结果* @throws IOException 读写异常*/StringBuilder exec(SSHEntity sshEntity, String shell) throws IOException;
}
package com.bq8023.task.service.serviceImpl;import com.bq8023.task.domain.SSHEntity;
import com.bq8023.task.service.SSHService;import java.io.IOException;
import java.io.InputStream;import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import org.springframework.stereotype.Service;/*** @Author bq8023* @Date 21/5/9 19:57* @Version 1.0*/
@Service
public class SSHServiceImpl implements SSHService {private Connection connect;/*** 连接服务器并 执行shell脚本** @param sshEntity ssh连接信息* @param shell shell语句* @return shell执行结果* @throws IOException 读写异常*/@Overridepublic StringBuilder exec(SSHEntity sshEntity, String shell) throws IOException {InputStream inputStream = null;StringBuilder result = new StringBuilder();try {// 认证登录信息if (this.login(sshEntity)) {// 登陆成功则打开一个会话Session session = connect.openSession();session.execCommand(shell);inputStream = session.getStdout();result = this.processStdout(inputStream);connect.close();}} finally {if (null != inputStream) {inputStream.close();}}return result;}private boolean login(SSHEntity sshEntity) {connect = new Connection(sshEntity.getHost(), sshEntity.getPort());try {connect.connect();return connect.authenticateWithPassword(sshEntity.getUser(), sshEntity.getPassword());} catch (IOException e) {e.printStackTrace();return false;}}private StringBuilder processStdout(InputStream in) {byte[] buf = new byte[1024];StringBuilder builder = new StringBuilder();try {int length;while ((length = in.read(buf)) != -1) {builder.append(new String(buf, 0, length));}} catch (IOException e) {e.printStackTrace();}return builder;}
}
测试
package com.bq8023.task;import com.bq8023.task.domain.SSHEntity;
import com.bq8023.task.service.SSHService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;
import java.io.IOException;@SpringBootTest
class JdtaskApplicationTests {@Qualifier("SSHServiceImpl")@Autowiredprivate SSHService sshService;@Resource(name = "ssh1")private SSHEntity sshEntity;@Testvoid contextLoads() throws IOException {StringBuilder stringBuilder = sshService.exec(sshEntity, "pwd");System.out.println(stringBuilder);}
}
结果输出
参考
windows下java远程调用shell脚本
springboot 远程调用shell脚本,环境为windows相关推荐
- java 远程 shell脚本_Java 远程调用 shell脚本
* pom.xml 引入相关jar包 ``` ch.ethz.ganymed ganymed-ssh2 262 ``` * 远程服务器上代码 ``` #/bin/bash echo "he ...
- java调用shell脚本及注意事项
需求: get方法下载远程zip包,然后zip包解压,取出第一级目录再次进行压缩获取新的压缩zip包. 问题: 如果选择使用java代码的IO流操作,在不确定zip包大小的情况下可能会占用很大的内存, ...
- java调用shell脚本_Java 执行Shell脚本指令
一.介绍 有时候我们在Linux中运行Java程序时,需要调用一些Shell命令和脚本.而Runtime.getRuntime().exec()方法给我们提供了这个功能,而且Runtime.getRu ...
- java无阻塞执行脚本,JAVA调用Shell脚本-及阻塞的解决方法
JAVA调用Shell脚本--及阻塞的解决办法 用java调用shell,使用 Process p=Runtime.getRuntime().exec(String[] cmd); Runtime.e ...
- hive运行mysql脚本_用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql
1:创建shell脚本 1 touch sqoop_options.sh2 chmod 777 sqoop_options.sh 编辑文件 特地将执行map的个数设置为变量 测试 可以java代码 ...
- powershell执行c语言文件,c语言中调用shell脚本
[转]c语言中调用shell脚本 关于在c语言中调用shell脚本,先是在百度上百度了一下 下面的这个应该是说的比较详细的 dreamerkxz.blog.163.com/blog/static/82 ...
- C语言程序中调用脚本,C语言调用SHELL脚本
在Linux 环境下Shell脚本具有非常强大的功能!使用Shell可以很方便的使用和管理Linux系统,最近学习了一点shell知识,所以一直在 想要是可以在C/C++中调用shell脚本那该有多 ...
- 基于sparksql调用shell脚本运行SQL
[Author]: kwu 基于sparksql调用shell脚本运行SQL,sparksql提供了类似hive中的 -e , -f ,-i的选项 1.定时调用脚本 #!/bin/sh # uplo ...
- Python 调用shell脚本
python调用Shell脚本,有两种方法:os.system(cmd)或os.popen(cmd),前者返回值是脚本的退出状态码,后者的返回值是脚本执行过程中的输出内容. 实际使用时视需求情况而选择 ...
最新文章
- 科技和法律的碰撞——人脸识别为何在旧金山被叫停
- 用户用老版IE访问网站时的jquery提示插件
- c 没有mysql名称空间_MySQL如何删除#sql开头的临时表
- 可视化计算java_Java可视化计算器
- 安装ugjava安装在哪里_讨论!空调安装安全绳该挂哪里
- mysql导入xml 工具下载_XmlToOracle(XML导入Oracle工具)下载_XmlToOracle(XML导入Oracle工具) 版本: V2.0官方版_魅蓝下载...
- vue 两个日期比较大小
- 实验8 群体类、流类库与输入/输出(4学时)
- Java中的生产消费者问题
- mac 下php运行bug
- 如何使用JavaScript重定向到其他网页?
- 苹果发布的Mac Pro就是“渣渣”?网友疯狂吐槽
- 30. 人类将如何变革--走出金字塔模型(下)
- vbs进阶——实用函数之msgbox篇
- 物联网信号测试软件,物联网开关量信号采集 NPN转WIFI 检测计数器
- vue 路由跳转返回上一级
- 干货|app自动化测试之Appium 原理 与 JsonWP 协议分析
- python中的序列是什么_Python中的序列
- 《乔布斯传》圈点(末篇)
- 易验:APP一键登录,就这么简单