使用 Python 将我的 Windows 计算机关联到 wifi AP [英] Associating my Windows computer to a wifi AP with Python

查看:29
本文介绍了使用 Python 将我的 Windows 计算机关联到 wifi AP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 python 脚本,它可以使我的计算机关联到无线接入点,将名称作为字符串.例如,我可能指定我要连接到 linksys,而我的脚本会导致计算机这样做.

I'm trying to write a python script that can make my computer associate to a wireless access point, given the name as a string. For example, I might specify I want to connect to linksys, and my script would cause the computer to do that.

我查看了这个 问题,但无法通过查看所提供的链接了解该怎么做.

I looked at this question, but wasn't able to understand what to do from looking at the links provided.

有人能指出我正确的方向吗?

Can somebody point me in the right direction?

推荐答案

我决定采纳 Paulo 的建议并尝试使用 Powershell/命令行.我找到了一篇关于通过命令行连接到网络的文章.

I decided to take Paulo's suggestion and try using Powershell/the command line. I found an article about connecting to a network via the command line.

从命令行,您可以:

netsh wlan connect <profile-name> [name=<ssid-name>]

...其中 name= 部分是可选的,并且仅当配置文件包含多个 ssid 时才必须如此.

...where the name=<ssid-name> part is optional and is necessarily only if the profile contains multiple ssids.

但是,该配置文件似乎必须已存在于机器上才能使命令行内容正常工作.我确实找到了一个 论坛帖子 以编程方式创建个人资料,但我不想仔细浏览它.

However, it looks like the profile must already exist on the machine in order for the command line stuff to work. I did find a forum post on programatically creating a profile, but I didn't feel like canvassing through it.

如果配置文件名称已经存在,那么您可以在 Python 中执行类似以下操作:

If the profile name already exists, then from Python you can do something similar to the following:

import subprocess

def connect_to_network(name):
    process = subprocess.Popen(
        'netsh wlan connect {0}'.format(name),
        shell=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)
    stdout, stderr = process.communicate()

    # Return `True` if we were able to successfully connect
    return 'Connection request was completed successfully' in stdout        

这是一个不完美的解决方案,我不确定它是否适用于所有情况,但它确实适用于我的特定情况.我想我会发布我的想法,以防其他人想尝试修改它以使其更好.

It's an imperfect solution, and I'm not entirely sure if it'll work in every case, but it did work for my particular case. I thought I'd post what I came up with in case somebody else wants to try modifying it to make it better.

这篇关于使用 Python 将我的 Windows 计算机关联到 wifi AP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