title: Need to automate SSH commands to router
body: <p>I need to find a way to automate ssh commands to my router. My goal is to make the router restart whenever I run the script from my Java program. I'm having some issues though. </p>
  
  <p>First of all, this is the sequence of output I get from my router's ssh:
  First I do:</p>
  
  <pre><code>ssh root@192.168.100.1
  </code></pre>
  
  <p>which returns:</p>
  
  <pre><code>root@192.168.100.1's password: 
  </code></pre>
  
  <p>I enter the password, "admin". It then goes to this prompt:</p>
  
  <pre><code>Welcome Visiting Huawei Home Gateway
  Copyright by Huawei Technologies Co., Ltd.
  
  Password is default value, please modify it!
  WAP&gt;
  </code></pre>
  
  <p>Now, I input "reset" and it restarts the router.</p>
  
  <p>I've tried Tcl with Expect and while I can get it working on Windows, it doesn't work on Linux. Here's my code for the Tcl script:</p>
  
  <pre><code>#!/bin/sh
  # \ exec tclsh "$0" ${1+"$@"}
  package require Expect
  spawn ssh root@192.168.100.1
  send "admin\r"
  send "reset\r"
  after 3000
  exit
  </code></pre>
  
  <p>Whenever I try to execute it, Tcl8.6 runs through it and terminates without actually doing anything. However, if I manually input all of these commands while running Tcl8.6, it works just fine</p>
  
  <p>I've also tried the JSch Java library. With that, I can get the Java program to connect and output the shell of the router, but any command that I try to send does nothing. Here's the code from that:</p>
  
  <pre><code>    ...
  JSch jsch = new JSch();
  
          Session session = jsch.getSession("root", "192.168.100.1", 22);
  
          Properties config = new Properties();
          config.put("StrictHostKeyChecking", "no");
          session.setConfig(config);
  
          // Skip prompting for the password info and go direct...
          session.setPassword("admin");
          session.connect();
  
          String command = "reset\r";
  
          Channel channel = session.openChannel("exec");
          ((ChannelExec) channel).setCommand(command);
  
          ((ChannelExec) channel).setErrStream(System.err);
  
          InputStream in = channel.getInputStream();
  
          System.out.println("Connect to session...");
          channel.connect();
  
  
          byte[] tmp = new byte[1024];
          while (true) {
              while (in.available() &gt; 0) {
                  int i = in.read(tmp, 0, 1024);
                  if (i &lt; 0) {
                      break;
                  }
                  System.out.print(new String(tmp, 0, i));
              }
              if (channel.isClosed()) {
                  System.out.println("exit-status: " + channel.getExitStatus());
                  break;
              }
              try {
                  Thread.sleep(1000);
              } catch (Exception ee) {
              }
          }
          channel.disconnect();
          session.disconnect();
          System.out.println("disconnected");
  </code></pre>
  
  <p>This is the output that I get:</p>
  
  <pre><code>Connect to session...
  
  Welcome Visiting Huawei Home Gateway
  Copyright by Huawei Technologies Co., Ltd.
  
  Password is default value, please modify it!
  WAP&gt;
  </code></pre>
  
  <p>It just stays here until I exit. The router doesn't restart. I've also tried:</p>
  
  <pre><code>String command = "reset";
  </code></pre>
  
  <p>but it does the same thing. Anybody know of any other ways I could do this?</p>