How to restart modem using command in java
Hi i want to restart my adsl modem using command line in java. but cmd stops when username is requested and needs to be entered. what is the problem?
import java.io.IOException;
public class ExecuteShellComand
{
public static void main(String[] args) throws IOException
{
String tel = "telnet 192.168.1.1 23";
String user = "admin";
String pass = "admin";
String reboot = "reboot";
String command = "cmd /c start cmd.exe /c"+tel+"/c"+user+"/c"+pass+"/c"+reboot;
Process child = Runtime.getRuntime().exec(command);
}
}
source to share
You may also need space between each of your arguments. By typing a command, you will send the command to a prompt that looks like this:
cmd /c start cmd.exe /ctelnet 192.168.1.1 23/cadmin/cadmin/creboot
Add System.out.println command (command); after creating the string to see what is actually in the String.
Change the code as follows:
String command = "cmd /c start cmd.exe /c "+tel+" /c "+user+" /c "+pass+" /c "+reboot;
System.out.println(command);
source to share
You need to send the username and password and the command reboot
to the standard input (stdin) of the telnet command. To do this, create a process and then:
// You /probably/ don't need "cmd /c" here ...
Process child = Runtime.getRuntime().exec(tel);
// Write to stdin of child, force flush after each line
// Yes, you need to get an OutputStream here ... it connected to stdin of the child
PrintWriter out = new PrintWriter(child.getOutputStream(), true);
out.println("admin");
out.println("admin");
out.println("reboot");
Note that you may have to read stdout while sending these commands to prevent blocking the child process. To do this, create a background thread. See this post: http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2
source to share