title: How to make a ssh connection to a firewall(router) with java?
body: <p>For some reason,I need to connect to a firewall(based on Linux) and add some rules with Java.</p>
  
  <p>After searching with google for a while, I found that <strong>jsch</strong> is my best choice.But when I </p>
  
  <p>use it to execute a command,"<strong>show hostname</strong>" for example, error is returned.If I </p>
  
  <p>execute commands like <strong>"ls -l"</strong> and <strong>"whoami"</strong>,the results are ok.That's to say,I can only </p>
  
  <p>connect to the Bash Shell！ My question is how can I connect to the firewall shell?</p>
  
  <p>My test code as follows：</p>
  
  <pre><code>import java.io.InputStream;
  
  import com.jcraft.jsch.ChannelExec;
  import com.jcraft.jsch.JSch;
  import com.jcraft.jsch.Session;
  import com.jcraft.jsch.UserInfo;
  
  public class SSHDemo {
      public static void main(String[] args) {
          SSHDemo t = new SSHDemo();
          try {
              t.go();
          } catch (Exception ex) {
              ex.printStackTrace();
          }
      }
  
      public void go() throws Exception {
          String host = "10.4.44.192";
          String user = "root";
          String password = "root";
          int port = 22;
  //      int tunnelLocalPort = 9080;
  //      String tunnelRemoteHost = "10.4.44.192";
  //      int tunnelRemotePort = 8000;
          JSch jsch = new JSch();
          Session session = jsch.getSession(user, host, port);
          session.setPassword(password);
          localUserInfo lui = new localUserInfo();
          session.setUserInfo(lui);
          session.connect();
  
          ChannelExec channel = (ChannelExec)session.openChannel("exec");
          channel.setInputStream(null);
  
              //Pay attension to this line
          channel.setCommand("show hostname");
          channel.setErrStream(System.err);
          channel.connect();
          InputStream in=channel.getInputStream();
          int i=0;
          byte[] tmp=new byte[1024*1024];
          while ((i = in.read(tmp, 0, tmp.length)) != -1) {
              System.out.write(tmp, 0, i);
          }
          in.close();
  
  
          channel.disconnect();
          session.disconnect();
          // session.setPortForwardingL(tunnelLocalPort,tunnelRemoteHost,tunnelRemotePort);
          System.out.println("Connected");
  
      }
  
  
      class localUserInfo implements UserInfo {
          String passwd;
  
          public String getPassword() {
              return passwd;
          }
  
          public boolean promptYesNo(String str) {
              return true;
          }
  
          public String getPassphrase() {
              return null;
          }
  
          public boolean promptPassphrase(String message) {
              return true;
          }
  
          public boolean promptPassword(String message) {
              return true;
          }
  
          public void showMessage(String message) {
          }
      }
  }
  </code></pre>