java jsch shell_如何在字符串中获取jsch shell命令输出
小编典典
OP可能不再需要我的解决方案,但是正在寻找能够同时满足这两个条件的解决方案的其他任何人:1)等待命令在远程计算机上完成;2)将输出捕获为字符串;您可以尝试以下方法:
public class SshConnectionManager {
private static Session session;
private static ChannelShell channel;
private static String username = "";
private static String password = "";
private static String hostname = "";
private static Session getSession(){
if(session == null || !session.isConnected()){
session = connect(hostname,username,password);
}
return session;
}
private static Channel getChannel(){
if(channel == null || !channel.isConnected()){
try{
channel = (ChannelShell)getSession().openChannel("shell");
channel.connect();
}catch(Exception e){
System.out.println("Error while opening channel: "+ e);
}
}
return channel;
}
private static Session connect(String hostname, String username, String password){
JSch jSch = new JSch();
try {
session = jSch.getSession(username, hostname, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(password);
System.out.println("Connecting SSH to " + hostname + " - Please wait for few seconds... ");
session.connect();
System.out.println("Connected!");
}catch(Exception e){
System.out.println("An error occurred while connecting to "+hostname+": "+e);
}
return session;
}
private static void executeCommands(List commands){
try{
Channel channel=getChannel();
System.out.println("Sending commands...");
sendCommands(channel, commands);
readChannelOutput(channel);
System.out.println("Finished sending commands!");
}catch(Exception e){
System.out.println("An error ocurred during executeCommands: "+e);
}
}
private static void sendCommands(Channel channel, List commands){
try{
PrintStream out = new PrintStream(channel.getOutputStream());
out.println("#!/bin/bash");
for(String command : commands){
out.println(command);
}
out.println("exit");
out.flush();
}catch(Exception e){
System.out.println("Error while sending commands: "+ e);
}
}
private static void readChannelOutput(Channel channel){
byte[] buffer = new byte[1024];
try{
InputStream in = channel.getInputStream();
String line = "";
while (true){
while (in.available() > 0) {
int i = in.read(buffer, 0, 1024);
if (i < 0) {
break;
}
line = new String(buffer, 0, i);
System.out.println(line);
}
if(line.contains("logout")){
break;
}
if (channel.isClosed()){
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee){}
}
}catch(Exception e){
System.out.println("Error while reading channel output: "+ e);
}
}
public static void close(){
channel.disconnect();
session.disconnect();
System.out.println("Disconnected channel and session");
}
public static void main(String[] args){
List commands = new ArrayList();
commands.add("ls -l");
executeCommands(commands);
close();
}
}
如果您需要一次发送多个命令并保持通道打开以供以后使用,则此解决方案也很有用。
2020-11-19
java jsch shell_如何在字符串中获取jsch shell命令输出相关推荐
- String字符串中获取所有匹配结果的索引值
String字符串中获取所有匹配结果的索引值 例如现在我们有这样一段代码 public interface ActErrorHisMapper {public List<ActError> ...
- java 普通类request_[Java教程]spring在普通类中获取session和request
[Java教程]spring在普通类中获取session和request 0 2014-08-12 08:01:13 在使用spring时,经常需要在普通类中获取session,request等对像. ...
- java 从一个容器获取对象,Java 如何实现从spring容器中获取注入的bean对象
Java 如何实现从spring容器中获取注入的bean对象 发布时间:2020-11-03 15:27:37 来源:亿速云 阅读:70 作者:Leah 这期内容当中小编将会给大家带来有关Java 如 ...
- java反序列化漏洞POP查找_利用 Java 反序列化漏洞在受限环境下获取反向 Shell
原标题:利用 Java 反序列化漏洞在受限环境下获取反向 Shell 原文链接: https://medium.com/abn-amro-red-team/java-deserialization-f ...
- 编写程序将字符串中最长的单词输出
<程序设计基础-c语言>杨莉 刘鸿翔 ISBN-978-7-03-032903-5 p241 习题7 8.写一个函数,输入一段英文,将此字符串中最长的单词输出 #include<st ...
- 用一个函数来实现将一行字符串中最长的单词输出。此行字符串从主函数传递给该函数.
/2.用一个函数来实现将一行字符串中最长的单词输出.此行字符串从主函数传递给该函数./ #include <stdio.h> #include <string.h> int m ...
- (C语言)输入一行字符,将此字符串中最长的单词输出。
输入一行字符,将此字符串中最长的单词输出. #include<stdio.h> #include<string.h> int main() {char a[100], b[10 ...
- python中command是什么意思_python中command执行shell命令脚本方法
在Python中有一个模块commands也很容易做到以上的效果. 看一下三个函数: 1). commands.getstatusoutput(cmd) 用os.popen()执行命令cmd, 然后返 ...
- mongodb中常用的shell命令
mongodb中常用的shell命令 常用的shell命令 1.帮助命令 2.数据库操作命令 3.创建数据库和集合 4.集合中的文档(document)操作 5.聚集集合查询 常用的shell命令 1 ...
最新文章
- [原创] 浅谈ETL系统架构如何测试?
- python web开发-flask中response,cookies,session对象使用详解
- 拥抱.NET Core,学习.NET Core的基础知识补遗
- php oracle817,Oracle 817 For Linux/Unix安装文档-数据库专栏,ORACLE
- java web 手机验证_JAVA-WEB,好知网,登录注册,手机验证
- SDUT 1157-小鼠迷宫问题(BFSamp;DFS)
- VMware 虚拟机如何连接网络,且远程可连接
- android mmkv使用_锦囊篇|一文摸懂SharedPreferences和MMKV(一)
- 【图像处理】美图秀秀使用技巧:抠图、透明、改色、教程
- (三)表单操作——提交表单-添加name属性 e.detail.value获取表单数据
- OC Foundation框架 字典
- Combating Spyware in the Enterprise
- 云端服务器(linux版)部署mysql
- 重装系统——Win10/win11
- 多用途数据主导“物联网未来”的实施
- 输入框根据拼音首字母/中文字符联想补全
- 菜鸟学习OGRE和天龙八部之一:OGRE+MFC+OIS
- 在内网使用DBL落地网关,连接公网的freeswitch
- ios 渐变透明背景_渐变色彩的室内应用技巧
- 数字图像处理笔记——酉变换( Unitary image transforms)
热门文章
- Linux 操作系统开篇!
- 建一个 5G 基站,到底要花多少钱?
- 神龙神龙你擦亮眼,阿里巴巴要“上天”!
- 从 0 经验到成为全球第一模组生产商,日海智能的「非典型」物联网之路
- 硬核干货! 你的业务是否适合上链、上链有那些坑?dfuse、慢雾、MYKEY大佬为你为你指点迷津……...
- 牛!这本 Python 3.6 的书火爆了 IT 圈!
- Amazon亲儿子MXNet与其他框架有哪些不同?| 赠书
- JavaScript 中的垃圾回收和内存泄露如何处理?| 技术头条
- 特斯拉起诉小鹏汽车员工窃取 30 多万份商业机密,何小鹏回应
- TOP 1%的软件工程师和其他 99%有什么不同?