java - 使用 Java JSch 在防火墙设备上通过 SSH "exec" channel 执行多个命令不起作用

标签 java shell ssh jsch

我提到了问题Multiple bash commands并实现如下。我正在连接到一个设备,其第一个命令应该是 configure,然后只有我会收到执行所有其他命令的提示。我没有得到任何命令的输出,并且控件没有返回。

以下是在终端中运行的命令。

ssh uname@ip
configure # this command changes prompt and enable following commands
move shared pre-rulebase security rules TEST top 
commit
exit
exit

按照要求,如果我这样做,输入密码后控件不会返回:

ssh user@host configure

脚本

String[] commands = new String[]{
    "configure", "move shared pre-rulebase security rules TEST top", "commit", "exit"};

FileWriter fileOut = new FileWriter(outFileName, true);
java.util.Properties config = new java.util.Properties();

config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected");

System.out.println(commands[0]);
ChannelExec channel = (ChannelExec) session.openChannel("exec");
((ChannelExec) channel).setCommand(commands[0]);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);

InputStream in = channel.getInputStream();
outStream = channel.getOutputStream();
channel.connect();

Thread.sleep(1000);

for(int i=1;i<commands.length;i++) {
    System.out.println("Executing:"+commands[i]);
    outStream.write((commands[i]+"\n").getBytes());
    outStream.flush();
}

byte[] tmp = new byte[1024];
while (true) {
    while (in.available() > 0) {
        int i = in.read(tmp, 0, 1024);
        if (i < 0)
            break;
        resultString = new String(tmp, 0, i);                   
        fileOut.write(resultString);
    }
    if (channel.isClosed()) {
        if(in.available()>0) continue; 
        System.out.println("exit-status: " + channel.getExitStatus());
        break;
    }
    try {
        Thread.sleep(1000);
    } catch (Exception ee) {
    }               
}
channel.disconnect();
session.disconnect();
outStream.close();
fileOut.close();

最佳答案

您设备上的“exec” channel 似乎未正确实现。因此,您不能将代码与“exec” channel 一起使用。当您可以“ssh”到设备时,“shell” channel 似乎完全正常工作。尝试与您的服务器管理员联系以修复服务器。

如果修复服务器不可行,您将不得不恢复使用“shell” channel ,尽管这通常不是实现命令自动化的正确方法。
请参阅What is the difference between the 'shell' channel and the 'exec' channel in JSch

JSch 默认情况下启用“shell” channel 的终端仿真,这会带来很多不需要的副作用(请参阅 Getting unwanted characters when reading command output from SSH server using JSch )。您可能需要通过调用setPty来禁用它.

ChannelShell channel = (ChannelShell) session.openChannel("shell"); 

InputStream in = channel.getInputStream(); 
outStream = channel.getOutputStream(); 

channel.setPty(false);
channel.connect(); 

outStream.write('configure\n'.getBytes());  
outStream.write('move shared pre-rulebase security rules TEST top\n'.getBytes());

关于java - 使用 Java JSch 在防火墙设备上通过 SSH "exec" channel 执行多个命令不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56493882/

相关文章:

node.js - 如何列出脚本中分离的进程?

服务器上的 Git 存储库恢复了我的更改,但具有与本地相同的日志

java - 用于 Wifi 状态的 Android Monkeyrunner 插件

java - 如何将数字转换为特定字母?

shell - Scrapy Shell无法正常工作,无法使用命令

linux - 列出登录到特定组的所有用户

java - StringBuilder 在转换为 String 时丢失数据

java - Java Maven 项目中 Jenkins 中带点的环境变量

ssh - 关闭 ssh session 而不在远程计算机上注销

c# - SSH 到服务器,然后 telnet 到设备