x86和ARM中配置无线网SSID和PASSWORD

提供一个可行的方法

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" in
    start)
        ifconfig wlp5s0 up
        if grep -q "^ssid=$" /etc/hostapd/hostapd.conf; then
                SSID="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.conf
                echo $SSID
        fi
        hostapd_startup.sh
        ;;
    stop)
        ifconfig wlp5s0 down
        echo "stop wifi!"
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    *)
        echo "$0 start|stop|restart"
        ;;
    esac

exit $?
  • hostapd_startup.sh:是AP启动的脚本,存放在 /etc/init.d/hostapd_startup.sh
#!/bin/sh

cnt=`ps aux | grep wpa_supplicant | grep -v grep  | wc -l`
if [ "${cnt}" != "0" ];then
	killall wpa_supplicant > /dev/null
fi

cnt1=`ps aux | grep hostapd | grep -v grep  | wc -l`
if [ "${cnt1}" != "0" ];then
	killall hostapd > /dev/null
fi

/etc/init.d/dnsmasq stop

echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

sleep 1
ifconfig wlp5s0 up
ifconfig wlp5s0 15.0.23.1

hostapd /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.conf

cp dnsmasq.conf /etc/dnsmasq.conf

chmod +x softap.sh
cp softap.sh /etc/init.d/softap.sh

chmod +x hostapd_startup.sh
cp hostapd_startup.sh /etc/init.d/hostapd_startup.sh

cp 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启停操作

你可能感兴趣的:(Xubuntu,无线网络配置)