Advertisement
Guest User

R850 auto reboot script

a guest
Oct 8th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. import org.openqa.selenium.By;
  2. import org.openqa.selenium.WebDriver;
  3. import org.openqa.selenium.WebElement;
  4. import org.openqa.selenium.chrome.ChromeDriver;
  5. import org.openqa.selenium.chrome.ChromeOptions;
  6. import java.net.InetAddress;
  7. import java.util.List;
  8.  
  9. public class InternetReachable {
  10.     // auto reboot R850 if internet is down
  11.     private static int testCount = 10;
  12.     private static double validityPercentage = 0.5;
  13.     private static String testAddr = "8.8.8.8";
  14.     private static String resetAddr = "http://192.168.128.1/hidden/data-lte.html";
  15.     private static int timeout = 10000;
  16.     public static void main(String[] args) throws Exception {
  17.         monitorProcess();
  18.     }
  19.  
  20.     private static void monitorProcess() throws Exception{
  21.         while(true){
  22.             if(isGoodConnection(testAddr, timeout)){
  23.                 System.out.println("Good connection, check again in 5 minutes.");
  24.                 Thread.sleep(1000 * 60 * 5);
  25.             }else{
  26.                 System.out.println("Bad connection, resetting modem.");
  27.                 System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
  28.  
  29.                 ChromeOptions chromeOptions = new ChromeOptions();
  30.                 chromeOptions.addArguments("--headless");
  31.                 WebDriver driver = new ChromeDriver(chromeOptions);
  32.  
  33.                 driver.get(resetAddr);
  34.                 List<WebElement> applyButtons = driver.findElements(By.cssSelector("button.brick-actor"));
  35.  
  36. //                System.out.println(applyButtons.size());
  37.                 Thread.sleep(1000);
  38.                 if(applyButtons.size() == 0){
  39.                     System.out.println("No connection to R850, wait 1 minute and check again.");
  40.                     Thread.sleep(1000 * 60);
  41.                     continue;
  42.                 }
  43.                 applyButtons.get(1).click();
  44.  
  45.                 WebElement accept = driver.findElement(By.cssSelector("button.modal-btn1"));
  46.                 Thread.sleep(1000);
  47.                 accept.click();
  48.  
  49.                 Thread.sleep(1000);
  50.                 driver.quit();
  51.  
  52.                 System.out.println("R850 rebooted, wait about 1 minute for it to boot up.");
  53.                 System.out.println("Wait 5 minutes for next check.");
  54.                 Thread.sleep(1000 * 60 * 5);
  55.             }
  56.         }
  57.     }
  58.  
  59.     private static boolean isGoodConnection(String address, int timeout) throws Exception{
  60.         InetAddress ia = InetAddress.getByName(address);
  61.         boolean[] flags = new boolean[testCount];
  62.         for(int i = 0; i < testCount; i++)
  63.             flags[i] = ia.isReachable(timeout);
  64.         int validCount = 0;
  65.         for(boolean flag : flags) validCount += flag ? 1 : 0;
  66.         if(validCount >= testCount * validityPercentage) return true;
  67.         return false;
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement