Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.PrintStream;
- import java.net.InetSocketAddress;
- import java.net.Socket;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Scanner;
- import org.apache.commons.net.telnet.TelnetClient;
- public class Main {
- static private class AutomatedTelnetClient {
- private TelnetClient telnet = new TelnetClient();
- private InputStream in;
- private PrintStream out;
- private String prompt = "#";
- public AutomatedTelnetClient(String server, String user, String password) {
- try {
- // Connect to the specified server
- telnet.connect(server, 23);
- // Get input and output stream references
- in = telnet.getInputStream();
- out = new PrintStream(telnet.getOutputStream());
- // Log the user on
- readUntil("login:");
- write(user);
- readUntil("Password: ");
- write(password);
- // Advance to a prompt
- readUntil(prompt + " ");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void su(String password) {
- try {
- write("su");
- readUntil("Password: ");
- write(password);
- prompt = "#";
- readUntil(prompt + " ");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public String readUntil(String pattern) {
- try {
- char lastChar = pattern.charAt(pattern.length() - 1);
- StringBuffer sb = new StringBuffer();
- boolean found = false;
- char ch = (char) in.read();
- while (true) {
- System.out.print(ch);
- sb.append(ch);
- if (ch == lastChar) {
- if (sb.toString().endsWith(pattern)) {
- return sb.toString();
- }
- }
- ch = (char) in.read();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- public void write(String value) {
- try {
- out.println(value);
- out.flush();
- System.out.println(value);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public String sendCommand(String command) {
- try {
- write(command);
- return readUntil(prompt + " ");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- public void disconnect() {
- try {
- telnet.disconnect();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public static boolean testInet() {
- Socket sock = new Socket();
- String site = "google.com";
- InetSocketAddress addr = new InetSocketAddress(site, 80);
- try {
- sock.connect(addr, 3000);
- return true;
- } catch (IOException e) {
- return false;
- } finally {
- try {
- sock.close();
- } catch (IOException e) {
- }
- }
- }
- private static String getTime()
- {
- return new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(Calendar.getInstance().getTime());
- }
- public static void main(String[] args) {
- String routerip;
- String username;
- String password;
- System.out.println("started at " + getTime());
- Scanner in = new Scanner(System.in);
- System.out.println("Input \n router ip: ");
- routerip = in.nextLine();
- System.out.println("login: ");
- username = in.nextLine();
- System.out.println("password: ");
- password = in.nextLine();
- System.out.println("Writen." +
- "ip: " + routerip + "\n login: " + username
- + "\n password: " + password + "\n");
- int i = 19;
- try {
- while (i > 0) {
- System.out.println(".");
- Thread.sleep(500);
- i--;
- }
- } catch (InterruptedException ex) {
- ex.printStackTrace();
- }
- try {
- while (true) {
- if (testInet() == false) {
- System.out.println("\n no connection at " + getTime() + "\n rebooting... ");
- AutomatedTelnetClient telnetClient = new
- AutomatedTelnetClient(routerip, username, password);
- telnetClient.sendCommand("reboot");
- Thread.sleep(60 * 1000 * 3);
- }
- else
- System.out.println("\n there is connection at " + getTime());
- Thread.sleep(60*1000);
- }
- } catch (InterruptedException ex) {
- ex.printStackTrace();
- } finally {
- System.out.println("\n something wrong while connecting. " +
- "\n Program will be closed.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment