提供一個可行的方法
1.準備文件
hostapd.conf
:是用戶控件的守護進程用于無線接入點(AP)和授權服務器(authentication servers),存放路徑:/etc/hostapd/hostapd.conf
interface=wlp5s0
driver=nl80211
channel=9
hw_mode=g
auth_algs=1
ieee80211n=1
wpa=1
ssid=Bossdog
wpa_passphrase=12345678
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
dnsmasq.conf
:是一個小巧且方便地用于配置DNS和DHCP的工具,適用于小型網絡,存放路徑:/etc/dnsmasq.conf
interface=wlp5s0
listen-address=15.0.23.1
dhcp-range=15.0.23.10,15.0.23.50,12h
server=/google/8.8.8.8
softap.sh
:操作的腳本,能夠啟動、停止、重啟ap wifi服務,存放在 /etc/init.d/softap.sh
case "$1" instart)ifconfig wlp5s0 upif grep -q "^ssid=$" /etc/hostapd/hostapd.conf; thenSSID="Bossdog_"$(head /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 1 | head -n 8 | tr -d '\n')sed -i "s/^ssid=.*/ssid=$SSID/" /etc/hostapd/hostapd.confecho $SSIDfihostapd_startup.sh;;stop)ifconfig wlp5s0 downecho "stop wifi!";;restart)$0 stop$0 start;;*)echo "$0 start|stop|restart";;esacexit $?
hostapd_startup.sh
:是AP啟動的腳本,存放在 /etc/init.d/hostapd_startup.sh
#!/bin/shcnt=`ps aux | grep wpa_supplicant | grep -v grep | wc -l`
if [ "${cnt}" != "0" ];thenkillall wpa_supplicant > /dev/null
ficnt1=`ps aux | grep hostapd | grep -v grep | wc -l`
if [ "${cnt1}" != "0" ];thenkillall hostapd > /dev/null
fi/etc/init.d/dnsmasq stopecho 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADEsleep 1
ifconfig wlp5s0 up
ifconfig wlp5s0 15.0.23.1hostapd /etc/hostapd/hostapd.conf &
/etc/init.d/dnsmasq start
softap.service
:是AP自啟動服務,存放在 /etc/systemd/system/softap.service
[Unit]
Description=Hostapd Soft AP Service
# Start this unit after the softap.service start
After=softap.service[Service]
ExecStart=/etc/init.d/hostapd_startup.sh
# Restart the service on non-zero exit code when terminated by a signal other than SIGHUP, SIGINT, SIGTERM or SIGPIPE
Restart=on-failure
Type=simple[Install]
# This unit should start when softap.service is starting
WantedBy=softap.service
2.環境配置
- 安裝hostapd、dnsmasq依賴庫
sudo apt-get install hostapd dnsmasq
- 導入wifi配置文件
cp hostapd.conf /etc/hostapd/hostapd.confcp dnsmasq.conf /etc/dnsmasq.confchmod +x softap.sh
cp softap.sh /etc/init.d/softap.shchmod +x hostapd_startup.sh
cp hostapd_startup.sh /etc/init.d/hostapd_startup.shcp softap.service /etc/systemd/system/softap.service
- 啟動wifi腳本
/etc/init.d/softap.sh start
3.配置無線參數和啟停
- 讀取
/etc/hostapd/hostapd.conf
配置文件進行對應SSID、密碼配置 - 使用
/etc/init.d/softap.sh
文件進行wifi啟停操作