简体   繁体   English

使用 Java JSch 在防火墙设备上通过 SSH“exec”通道执行多个命令不起作用

[英]Executing multiple commands over SSH "exec" channel on firewall device with Java JSch does not work

I referred the question Multiple bash commands and implemented as below.我提到了问题Multiple bash commands并实现如下。 I am connecting to a device for which first command should be configure then only I will get a prompt to execute all other commands.我正在连接到应为其configure第一个命令的设备,然后只有我才会收到执行所有其他命令的提示。 I don't get output for any of the commands and the control does not return.我没有得到任何命令的输出,控件也没有返回。

The following are the commands that work in terminal.以下是在终端中工作的命令。

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

As asked for, if I do this instead, after entering password the control doesn't return:根据要求,如果我这样做,在输入密码后,控件不会返回:

ssh user@host configure

The script剧本

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();

The "exec" channel on your device seems to be implemented incorrectly.您设备上的“exec”频道似乎未正确实现。 So you cannot use your code with the "exec" channel.所以你不能在“exec”通道中使用你的代码。 As you can "ssh" to the device, it seems that the "shell" channel is fully working.由于您可以“ssh”到设备,因此“shell”通道似乎完全正常工作。 Try talking to your server administrator, to get the server fixed.尝试与您的服务器管理员交谈,以修复服务器。

If fixing the server is not feasible, you will have to revert to using the "shell" channel, although it is generally not the correct way to implement command automation.如果修复服务器不可行,您将不得不恢复使用“shell”通道,尽管这通常不是实现命令自动化的正确方法。
See What is the difference between the 'shell' channel and the 'exec' channel in JSch请参阅JSch 中的“shell”通道和“exec”通道有什么区别

JSch by default enables terminal emulation for the "shell" channel, what will bring lot of unwanted side effects (see Getting unwanted characters when reading command output from SSH server using JSch ). JSch 默认为“shell”通道启用终端仿真,这会带来很多不需要的副作用(请参阅使用 JSch 从 SSH 服务器读取命令输出时获取不需要的字符)。 You may need to disable that by calling setPty .您可能需要通过调用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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM