仅供参考
#!/usr/bin/python
#-- coding: utf-8 --
#author: Zbertj
#--------------------------------------------------
#首先需要使用yum -y install hostapd 安装hostapd服务
#然后要关闭NetworkManager服务
#--------------------------------------------------
import sys
import os
argv_ssid = "" #wifi 名称
argv_pwd = "" #wifi 密码
argv_way = "" #wifi 加密方式
argv_fre = "" #wifi 频段
interface = "wlp0s20f0u1"
conf_str = '''#wifi config
interface={}
driver=nl80211
#bridge=br0
hw_mode=g
channel=10
auth_algs=3
ignore_broadcast_ssid=0
wpa=2
ssid={}
wpa_passphrase={}
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP CCMP
rsn_pairwise=TKIP CCMP
'''
def prepareFun():
if len(sys.argv) != 5:
print("argv err")
sys.exit(-1)
global argv_ssid, argv_pwd, argv_way, argv_fre
argv_ssid = sys.argv[1]
argv_pwd = sys.argv[2]
argv_way = sys.argv[3]
argv_fre = sys.argv[4]
# print(argv_ssid,argv_pwd,argv_way,argv_fre)
print("run systemctl stop NetworkManager")
os.system("systemctl stop NetworkManager")
print("run systemctl stop hostapd")
os.system("systemctl stop hostapd")
def setWifiFun():
print("run vim /etc/hostapd/hostapd.conf")
with open("/etc/hostapd/hostapd.conf", "w") as f:
str = conf_str.format(interface,argv_ssid,argv_pwd)
# print(str)
f.write(str)
def runFun():
print("run ifconfig wlp0s20f0u1 10.10.10.10/24 up")
os.system("ifconfig wlp0s20f0u1 10.10.10.10/24 up")
print("run systemctl start hostapd")
os.system("systemctl start hostapd")
if __name__ == '__main__':
prepareFun()
setWifiFun()
runFun()