Python подключение к wifi

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

A cross-platform module for manipulating WiFi devices.

License

awkman/pywifi

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

pywifi provides a cross-platform Python module for manipulating wireless interfaces.

On Linux, you will need to run wpa_supplicant to manipulate the wifi devices, and then pywifi can communicate with wpa_supplicant through socket.

On Windows, the Native Wifi component comes with Windows versions greater than Windows XP SP2.

After installing the prerequisites listed above for your platform, you can use pip to install from source:

For the details of pywifi, please refer to Documentation.

import time import pywifi from pywifi import const wifi = pywifi.PyWiFi() iface = wifi.interfaces()[0] iface.disconnect() time.sleep(1) assert iface.status() in\ [const.IFACE_DISCONNECTED, const.IFACE_INACTIVE] profile = pywifi.Profile() profile.ssid = 'testap' profile.auth = const.AUTH_ALG_OPEN profile.akm.append(const.AKM_TYPE_WPA2PSK) profile.cipher = const.CIPHER_TYPE_CCMP profile.key = '12345678' iface.remove_all_network_profiles() tmp_profile = iface.add_network_profile(profile) iface.connect(tmp_profile) time.sleep(30) assert iface.status() == const.IFACE_CONNECTED iface.disconnect() time.sleep(1) assert iface.status() in\ [const.IFACE_DISCONNECTED, const.IFACE_INACTIVE] 

Источник

Connect to a WiFi network in Python

Connecting a computer to the internet has become inevitable now. The connection can be made either with Ethernet technology or Wi-Fi technology. Though every Operating System offers way with its simple easy GUI, using the Python script has a nice ring to it. This article explains how a computer can be connected to the internet with Wi-Fi technology using a Python script in Windows and Linux operating systems.

Читайте также:  Ноутбук принимает маленькую скорость вай фай

The netsh and nmcli

netsh is a command-line tool in Windows that offers various facilities for networking. To add a new Wi-Fi connection, Windows requires the credentials to be stored in an XML file. nmcli is a command-line tool in the Linux distributions that offers facilities for networking. Unlike Windows netsh , nmlci is quite simple to use. These commands are used in the Python script to connect to a network.

A Python script to connect with Wi-Fi network

Typing a series of commands every time for connecting to a network can be annoying. With the knowledge of the commands, a Python script can be used to do it. The script works by executing the commands in a subshell. Here is a Python script that connects to a Wi-Fi network, given its name and password (for new networks).

import os import platform import getpass def createNewConnection(name, SSID, key): config = """  """+name+"""  """+SSID+"""   ESS auto   WPA2PSK AES false  passPhrase false """+key+"""    """ if platform.system() == "Windows": command = "netsh wlan add profile filename=\""+name+".xml\""+" interface=Wi-Fi" with open(name+".xml", 'w') as file: file.write(config) elif platform.system() == "Linux": command = "nmcli dev wifi connect '"+SSID+"' password '"+key+"'" os.system(command) if platform.system() == "Windows": os.remove(name+".xml") def connect(name, SSID): if platform.system() == "Windows": command = "netsh wlan connect name=\""+name+"\" ssid=\""+SSID+"\" interface=Wi-Fi" elif platform.system() == "Linux": command = "nmcli con up "+SSID os.system(command) def displayAvailableNetworks(): if platform.system() == "Windows": command = "netsh wlan show networks interface=Wi-Fi" elif platform.system() == "Linux": command = "nmcli dev wifi list" os.system(command) try: displayAvailableNetworks() option = input("New connection (y/N)? ") if option == "N" or option == "": name = input("Name: ") connect(name, name) print("If you aren't connected to this network, try connecting with correct credentials") elif option == "y": name = input("Name: ") key = getpass.getpass("Password: ") createNewConnection(name, name, key) connect(name, name) print("If you aren't connected to this network, try connecting with correct credentials") except KeyboardInterrupt as e: print("\nExiting. ")

The script uses platform.system() to identify commands for the appropriate platform. Here the commands are executed in a subshell with os.system() method with a command as its argument. getpass() is a method that can make password invisible when typed. The try-except is used to prevent any runtime exceptions.
Running the script in Windows produces the following output.

Читайте также:  Есть ли безлимитные wifi

Output when connecting to a known network

Interface name : Wi-Fi There are 1 networks currently visible. SSID 1 : Lenovo Wi-Fi Network type : Infrastructure Authentication : WPA2-Personal Encryption : CCMP New connection (y/N)? Name: Lenovo Wi-Fi Connection request was completed successfully. If you aren't connected to this network, try connecting with correct credentials

Output when connecting to a new network

