123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- package space.anyi.autoConnectNetwork;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.firefox.FirefoxDriver;
- import org.openqa.selenium.firefox.FirefoxOptions;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.time.LocalDateTime;
- import java.time.ZoneId;
- /**
- * @ProjectName: autoConnectNetwork
- * @FileName: Main
- * @Author: 杨逸
- * @Data:2024/5/28 8:38
- * @Description:
- */
- public class Main {
- public static void main(String[] args) {
- if (args.length!=4) {
- System.out.println("参数错误");
- System.out.println("用户名,密码,firefoxDriver,firefoxPath未设置正确");
- return;
- }
- String username = args[0];
- String password = args[1];
- String gecko = args[2];
- String firefoxPath = args[3];
- System.setProperty("webdriver.gecko.driver",gecko);
- while (true){
- System.out.println("判断网络是否连接");
- System.out.println(LocalDateTime.now(ZoneId.systemDefault()));
- try {
- if (!connected()){
- // 执行连接操作
- System.out.println("执行连接操作");
- connection(username,password,firefoxPath);
- }
- }catch (Exception e){
- System.out.println(LocalDateTime.now(ZoneId.systemDefault()));
- e.printStackTrace();
- }finally {
- // 每隔60秒检查一次
- try {
- Thread.sleep(1000*60);
- } catch (InterruptedException e) {
- System.gc();
- System.out.println("休眠失败1");
- //throw new RuntimeException(e);
- }
- }
- }
- }
- private static void connection(String username, String password,String firefoxPath){
- FirefoxDriver firefoxDriver = null;
- try {
- System.out.println("注册驱动");
- firefoxDriver = fireFoxDriverInit(firefoxPath);
- //打开网页
- firefoxDriver.get("http://10.30.252.71/eportal/index.jsp?nasip=cc5b64e516a1fa61d915e184b913e171");
- //firefoxDriver.get("http://baidu.com");
- Thread.sleep(200);
- //模拟输入用户名和密码
- firefoxDriver.findElement(By.cssSelector("#username")).sendKeys(username);
- WebElement element = firefoxDriver.findElement(By.cssSelector("#pwd"));
- firefoxDriver.executeScript("arguments[0].value = " + password,element);
- //选择登陆模式
- firefoxDriver.findElement(By.cssSelector("#selectDisname")).click();
- firefoxDriver.findElement(By.cssSelector("#_service_2")).click();
- Thread.sleep(1000);
- //模拟点击登录
- firefoxDriver.findElement(By.cssSelector("#loginLink_div")).click();
- } catch (InterruptedException e) {
- //throw new RuntimeException(e);
- System.err.println("休眠失败2");
- } finally {
- //释放资源
- if (firefoxDriver != null) {
- firefoxDriver.quit();
- }
- System.out.println("释放资源");
- }
- }
- private static FirefoxDriver fireFoxDriverInit(String fireFoxPath) {
- //创建一个浏览器配置对象
- FirefoxOptions options = new FirefoxOptions();
- //设置参数
- //开启无头模式
- options.addArguments("--headless");
- //设置窗口大小
- options.addArguments("--window-size=1920,1080");
- //设置允许所有远程连接
- //options.addArguments("--remote-allow-origins=*");
- //设置请求头
- 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");
- options.setBinary(fireFoxPath);
- return new FirefoxDriver(options);
- }
- /**
- * 判断是否联网
- * @return boolean
- * @throws IOException
- * @description:
- * @author: 杨逸
- * @data:2024/05/28 08:50:05
- * @since 1.0.0
- */
- private static boolean connected(){
- return ping("baidu.com",5);
- }
- /**
- * 判断是否能ping通
- * @param target_name 主机名
- * @param out_time 超时时间
- * @return boolean
- * @throws IOException
- * @description:
- * @author: 杨逸
- * @data:2024/05/28 08:49:12
- * @since 1.0.0
- */
- private static boolean ping(String target_name, int out_time){
- Runtime runtime = Runtime.getRuntime();
- String ping_command = "ping " + target_name + " -w " + out_time;
- System.out.println("命令格式:" + ping_command);
- BufferedReader bufferedReader = null;// windows下编码默认是GBK,Linux是UTF-8
- try {
- Process process = runtime.exec(ping_command);
- if (null == process) return false;
- bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
- String line = null;
- while (null != (line = bufferedReader.readLine())) {
- System.out.println(line);
- if (line.startsWith("bytes from",3))
- return true;
- if (line.startsWith("来自"))
- return true;
- }
- } catch (UnsupportedEncodingException e) {
- //throw new RuntimeException(e);
- System.err.println("编码异常1");
- } catch (IOException e) {
- System.err.println("io异常1");
- //throw new RuntimeException(e);
- } finally {
- try {
- bufferedReader.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return false;
- }
- }
|