環境
ubuntu18.04 + python3.6(其他也系統可以參看)
實戰中經常需要命令行和python來設置wifi密碼賬號,此項目是通過觸摸屏反饋賬號密碼,電腦自動設置wifi進行連接
直接上代碼吧
linux_netplan_yaml.py
"""
network:
version: 2
renderer: networkd
ethernets:
enp2s0:
dhcp4: no
dhcp6: no
addresses: [192.168.18.20/24]
gateway4: 192.168.18.1
nameservers:
addresses: [114.114.114.114, 8.8.8.8]
wifis:
wlp1s0:
dhcp4: yes
access-points:
"ssid": "grobot2"
password: "a1b2c3d4e5"
network:
version: 2
renderer: NetworkManager
ethernets:
enp2s0:
dhcp4: no
dhcp6: no
nameservers:
addresses: [114.114.114.114, 8.8.8.8]
wifis:
wlp1s0:
dhcp4: yes
access-points:
"grobot2":
password: "a1b2c3d4e5"
"""
import yaml
import os
import sys
import time
import psutil
_PATH_ = os.path.dirname(__file__)
if _PATH_ not in sys.path:
sys.path.append(_PATH_)
# 獲取網卡名稱和其ip地址,不包括迴環
def get_netcard():
netcard_info = "wlp2s0"
info = psutil.net_if_addrs()
for k, v in info.items():
if k[:2] == 'wl':
netcard_info = k
# for item in v:
# # 可用網絡情況下的網卡名字和ip地址
# if item[0] == 2 and not item[1] == '127.0.0.1':
# netcard_info.append((k, item[1]))
return netcard_info
def change_linux_wifi_ssid_password_old(uers_wifi):
ssid_, password_ = uers_wifi
grb_plan = _PATH_ + '/01-network-manager-all.yaml'
sys_plan = ' /etc/netplan/01-network-manager-all.yaml'
# 執行拷貝文件
sudo_psd = 'grb'
command = 'cp ' + grb_plan + sys_plan
os.system('echo %s|sudo -S %s' % (sudo_psd, command))
# 執行權限設置
command = 'chmod 777 /etc/netplan/01-network-manager-all.yaml'
os.system('echo %s|sudo -S %s' % (sudo_psd, command))
# linux wifi 文件格式
# netplan = ['network', 'wifis', 'wlp1s0', 'access-points']
netplan = ['network', 'wifis', 'wlp2s0', 'access-points']
# 讀取yaml文件
with open(r"/etc/netplan/01-network-manager-all.yaml", "r") as yaml_file:
data = yaml.full_load(yaml_file.read())
# print(data)
wifi = data[netplan[0]][netplan[1]][netplan[2]][netplan[3]]
old_name = 'grobot2'
wifi[old_name]['password'] = password_
new_id_psd = {ssid_: wifi[old_name]}
print(new_id_psd)
wifi.clear()
wifi.update(new_id_psd)
# wifi2 = data[netplan[0]][netplan[1]][netplan[2]][netplan[3]]
# wifi2[old_name]['password'] = password_
# new_id_psd = {ssid_: wifi2[old_name]}
# print(new_id_psd)
# wifi2.clear()
# wifi2.update(new_id_psd)
# yaml_file.close()
# print(data)
# 寫入yaml文件
with open(r"/etc/netplan/01-network-manager-all.yaml", "w") as yaml_file:
yaml.dump(data, yaml_file)
command = 'netplan generate'
os.system('echo %s|sudo -S %s' % (sudo_psd, command))
time.sleep(2)
# 執行linux命令重啓wifi
command = 'netplan apply'
os.system('echo %s|sudo -S %s' % (sudo_psd, command))
print("Reseting system wifi, please wait 30s....!")
time.sleep(30)
print("wifi reset finish, please check!")
def change_linux_networt_line():
grb_plan = _PATH_ + '/01-network-manager-all.yaml'
sys_plan = ' /etc/netplan/01-network-manager-all.yaml'
# 執行拷貝文件
sudo_psd = 'grb'
command = 'cp ' + grb_plan + sys_plan
os.system('echo %s|sudo -S %s' % (sudo_psd, command))
command = 'netplan generate'
os.system('echo %s|sudo -S %s' % (sudo_psd, command))
time.sleep(2)
# 執行linux命令重啓wifi
command = 'netplan apply'
os.system('echo %s|sudo -S %s' % (sudo_psd, command))
print("Reseting system wlan, please wait....!")
time.sleep(15)
print("wlan reset finish, please check!")
def change_linux_wifi_ssid_password(user_wifi):
ssid_, password_ = user_wifi
netcard_ = get_netcard()
sudo_psd = 'grb'
command = 'nmcli d wifi connect ' + ssid_ + ' password ' + password_ + ' ifname ' + netcard_
for i in range(3):
result = os.popen('echo %s|sudo -S %s' % (sudo_psd, command))
res = result.read()
for line in res.splitlines():
print(line)
if "successfully" in line:
print("wifi successfully connect to", ssid_)
return True
else:
time.sleep(2)
return False
# # 查看wifi
# command = 'nmcli c'
# result = os.popen('echo %s|sudo -S %s' % (sudo_psd, command))
# res = result.read()
# print(res.splitlines()[1])
if __name__ == '__main__':
change_linux_wifi_ssid_password(["Grobot", "a1b2c3d4e5"])
# change_linux_wifi_ssid_password(["Grobot5G", "a1b2c3d4e5"])
# change_linux_wifi_ssid_password(["Grobot5G", "a1b2c3d4e5"])
# change_linux_wifi_ssid_password(["grb360", "12345678"])