简体   繁体   English

使用JSF为通过JSch通过SSH执行的命令提供输入/子命令

[英]Providing input/subcommands to command executed over SSH with JSch Using JSF

I'm trying to send config of router to TFTP server using JSF . 我正在尝试使用JSF将路由器的配置发送到TFTP服务器。 When i test my code with Main class it works but when i try to integrate this code into button action's it doesn't work . 当我使用Main类测试我的代码时,它可以工作,但是当我尝试将此代码集成到按钮动作中时,它不起作用。 This is my session bean code 这是我的会话bean代码

public Void SendConfigViaTftp(Router r) {
        int port=22;
        String name = r.getRouterName();
        String ip=r.getRouterIP();
        String password =r.getRouterPassword();
        try
            {
            JSch jsch = new JSch();
            Session session = jsch.getSession(name, ip, port);
                session.setPassword(password);
                session.setConfig("StrictHostKeyChecking", "no");
            System.out.println("Establishing Connection...");
            session.connect();
                System.out.println("Connection established.");


                ChannelExec channelExec = (ChannelExec)session.openChannel("exec");

                InputStream in = channelExec.getInputStream();
             channelExec.setCommand("enable");

         channelExec.setCommand("copy run tftp:");
         OutputStream out = channelExec.getOutputStream();

         channelExec.connect();

         System.out.println("Copy.");
         out.write(("192.168.18.1 \n").getBytes());
         System.out.println("IP.");
         out.write(name.getBytes());
         System.out.println("name.");
         out.flush();
         out.close();


                session.disconnect();
                return true;


                }
        catch(Exception e){System.err.print(e);

       }


}

This is the output : 这是输出:

11:53:25,279 INFO [stdout] (default task-11) Establishing Connection... 11:53:25,279 INFO [stdout](默认任务11)建立连接...

11:53:25,516 INFO [stdout] (default task-11) Connection established. 11:53:25,516 INFO [stdout](默认任务11)已建立连接。

11:53:25,578 INFO [stdout] (default task-11) Copy. 11:53:25,578 INFO [stdout](默认任务11)复制。

11:53:25,578 INFO [stdout] (default task-11) IP. 11:53:25,578 INFO [stdout](默认任务11)IP。

11:53:25,578 INFO [stdout] (default task-11) name. 11:53:25,578 INFO [stdout](默认任务11)名称。

This is the code of my button 这是我按钮的代码

<p:commandButton value="Sauvegarder(TFTP)" action="#{ListBean.sauvegardeTFTP(rtr)}" update=":routeurs" ><f:ajax disabled="true"/></p:commandButton>

I'm sure that the problem is my jsf application has a problem with the OutputStream . 我确定问题是我的jsf应用程序的OutputStream存在问题。 Can some one help me . 有人能帮我吗 。

You didn't provide us any information, that we can use to debug your problem. 您没有提供任何信息,我们可以用来调试您的问题。 "it doesn't work" is not a problem description. “不起作用”不是问题描述。


Anyway, one obvious problem is, that you have removed the code that reads the command output and you have not replaced it with other way to wait for the command to finish. 无论如何,一个明显的问题是,您已经删除了读取命令输出的代码,而没有用其他方式替换它来等待命令完成。 So it's quite possible that the connection is simply terminated before the commands finishes, killing the command with it. 因此很有可能只是在命令完成之前就终止了连接,从而终止了该命令。

Wait for the channel to be closed, before you close the session: 在关闭会话之前,请等待通道关闭:

while (!channelExec.isClosed()) Thread.sleep(100);

Or keep your command output stream reading code from your original question (you do not have to pass the output anywhere): 或者让您的命令输出流从原始问题中读取代码 (您不必将输出传递到任何地方):

InputStream in = channelExec.getInputStream();

// ...

BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null)
{
}

session.disconnect();

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

相关问题 使用 JSch 为通过 SSH 执行的命令提供输入/子命令 - Providing input/subcommands to command executed over SSH with JSch 为使用 JSch 在 SSH 服务器上执行的命令提供多行输入 - Provide multiple lines of input to command executed on SSH server with JSch 使用 JSch 通过 SSH 运行命令 - Run a command over SSH with JSch 未执行使用 ChannelExec 的命令 - Jsch - Command using ChannelExec not executed - Jsch 执行命令后,Jsch Java ssh客户端未断开连接 - Jsch java ssh client is not getting disconnected after command gets executed 当我使用JSCH通过SSH运行命令时,退出状态2 - ExitStatus 2 when I run a command over SSH with JSCH 使用Jsch SSH到我的Raspberry Pi和Shutdown命令不起作用… - Using Jsch to SSH to my Raspberry Pi and Shutdown command not working… 使用JSch在远程SSH会话上运行telnet命令 - Running telnet command on remote SSH session using JSch 在单独的控制台/ cmd窗口中(而不是在IDE控制台中)显示用JSch执行的SSH命令的结果 - Show results of SSH command executed with JSch in separate console/cmd window, not in IDE console 使用JSch执行的“ sudo”命令需要密码,即使在交互式SSH会话中不需要密码时也是如此 - “sudo” command executed with JSch requires password, even when the password is not required in an interactive SSH session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM