- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我需要找到一种方法来自动向我的路由器发送 ssh 命令。我的目标是在我从我的 Java 程序运行脚本时让路由器重新启动。不过我遇到了一些问题。
首先,这是我从路由器的 ssh 获得的输出序列:首先我做:
ssh root@192.168.100.1
返回:
root@192.168.100.1's password:
我输入密码“admin”。然后转到此提示:
Welcome Visiting Huawei Home Gateway
Copyright by Huawei Technologies Co., Ltd.
Password is default value, please modify it!
WAP>
现在,我输入“reset”,它会重启路由器。
我已经尝试将 Tcl 与 Expect 结合使用,虽然我可以让它在 Windows 上运行,但它在 Linux 上却无法运行。这是我的 Tcl 脚本代码:
#!/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
每当我尝试执行它时,Tcl8.6 都会运行它并在没有实际执行任何操作的情况下终止。但是,如果我在运行 Tcl8.6 时手动输入所有这些命令,它就可以正常工作
我还尝试过 JSch Java 库。有了它,我可以让 Java 程序连接并输出路由器的 shell,但是我尝试发送的任何命令都不起作用。这是其中的代码:
...
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";
但它做同样的事情。任何人都知道我可以做到这一点的任何其他方式吗?
最佳答案
尝试使用 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
看看它是否适合你。
关于java - 需要将 SSH 命令自动化到路由器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38931206/
使用 ui-router-ng2 有哪些优点和缺点?而不是新的 Angular2 路由器?过去我使用 ui-router 和 Angular 1.x 而不是使用 ngRoute,因为我需要更好地支持嵌
本文主要记录个人对小米 WR30U 路由器的解锁和刷机过程,整体步骤与 一般安装流程 类似,但是由于 WR30U 的解锁 ssh 和刷机的过程中有一些细节需要注意,因此记录一下 解锁
我有路由器 { path: 'reset-password', component: ResetPasswordComponent, } 我希望能够将其作为直接链接 (/reset-passw
所以,我试图制作一个具有 Backbone 的示例应用程序,但我无法使路由器工作,对此有什么想法吗?谢谢。这是我的路由器 var Workspace = Backbone.Router.extend(
我正在研究分布式应用程序。网络基于ZMQ(jeromq),这是我的架构: N Clients (Dealer socket) (Router) Proxy (Dealer) 1 (Dealer)
In this plunk我有一个 ui-router $state.goto这仅在第一次有效,即使我增加了 seq变量以强制重新加载。看到时间戳没有改变。这段代码有什么问题? HTML G
我有面包屑组件,测试如下。测试应覆盖75%以上。测试失败,因为 ActivatedRoute.root.children 未定义。谁知道我该如何定义它?我需要至少一个 children 作为 root
我正在使用 RC5 和 "@angular/router": "3.0.0-rc.1" 我似乎无法捕捉到旧路线(示例): /category/subcategory/subcategory/artic
我确实有一个延迟加载模块,当我转到 /lazy 时默认加载 LazyLoadedComponent。到目前为止,没问题。 此组件包含 2 个路由器 socket : 模块有以下路由: const
我试图用不同的参数路由到当前页面,但没有成功。我有一个触发的组合框: this.router.navigate(['page', selectedId]); Url 正在更改,仅此而已。 如何使用不同
我正在尝试使用 React 路由器构建 SPA。当我使用链接导航时,它工作得很好,但是刷新或写入 url 时,它给我 cannot GET/(any route except main)。我试图将 h
我无法在我的 app.js 文件中添加样式。 div 在路线内不起作用。有没有其他方法可以在路由中添加样式? : } /> } /> }> } />
根据用户角色在 react-router 中有条件地呈现路由的最佳方法是什么。我有一个案例,并非所有角色都有权查看某些路线。而且我还需要处理子路线。因此,如果主要路线之一类似于/posts,我只希望
我想使用 Flash 的 RTMFP 对等协议(protocol),但我想知道它是否值得为主流受众开发?据我了解,它使用 UDP,除非用户的防火墙/路由器配置正确,否则它变得毫无用处。 这个问题有解决
我有一 strip 有 waitOn 的路线返回 Meteor.subscribe 的钩子(Hook).每次触发路线时,我都会从我的 loadingTemplate 中看到微调器在看到实际数据之前简要
所以我最近开始尝试使用 createMemoryHistory,因为我正在构建一个将在 iframe 中呈现的应用程序。幸运的是,单击 UI 按钮,我可以在不修改 url 的情况下四处导航。但是,当我
我在我的 React redux 应用程序中使用 connected-react-router。我需要服务器端渲染和客户端渲染(我通过 limenius react bundle 在 symfony
我是一名 ios 开发人员,并试图找出 viper 架构中的路由器和 coordinator 之间的区别。 我真的找不到明确的答案是协调器只是被认为是一种模式,所以路由器毕竟只是某种协调器还是它们有不
我想要这样的东西: if ($state.get("^")) $state.go("^"); else $location.path('/'); 但它显然不起作用(即使没有父状态,它也总是与 $sta
当我从 react-router-dom 使用 BrowserRouter 时,我的路由正在工作。但是为了使用自定义历史记录,我用来自 react-router 的 Router 替换了 Browse
我是一名优秀的程序员,十分优秀!