gpt4 book ai didi

java - JSCH依次执行并打印命令输出

转载 作者:行者123 更新时间:2023-12-01 10:21:57 24 4
gpt4 key购买 nike

Jsch 在 shell 模式下我想顺序执行命令。我尝试使用下面的代码。代码的问题是代码等待 readLine()。任何人都可以提出解决方案吗?如果可以的话请修复以下代码

注意:我只想在 shell 模式下执行命令

public class SSHClient1 {

private Session session;
private ChannelShell channel;
private String username = "";
private String password = "";
private String hostname = "";

public SSHClient1(String username, String password, String hostname) throws Exception {
super();
this.username = username;
this.password = password;
this.hostname = hostname;
startSession();
intializeChannel();
}

private void startSession() throws Exception{
if(session == null || !session.isConnected()){
session = connect(hostname,username,password);
}
}

private void intializeChannel() throws Exception{
if(channel == null || !channel.isConnected()){
try{
channel = (ChannelShell)session.openChannel("shell");
channel.setPty(false);
channel.connect();
}catch(Exception e){
}
}
}

private Session connect(String hostname, String username, String password) throws Exception{
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);
session.connect();
System.out.println("Connected successfully to host "+ hostname);
}catch(Exception e){
}
return session;
}

public void executeCommands(List<String> commands) throws Exception{
PrintStream out = null;
try{
out = new PrintStream(channel.getOutputStream());
for(String command : commands){
out.println(command);
out.flush();
printResult();
}

}catch(Exception e){
} finally{
if(out != null)
out.close();

}
}

private void printResult() throws IOException{
try{
InputStream in = channel.getInputStream();
String line = "";
BufferedReader br =new BufferedReader(new InputStreamReader(in));
while((line = br.readLine()) != null){
System.out.println(line);
}
}catch(Exception e){
}

}


public void close(){
if(channel != null)
channel.disconnect();
if(session != null)
session.disconnect();
}


public static void main(String[] args) throws Exception{

SSHClient1 ssh = new SSHClient1("admin", "password", "192.168.2.9");
List<String> commands = new ArrayList<String>();
commands.add("enable");
commands.add("configure terminal");
commands.add("show running-config");
ssh.executeCommands(commands);
ssh.close();

}
}

如果我更改上述代码的两个方法,我就能够实现我想要的,该方法的问题是我需要在创建缓冲区后放置一个Thread.sleep,否则它不会打印任何东西。

public void executeCommands(List<String> commands) throws Exception{
PrintStream out = null;
try{
out = new PrintStream(channel.getOutputStream(),true);
for(String command : commands){
out.println(command);
out.flush();
printResult();
}
}catch(Exception e){
} finally{
if(out != null)
out.close();

}
}

private void printResult() throws IOException{
try{
InputStream in = channel.getInputStream();
BufferedReader br =new BufferedReader(new InputStreamReader(in));
Thread.sleep(10000);
boolean ready = false;
int c = 0;
StringBuilder line = new StringBuilder();
while((ready = br.ready()) == true){
ready = br.ready();
c = br.read();
System.out.print(String.valueOf((char)c));
}
}catch(Exception e){
}
}

最佳答案

如果可以选择包装命令,您可以这样做:

# cat /usr/bin/your-enable
#!/bin/bash
enable
echo "#END#"

更改主要内容(示例):

 commands.add("/usr/bin/your-enable");

更改打印结果:

while((line = br.readLine()) != null){

if (line.equals("#END#")) {
break;
} else {
System.out.println(line);
}
}

希望有帮助。

关于java - JSCH依次执行并打印命令输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35537473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com