Main.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package space.anyi.autoConnectNetwork;
  2. import org.openqa.selenium.By;
  3. import org.openqa.selenium.WebElement;
  4. import org.openqa.selenium.firefox.FirefoxDriver;
  5. import org.openqa.selenium.firefox.FirefoxOptions;
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.io.UnsupportedEncodingException;
  10. import java.time.LocalDateTime;
  11. import java.time.ZoneId;
  12. /**
  13. * @ProjectName: autoConnectNetwork
  14. * @FileName: Main
  15. * @Author: 杨逸
  16. * @Data:2024/5/28 8:38
  17. * @Description:
  18. */
  19. public class Main {
  20. public static void main(String[] args) {
  21. if (args.length!=4) {
  22. System.out.println("参数错误");
  23. System.out.println("用户名,密码,firefoxDriver,firefoxPath未设置正确");
  24. return;
  25. }
  26. String username = args[0];
  27. String password = args[1];
  28. String gecko = args[2];
  29. String firefoxPath = args[3];
  30. System.setProperty("webdriver.gecko.driver",gecko);
  31. while (true){
  32. System.out.println("判断网络是否连接");
  33. System.out.println(LocalDateTime.now(ZoneId.systemDefault()));
  34. try {
  35. if (!connected()){
  36. // 执行连接操作
  37. System.out.println("执行连接操作");
  38. connection(username,password,firefoxPath);
  39. }
  40. }catch (Exception e){
  41. System.out.println(LocalDateTime.now(ZoneId.systemDefault()));
  42. e.printStackTrace();
  43. }finally {
  44. // 每隔60秒检查一次
  45. try {
  46. Thread.sleep(1000*60);
  47. } catch (InterruptedException e) {
  48. System.gc();
  49. System.out.println("休眠失败1");
  50. //throw new RuntimeException(e);
  51. }
  52. }
  53. }
  54. }
  55. private static void connection(String username, String password,String firefoxPath){
  56. FirefoxDriver firefoxDriver = null;
  57. try {
  58. System.out.println("注册驱动");
  59. firefoxDriver = fireFoxDriverInit(firefoxPath);
  60. //打开网页
  61. firefoxDriver.get("http://10.30.252.71/eportal/index.jsp?nasip=cc5b64e516a1fa61d915e184b913e171");
  62. //firefoxDriver.get("http://baidu.com");
  63. Thread.sleep(200);
  64. //模拟输入用户名和密码
  65. firefoxDriver.findElement(By.cssSelector("#username")).sendKeys(username);
  66. WebElement element = firefoxDriver.findElement(By.cssSelector("#pwd"));
  67. firefoxDriver.executeScript("arguments[0].value = " + password,element);
  68. //选择登陆模式
  69. firefoxDriver.findElement(By.cssSelector("#selectDisname")).click();
  70. firefoxDriver.findElement(By.cssSelector("#_service_2")).click();
  71. Thread.sleep(1000);
  72. //模拟点击登录
  73. firefoxDriver.findElement(By.cssSelector("#loginLink_div")).click();
  74. } catch (InterruptedException e) {
  75. //throw new RuntimeException(e);
  76. System.err.println("休眠失败2");
  77. } finally {
  78. //释放资源
  79. if (firefoxDriver != null) {
  80. firefoxDriver.quit();
  81. }
  82. System.out.println("释放资源");
  83. }
  84. }
  85. private static FirefoxDriver fireFoxDriverInit(String fireFoxPath) {
  86. //创建一个浏览器配置对象
  87. FirefoxOptions options = new FirefoxOptions();
  88. //设置参数
  89. //开启无头模式
  90. options.addArguments("--headless");
  91. //设置窗口大小
  92. options.addArguments("--window-size=1920,1080");
  93. //设置允许所有远程连接
  94. //options.addArguments("--remote-allow-origins=*");
  95. //设置请求头
  96. options.addArguments("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36");
  97. options.setBinary(fireFoxPath);
  98. return new FirefoxDriver(options);
  99. }
  100. /**
  101. * 判断是否联网
  102. * @return boolean
  103. * @throws IOException
  104. * @description:
  105. * @author: 杨逸
  106. * @data:2024/05/28 08:50:05
  107. * @since 1.0.0
  108. */
  109. private static boolean connected(){
  110. return ping("baidu.com",5);
  111. }
  112. /**
  113. * 判断是否能ping通
  114. * @param target_name 主机名
  115. * @param out_time 超时时间
  116. * @return boolean
  117. * @throws IOException
  118. * @description:
  119. * @author: 杨逸
  120. * @data:2024/05/28 08:49:12
  121. * @since 1.0.0
  122. */
  123. private static boolean ping(String target_name, int out_time){
  124. Runtime runtime = Runtime.getRuntime();
  125. String ping_command = "ping " + target_name + " -w " + out_time;
  126. System.out.println("命令格式:" + ping_command);
  127. BufferedReader bufferedReader = null;// windows下编码默认是GBK,Linux是UTF-8
  128. try {
  129. Process process = runtime.exec(ping_command);
  130. if (null == process) return false;
  131. bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
  132. String line = null;
  133. while (null != (line = bufferedReader.readLine())) {
  134. System.out.println(line);
  135. if (line.startsWith("bytes from",3))
  136. return true;
  137. if (line.startsWith("来自"))
  138. return true;
  139. }
  140. } catch (UnsupportedEncodingException e) {
  141. //throw new RuntimeException(e);
  142. System.err.println("编码异常1");
  143. } catch (IOException e) {
  144. System.err.println("io异常1");
  145. //throw new RuntimeException(e);
  146. } finally {
  147. try {
  148. bufferedReader.close();
  149. } catch (IOException e) {
  150. throw new RuntimeException(e);
  151. }
  152. }
  153. return false;
  154. }
  155. }