Guest User

Untitled

a guest
Jun 24th, 2015
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.64 KB | None | 0 0
  1.  
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.PrintStream;
  6. import java.net.InetSocketAddress;
  7. import java.net.Socket;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Calendar;
  10. import java.util.Scanner;
  11.  
  12. import org.apache.commons.net.telnet.TelnetClient;
  13.  
  14.  
  15.  
  16.  
  17. public class Main {
  18.  
  19.     static private class AutomatedTelnetClient {
  20.         private TelnetClient telnet = new TelnetClient();
  21.         private InputStream in;
  22.         private PrintStream out;
  23.         private String prompt = "#";
  24.  
  25.         public AutomatedTelnetClient(String server, String user, String password) {
  26.             try {
  27. // Connect to the specified server
  28.                 telnet.connect(server, 23);
  29.  
  30. // Get input and output stream references
  31.                 in = telnet.getInputStream();
  32.                 out = new PrintStream(telnet.getOutputStream());
  33.  
  34. // Log the user on
  35.                 readUntil("login:");
  36.                 write(user);
  37.                 readUntil("Password: ");
  38.                 write(password);
  39.  
  40. // Advance to a prompt
  41.                 readUntil(prompt + " ");
  42.             } catch (Exception e) {
  43.                 e.printStackTrace();
  44.             }
  45.         }
  46.  
  47.         public void su(String password) {
  48.             try {
  49.                 write("su");
  50.                 readUntil("Password: ");
  51.                 write(password);
  52.                 prompt = "#";
  53.                 readUntil(prompt + " ");
  54.             } catch (Exception e) {
  55.                 e.printStackTrace();
  56.             }
  57.         }
  58.  
  59.         public String readUntil(String pattern) {
  60.             try {
  61.                 char lastChar = pattern.charAt(pattern.length() - 1);
  62.                 StringBuffer sb = new StringBuffer();
  63.                 boolean found = false;
  64.                 char ch = (char) in.read();
  65.                 while (true) {
  66.                     System.out.print(ch);
  67.                     sb.append(ch);
  68.                     if (ch == lastChar) {
  69.                         if (sb.toString().endsWith(pattern)) {
  70.                             return sb.toString();
  71.                         }
  72.                     }
  73.                     ch = (char) in.read();
  74.                 }
  75.             } catch (Exception e) {
  76.                 e.printStackTrace();
  77.             }
  78.             return null;
  79.         }
  80.  
  81.         public void write(String value) {
  82.             try {
  83.                 out.println(value);
  84.                 out.flush();
  85.                 System.out.println(value);
  86.             } catch (Exception e) {
  87.                 e.printStackTrace();
  88.             }
  89.         }
  90.  
  91.         public String sendCommand(String command) {
  92.             try {
  93.                 write(command);
  94.                 return readUntil(prompt + " ");
  95.             } catch (Exception e) {
  96.                 e.printStackTrace();
  97.             }
  98.             return null;
  99.         }
  100.  
  101.         public void disconnect() {
  102.             try {
  103.                 telnet.disconnect();
  104.             } catch (Exception e) {
  105.                 e.printStackTrace();
  106.             }
  107.         }
  108.     }
  109.         public static boolean testInet() {
  110.             Socket sock = new Socket();
  111.             String site = "google.com";
  112.             InetSocketAddress addr = new InetSocketAddress(site, 80);
  113.             try {
  114.                 sock.connect(addr, 3000);
  115.                 return true;
  116.             } catch (IOException e) {
  117.                 return false;
  118.             } finally {
  119.                 try {
  120.                     sock.close();
  121.                 } catch (IOException e) {
  122.                 }
  123.             }
  124.         }
  125.  
  126.        private static String getTime()
  127.         {
  128.             return new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(Calendar.getInstance().getTime());
  129.         }
  130.  
  131.         public static void main(String[] args) {
  132.             String routerip;
  133.             String username;
  134.             String password;
  135.             System.out.println("started at " + getTime());
  136.             Scanner in = new Scanner(System.in);
  137.             System.out.println("Input \n router ip: ");
  138.  
  139.             routerip = in.nextLine();
  140.             System.out.println("login: ");
  141.             username = in.nextLine();
  142.             System.out.println("password: ");
  143.             password = in.nextLine();
  144.  
  145.             System.out.println("Writen." +
  146.                     "ip: " + routerip + "\n login: " + username
  147.                     + "\n password: " + password + "\n");
  148.             int i = 19;
  149.             try {
  150.                 while (i > 0) {
  151.                     System.out.println(".");
  152.                     Thread.sleep(500);
  153.                     i--;
  154.                 }
  155.             } catch (InterruptedException ex) {
  156.                 ex.printStackTrace();
  157.             }
  158.  
  159.             try {
  160.                 while (true) {
  161.                     if (testInet() == false) {
  162.                         System.out.println("\n no connection at " + getTime() + "\n rebooting... ");
  163.                         AutomatedTelnetClient telnetClient = new
  164.                                 AutomatedTelnetClient(routerip, username, password);
  165.                         telnetClient.sendCommand("reboot");
  166.                         Thread.sleep(60 * 1000 * 3);
  167.                     }
  168.                     else
  169.                         System.out.println("\n there is connection at " + getTime());
  170.                     Thread.sleep(60*1000);
  171.  
  172.                 }
  173.             } catch (InterruptedException ex) {
  174.                 ex.printStackTrace();
  175.             } finally {
  176.                 System.out.println("\n something wrong while connecting. " +
  177.                         "\n Program will be closed.");
  178.             }
  179.  
  180.  
  181.         }
  182.  
  183. }
Advertisement
Add Comment
Please, Sign In to add comment