Interface name : Wi-Fi There are 1 networks currently visible. SSID 1 : Lenovo Wi-Fi Network type : Infrastructure Authentication : WPA2-Personal Encryption : CCMP New connection (y/N)? y Attempting to add Profile. Name: Lenovo Wi-Fi Password: Profile Lenovo Wi-Fi is added on interface Wi-Fi. Connection request was completed successfully. If you aren't connected to this network, try connecting with correct credentials

Running the script in Linux produces some pretty output.

Output when connecting to a known network

IN-USE BSSID SSID MODE CHAN RATE SIGNAL BARS SECURITY E4:A7:C5:C1:75:E6 Lenovo Wi-Fi Infra 11 65 Mbit/s 100 ▂▄▆█ WPA2 New connection (y/N)? Name: Lenovo Wi-Fi Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4) If you aren't connected to this network, try connecting with correct credentials

Output when connecting to a new network

IN-USE BSSID SSID MODE CHAN RATE SIGNAL BARS SECURITY E4:A7:C5:C1:75:E6 Lenovo Wi-Fi Infra 11 65 Mbit/s 100 ▂▄▆█ WPA2 New connection (y/N)? y Name: Lenovo Wi-Fi Password: Device 'wlp2s0' successfully activated with '82005b12-d6a5-4601-9579-113214923eb9'. Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6) If you aren't connected to this network, try connecting with correct credentials

I hope you have understood and able to connect with the WiFi network by yourself by writing code in Python.

Источник

The wifi Command¶

This library comes with a command line program for managing and saving your WiFi connections.

Tutorial¶

This tutorial assumes you are comfortable on the command line. (If you aren’t, perhaps wifi is not quite the right tool for you.)

First, if you haven’t already, install wifi.

Now, you want to see what networks are available. You can run the scan command to do that.

All of these commands need to run as a superuser.

# wifi scan -61 SomeNet protected -62 SomeOtherNet unprotected -78 zxy-12345 protected -86 TP-LINK_CB1676 protected -86 TP-LINK_PocketAP_D8B616 unprotected -82 TP-LINK_C1DBE8 protected -86 XXYYYYZZZ protected -87 Made Up Name protected

The wifi command is also accessible through the python argument as:

The scan command returns three bits of data: the signal quality, the SSID and if the network is protected or not. If you want to order the networks by quality, you can pipe the output into sort.

# wifi scan | sort -rn -61 SomeNet protected -62 SomeOtherNet unprotected -78 zxy-12345 protected -82 TP-LINK_C1DBE8 protected -86 XXYYYYZZZ protected -86 TP-LINK_PocketAP_D8B616 unprotected -86 TP-LINK_CB1676 protected -87 Made Up Name protected

The greater the number, the better the signal.

Читайте также:  Windows 10 create wifi hotspot

We decide to use the SomeNet network because that’s the closest one (plus we know the password). We can connect to it directly using the connect command.

# wifi connect --ad-hoc SomeNet passkey>

The —ad-hoc or -a option allows us to connect to a network that we haven’t configured before. The wifi asks you for a passkey if the network is protected and then it will connect.

If you want to actually save the configuration instead of just connecting once, you can use the add command.

# wifi add some SomeNet passkey>

some here is a nickname for the network you can use when you want to connect to the network again. Now we can connect to the saved network if you want using the connect command.

If you wish to see all the saved networks, you can use the list command.

Usage¶

usage: wifi scan,list,config,add,connect,init> . 

scan¶

Shows a list of available networks.

list¶

Shows a list of networks already configured.

add, config¶

Prints or adds the configuration to connect to a new network.

usage: wifi config SCHEME [SSID] usage: wifi add SCHEME [SSID] positional arguments: SCHEME A memorable nickname for a wireless network. If SSID is not provided, the network will be guessed using SCHEME. SSID The SSID for the network to which you wish to connect. This is fuzzy matched, so you don't have to be precise. 

connect¶

Connects to the network corresponding to SCHEME.

usage: wifi connect [-a] SCHEME positional arguments: SCHEME The nickname of the network to which you wish to connect. optional arguments: -a, --ad-hoc Connect to a network without storing it in the config file 

autoconnect¶

Searches for saved schemes that are currently available and connects to the first one it finds.

Completion¶

The wifi command also comes packaged with completion for bash. If you want to write completion for your own shell, wifi provides an interface for extracting completion information. Please see the wifi-completion.bash and bin/wifi files for more information.

© Copyright 2012, Rocky Meza and Gavin Wahl. Revision 4d1aacf1 .

Versions latest stable Downloads pdf htmlzip epub On Read the Docs Project Home Builds Free document hosting provided by Read the Docs.

Источник

Оцените статью
Adblock
detector