Skip to content
Advertisement

Need to automate SSH commands to router

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:

JavaScript

which returns:

JavaScript

I enter the password, “admin”. It then goes to this prompt:

JavaScript

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:

JavaScript

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:

JavaScript

This is the output that I get:

JavaScript

It just stays here until I exit. The router doesn’t restart. I’ve also tried:

JavaScript

but it does the same thing. Anybody know of any other ways I could do this?

Advertisement

Answer

Try interacting with the device using shell instead of exec.

Here is a quick-and-dirty code I used to do so, you can adjust it to suit your needs:

JavaScript

UPDATE: I have noticed that the command you send via SSH ends with r. Try n instead and see if it works for you.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement