2016-08-13 64 views
0

我需要找到一种方法来自动执行到我的路由器的ssh命令。我的目标是在我从Java程序运行脚本时重新启动路由器。我有一些问题,但。需要自动执行SSH命令到路由器

首先,这是输出的顺序,我从我的路由器的SSH得到:返回

ssh [email protected] 

: 首先,我做

[email protected]'s password: 

我输入密码,“管理”。接下来到了这样的提示:

Welcome Visiting Huawei Home Gateway 
Copyright by Huawei Technologies Co., Ltd. 

Password is default value, please modify it! 
WAP> 

现在,我输入“复位”,它重新启动路由器。

我已经尝试了Tcl和Expect,虽然我可以在Windows上工作,但它在Linux上不起作用。这是我针对Tcl脚本代码:

#!/bin/sh 
# \ exec tclsh "$0" ${1+"[email protected]"} 
package require Expect 
spawn ssh [email protected] 
send "admin\r" 
send "reset\r" 
after 3000 
exit 

每当我试着执行它,Tcl8.6贯穿它并终止,而无需实际做任何事情。但是,如果我在运行Tcl8.6时手动输入所有这些命令,那么它工作得很好。

我也试过了JSch Java库。因此,我可以让Java程序连接并输出路由器的外壳,但是我尝试发送的任何命令都不会执行任何操作。下面是该代码:

... 
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() > 0) { 
       int i = in.read(tmp, 0, 1024); 
       if (i < 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"); 

这是我得到的输出:

Connect to session... 

Welcome Visiting Huawei Home Gateway 
Copyright by Huawei Technologies Co., Ltd. 

Password is default value, please modify it! 
WAP> 

它只是留在了这里,直到我退出。路由器不重新启动。我也试过:

String command = "reset"; 

但它做同样的事情。任何人都知道我可以做到这一点吗?

+1

我刚刚注意到你的命令以'\ r'结尾。试试'\ n',看看它是否适合你。 – vempo

回答

4

尝试与设备进行交互使用shell而不是exec

这里是一个快速和肮脏的代码,我以前做的话,你可以调整它来满足您的需求:

private static final String PROMPT = ">"; 

public static void main(String[] args) throws Exception { 

    Session session = null; 
    ChannelShell channel = null; 

    try { 

     JSch jsch = new JSch(); 
     session = jsch.getSession("root", "192.168.100.1", 22); 
     session.setConfig("StrictHostKeyChecking", "no"); 
     session.setPassword("admin"); 
     session.connect(); 

     channel = (ChannelShell) session.openChannel("shell"); 

     PipedOutputStream reply = new PipedOutputStream(); 
     PipedInputStream input = new PipedInputStream(reply); 
     ByteArrayOutputStream output = new ByteArrayOutputStream(); 

     channel.setInputStream(input, true); 
     channel.setOutputStream(output, true); 

     channel.connect(); 

     getPrompt(channel, output); 
     writeCommand(reply, "reset"); 
     getPrompt(channel, output); 

    } finally { 

     if (channel != null) { 
      channel.disconnect(); 
     } 

     if (session != null) { 
      session.disconnect(); 
     } 
    } 
} 

private static void writeCommand(PipedOutputStream reply, String command) throws IOException { 
    System.out.println("Command: " + command); 
    reply.write(command.getBytes()); 
    reply.write("\n".getBytes()); 
} 

private static void getPrompt(ChannelShell channel, ByteArrayOutputStream output) 
     throws UnsupportedEncodingException, InterruptedException { 

    while (!channel.isClosed()) { 

     String response = new String(output.toByteArray(), "UTF-8"); 
     System.out.println(response); 
     if (response.trim().endsWith(PROMPT)) { 
      output.reset(); 
      return; 
     } 

     Thread.sleep(100); 
    } 
} 

更新:我注意到,您通过SSH端发送命令与\r。改为尝试\n,看看它是否适合你。

+0

不幸的是,它似乎没有与该代码一起工作。它只是做同样的事情。它输出路由器的外壳,但之后什么都不做。我已经尝试了“\ n”和“\ r”。 – seventy70

+0

它现在似乎在工作。你的代码很好,只是我做了一个小的修改,导致了问题。谢谢! – seventy70

+0

太棒了!你可能想在你的问题更新中解释这种修改,以便其他人知道。或写在这里,我会更新答案。 – vempo