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.
First of all, this is the sequence of output I get from my router's ssh: First I do:
ssh [email protected]
which returns:
[email protected]'s password:
I enter the password, "admin". It then goes to this prompt:
Welcome Visiting Huawei Home Gateway
Copyright by Huawei Technologies Co., Ltd.
Password is default value, please modify it!
WAP>
Now, I input "reset" and it restarts the router.
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:
#!/bin/sh
# \ exec tclsh "$0" ${1+"$@"}
package require Expect
spawn ssh [email protected]
send "admin\r"
send "reset\r"
after 3000
exit
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
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:
...
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");
This is the output that I get:
Connect to session...
Welcome Visiting Huawei Home Gateway
Copyright by Huawei Technologies Co., Ltd.
Password is default value, please modify it!
WAP>
It just stays here until I exit. The router doesn't restart. I've also tried:
String command = "reset";
but it does the same thing. Anybody know of any other ways I could do this?
\r. Try\ninstead and see if it works for you.