跳至主要內容

自动拨号rasdial

chanchaw大约 1 分钟javaspring

源码

使用 rasdial 拨号 vpn 的代码,制作 jar 项目,下面是全部代码

package com.cc.autowpm.utils;

/**
 * @author chanchaw
 * @create 2023-05-12 10:53
 */
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;

/**
 * wpm = w砍掉一半+p+m砍掉一半 = 不可描述的神秘力量
 * 之前做过一次该功能的项目放在 gitee 上,结果源码都给屏蔽了
 * 真是日了狗了
 */
@Component
@Configuration
@EnableScheduling
public class WpmUtils {
    @Value("${globalv.wpmname}")
    private String wpmName;

    @Value("${globalv.account}")
    private String account;

    @Value("${globalv.password}")
    private String pwd;
    @Value("${globalv.testip}")
    private String testip;

    @Scheduled(fixedDelayString = "${globalv.fixedDelay}")
    public void autoDial(){
        boolean b = checkIpStatus(testip);
        if(!b) {
            dialWpm();
        }else{
            System.out.println("wpm在线,不需要拨号!");
        }
    }

    public static boolean checkIpStatus(String ipAddress) {
        boolean reachable = false;
        try {
            reachable = InetAddress.getByName(ipAddress).isReachable(3000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return reachable;
    }
    public void dialWpm() {
        String wpmConnectionName = wpmName;
        String username = account;
        String password = pwd;

        try {
            // 构建命令行
            String[] cmd = {"rasdial", wpmConnectionName, username, password};

            // 创建进程并执行命令
            ProcessBuilder builder = new ProcessBuilder(cmd);
            Process process = builder.start();

            // 读取命令执行的输出信息
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            // 等待命令执行完成
            int exitCode = process.waitFor();
            System.out.println("Command executed with exit code: " + exitCode);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

下面是配置文件

server:
  port: 7001
  servlet:
    context-path: /autowpm
globalv:
  wpmname: leda3cloud
  account: chanchaw
  password: "密码"
  testip: "17.71.0.1"
  fixedDelay: 5000

错误与提示

使用 WinSW 制作 windows 系统服务,查看日志文件 WinSW.NET4.out.log 提示错误623要在服务中设置登录账号,即当前 windows 的登录账号 自动拨号