0

I'm trying to send config of router to TFTP server using JSF . 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 . This is my session bean code

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,516 INFO [stdout] (default task-11) Connection established.

11:53:25,578 INFO [stdout] (default task-11) Copy.

11:53:25,578 INFO [stdout] (default task-11) IP.

11:53:25,578 INFO [stdout] (default task-11) name.

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 . Can some one help me .

1 Answers1

0

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();
Community
  • 1
  • 1
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992