ubuntu 无线网卡 断网重连 教程

一、网卡驱动安装

参考Kali网卡设置comfast cf-812AC(Realtek 8812BU芯片)_banyejiu的博客-CSDN博客

网卡驱动安装的shell命令(自己是啥网卡,就去找对应的安装程序和安装方法)

sudo apt update

sudo apt install bc -y

git clone https://github.com/cilynx/rtl88x2BU_WiFi_linux_v5.3.1_27678.20180430_COEX20180427-5959.git

cd rtl88x2BU_WiFi_linux_v5.3.1_27678.20180430_COEX20180427-5959

VER=$(sed -n 's/\PACKAGE_VERSION="\(.*\)"/\1/p' dkms.conf)

sudo rsync -rvhP ./ /usr/src/rtl88x2bu-${VER}

sudo dkms add -m rtl88x2bu -v ${VER}

sudo dkms build -m rtl88x2bu -v ${VER}

sudo dkms install -m rtl88x2bu -v ${VER}

sudo modprobe 88x2bu

注:ifconfig 查看无线网卡id——wl开头的那个就是,比如wlan0

二、ubuntu wifi重连代码

持续扫描网络状态,10秒1间隔,断开则重连网络(python代码)

import subprocess
import time
import os
from time import strftime, localtime
import traceback
import logging


LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
logging.basicConfig(filename='wifi.log', level=logging.DEBUG, format=LOG_FORMAT)

#ssid="UESTC-WiFi-GUEST"
ssid="UESTC-WiFi"
 
def reconnect():
    global ssid
    # print("%s reconnecting WiFi" % strftime("%Y-%m-%d %H:%M:%S", localtime()))
    # logging.info("%s reconnecting WiFi" % strftime("%Y-%m-%d %H:%M:%S", localtime()))
    logging.info("reconnecting WiFi: %s" % ssid)
    #os.system("netsh wlan disconnect")
    #os.system("netsh wlan connect ssid=%s name=%s" % (ssid, ssid))
    try:
        os.system("sudo nmcli dev wifi connect %s"%ssid)
    except Exception as e:
        logging.error(traceback.format_exc())


def check_wifi():
    
    subp = subprocess.Popen('ping www.baidu.com', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    while subp.poll() is None:
        text = str(subp.stdout.readline())
        # print(text, 'now time: ',time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
        # logging.info("%s %s %s"%(text.replace('\n',''), 'now time: ',str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))))
        logging.info("%s"%(text.replace('\n','')))
        if match(text):
            reconnect()
            break
        time.sleep(10)
#    subp.close()


def match(text):
    if text:
        if text.find('ping: unknown host') >= 0:
            return True
        else:
            return False
    else:
        return False
 
 
if __name__ == '__main__':
    while True:
        check_wifi()
        # print('now time: ',time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
        # print('************************************************')
        # time.sleep(60)

再让它一直挂在服务器上

nohup sudo python wifi.py &

三、win10 wifi重连代码

附带win10 wifi自动重连代码(python代码)

import subprocess
import time
import os
from time import strftime, localtime

#ssid="UESTC-WiFi-GUEST"
ssid="UESTC-WiFi"

def reconnect():
    global ssid
    print("%s reconnecting WiFi" % strftime("%Y-%m-%d %H:%M:%S", localtime()))
    os.system("netsh wlan disconnect")
    os.system("netsh wlan connect ssid=%s name=%s" % (ssid, ssid))



def check_wifi():
   
    subp = subprocess.Popen("ping baidu.com", stdout=subprocess.PIPE)
    while subp.poll() is None:
        text = str(subp.stdout.readline(), encoding='gb18030')
        print(" %s" % text)
        if match(text):
            reconnect()
            break
       


def match(text):
    if text:
        if text.find('请求找不到主机') >= 0 or text.find('请检查该名称,然后重试') >= 0:
            return True
        else:
            return False
    else:
        return False


if __name__ == '__main__':
    while True:
        check_wifi()
        print('now time: ',time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
        print('************************************************')
        time.sleep(60)


 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值