自动连接需要登录认证的WIFI和切换电脑模式

1、有的WIFI在使用时需要进入登录网址输入账号和密码,并且长时间未操作电脑后网络会掉线需进入登录网址才能重新连接互联网,本代码旨在解决此问题。定义了函数connect_wifi(ssid, password)用于自动连接WIFI,无需手动输入此WIFI的密码。

2、在规定的时间段内切换电脑模式为预先设定的模式,以联想拯救者Y7000P2023款为例,自由切换电脑模式为安静、均衡、野兽和超能模式,如在00:00:00~08:00:00这段时间为野兽模式,在08:00:01~23:59:59又自动切换为均衡模式,而无需手动打开Legion Zone软件手动切换或按键盘快捷键切换。

3、可关闭电脑的登陆密码,然后将bat文件放入启动文件夹里,这样便可实现开机后自动连接wifi

win+r打开启动文件夹,然后输入如下代码

先创建txt文件,里面输入类似如下的代码,然后改后缀名为bat,电脑开机后便会自动运行此py文件。bat里的代码意思就是指定编译器为通过conda创建的环境yolo里的python,然后便可通过python运行py文件里的代码了。

从上可看出这是为了解决远程操作电脑而生的,如果电脑就在你面前当然就无需这些了,远程就怕连接了WIFI却又掉网了,而本代码能确保长时间未操作电脑网络掉线后能迅速重新自动接入互联网。

import os
import webbrowser
import pyautogui
import time
from datetime import datetime
import traceback
import socket
import pywifi
from pywifi import const


def connect_wifi(ssid, password):
    wifi = pywifi.PyWiFi()
    while len(wifi.interfaces())==0:
        print('Waiting for Windows system to initialize WiFi module (10s).')
        time.sleep(10)
    iface = wifi.interfaces()[0]
    iface.disconnect()
    time.sleep(1)
    if iface.status() in [const.IFACE_DISCONNECTED, const.IFACE_INACTIVE]:
        profile = pywifi.Profile()
        profile.ssid = ssid
        profile.auth = const.AUTH_ALG_OPEN
        profile.akm.append(const.AKM_TYPE_WPA2PSK)
        profile.cipher = const.CIPHER_TYPE_CCMP
        profile.key = password
        iface.remove_all_network_profiles()
        tmp_profile = iface.add_network_profile(profile)
        iface.connect(tmp_profile)
        for i in range(10):
            if iface.status() == const.IFACE_CONNECTED:
                return True
            time.sleep(1)
        return False
    else:
        return False


def is_connected():
    try:
        socket.create_connection(("8.8.8.8", 53), timeout=5)
        return True
    except OSError:
        return False


def move_and_click(x, y, times=0.2):
    pyautogui.moveTo(x, y, times)
    time.sleep(0.1)
    pyautogui.click()
    time.sleep(0.1)


def main():
    print('****************************************\nWARNING: Must run in administrator mode!\n****************************************')
    if not open_switch_computer_mode:
        print('Computer mode switching not enabled.')
    if not open_log_in_to_campus_network:
        print('Login to campus network not enabled.')
    compute_mode = initial_compute_mode
    while True:
        try:
            if open_log_in_to_campus_network:
                if not is_connected():
                    print('The network is not connected, try opening the login interface.')
                    webbrowser.open(url)
                    time.sleep(3)
                    if not is_connected():
                        move_and_click(1895, 13)
                        print('Still not connected, try logging in to WiFi.')
                        if connect_wifi("wifi name", "wifi password"):
                            print('Successfully logged in to WIFI!')
                        webbrowser.open(url)
                        time.sleep(3)
                        if not is_connected():
                            print('The network is still not connected, try to automatically enter the account and password to log in.')
                            move_and_click(user_name_x_axis_coordinate, user_name_y_axis_coordinate)
                            pyautogui.typewrite('wifi账号(此wifi需再次登陆认证)', interval=0.25)
                            move_and_click(960, 540)
                            move_and_click(user_name_x_axis_coordinate, user_name_y_axis_coordinate + 40)
                            pyautogui.typewrite('wifi密码(此wifi需再次登陆认证)', interval=0.25)
                            move_and_click(960, 540)
                            pyautogui.click(user_name_x_axis_coordinate, user_name_y_axis_coordinate + 80)
                            move_and_click(user_name_x_axis_coordinate, user_name_y_axis_coordinate + 120)
                            move_and_click(user_name_x_axis_coordinate - 90, user_name_y_axis_coordinate + 115)
                            move_and_click(user_name_x_axis_coordinate + 125, user_name_y_axis_coordinate + 115)
                            move_and_click(user_name_x_axis_coordinate, user_name_y_axis_coordinate + 170)
                    move_and_click(1895, 13)
                    pyautogui.moveTo(960, 540)
                    time.sleep(1)
                    print('The time to log in to the campus network is ' + datetime.now().strftime('%Y-%m-%d %H:%M:%S') + '.')
            if open_switch_computer_mode:
                now = datetime.now()
                if datetime(now.year, now.month, now.day, 0, 0, 0) <= now <= datetime(now.year, now.month, now.day, 8, 0, 0) and compute_mode != 'beast':
                    os.system(legionzone_path)
                    time.sleep(2)
                    move_and_click(beast_x_axis_coordinate, beast_y_axis_coordinate)
                    move_and_click(legion_zone_close_coordinates[0], legion_zone_close_coordinates[1])
                    pyautogui.moveTo(960, 540)
                    time.sleep(1)
                    compute_mode = 'beast'
                    print('Beast mode has been activated. Current time is ' + now.strftime('%Y-%m-%d %H:%M:%S') + '.')
                if datetime(now.year, now.month, now.day, 8, 0, 0) < now <= datetime(now.year, now.month, now.day, 23, 59, 59) and compute_mode == 'beast':
                    os.system(legionzone_path)
                    time.sleep(2)
                    if initial_compute_mode == 'quiet':
                        move_and_click(beast_x_axis_coordinate - 190, beast_y_axis_coordinate)
                        move_and_click(legion_zone_close_coordinates[0], legion_zone_close_coordinates[1])
                        pyautogui.moveTo(960, 540)
                        time.sleep(1)
                        compute_mode = 'quiet'
                        print('Quiet mode has been activated. Current time is ' + now.strftime('%Y-%m-%d %H:%M:%S') + '.')
                    else:
                        move_and_click(beast_x_axis_coordinate - 93, beast_y_axis_coordinate)
                        move_and_click(legion_zone_close_coordinates[0], legion_zone_close_coordinates[1])
                        pyautogui.moveTo(960, 540)
                        time.sleep(1)
                        compute_mode = 'balanced'
                        print('Balanced mode has been activated. Current time is ' + now.strftime('%Y-%m-%d %H:%M:%S') + '.')
        except:
            print('⚠ERROR: Unexpected Error.')
            time.sleep(1)
            traceback.print_exc()
        time.sleep(check_interval_time_in_minutes * 60)


if __name__ == '__main__':
    user_name_x_axis_coordinate = 1561
    user_name_y_axis_coordinate = 374
    beast_x_axis_coordinate = 936
    beast_y_axis_coordinate = 579
    legion_zone_close_coordinates = (1551, 147)
    check_interval_time_in_minutes = 10
    open_log_in_to_campus_network = True
    open_switch_computer_mode = False
    initial_compute_mode = 'balanced'  # quiet or balanced
    url = 'http://192.168.1.1/'
    legionzone_path = r"D:\legion_zone\LegionZone\LZMain.exe"
    main()

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值