Qt wifi helper script

本文介绍了一个用于控制 wpa_supplicant 服务的 Bash 脚本,该脚本可以启动、停止无线连接,并通过 DHCP 获取 IP 地址。文章详细展示了如何使用此脚本来配置无线网络。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Start


yantai:/home/shell.albert/project/build-EAVCapturex86-Desktop_Qt_5_3_GCC_64bit-Debug/bin # ./zwpactl.sh  start

need ssid and psk
yantai:/home/shell.albert/project/build-EAVCapturex86-Desktop_Qt_5_3_GCC_64bit-Debug/bin # ./zwpactl.sh  start zhangshaoyan
need ssid and psk
yantai:/home/shell.albert/project/build-EAVCapturex86-Desktop_Qt_5_3_GCC_64bit-Debug/bin # ./zwpactl.sh  start zhangshaoyan  beijing12345
Successfully initialized wpa_supplicant

yantai:/home/shell.albert/project/build-EAVCapturex86-Desktop_Qt_5_3_GCC_64bit-Debug/bin #


Check

yantai:/home/shell.albert # ps aux | grep "wpa"
root       948  0.0  0.0  30836  3772 ?        Ss   08:37   0:00 /usr/sbin/wpa_supplicant -c /etc/wpa_supplicant/wpa_supplicant.conf -u -f /var/log/wpa_supplicant.log
root     13358  0.0  0.0  10524  1624 pts/3    S+   15:44   0:00 grep --color=auto wpa
yantai:/home/shell.albert # ps aux | grep "wpa"
root       948  0.0  0.0  30836  3772 ?        Ss   08:37   0:00 /usr/sbin/wpa_supplicant -c /etc/wpa_supplicant/wpa_supplicant.conf -u -f /var/log/wpa_supplicant.log
root     13419  0.0  0.0  30836  2168 ?        Ss   15:45   0:00 wpa_supplicant -iwlp5s0 -czwpa.conf -P/var/run/zwpa.pid -B
root     13429  0.0  0.0  10524  1528 pts/3    S+   15:45   0:00 grep --color=auto wpa
yantai:/home/shell.albert # cat /var/run/zwpa.pid
13419
yantai:/home/shell.albert #


Stop

yantai:/home/shell.albert/project/build-EAVCapturex86-Desktop_Qt_5_3_GCC_64bit-Debug/bin # ./zwpactl.sh  stopyantai:/home/shell.albert/project/build-EAVCapturex86-Desktop_Qt_5_3_GCC_64bit-Debug/bin #
yantai:/home/shell.albert # cat /var/run/zwpa.pid
cat: /var/run/zwpa.pid: No such file or directory
yantai:/home/shell.albert # ps aux | grep "wpa"
root       948  0.0  0.0  30836  3772 ?        Ss   08:37   0:00 /usr/sbin/wpa_supplicant -c /etc/wpa_supplicant/wpa_supplicant.conf -u -f /var/log/wpa_supplicant.log
root     13455  0.0  0.0  10524  1568 pts/3    S+   15:45   0:00 grep --color=auto wpa
yantai:/home/shell.albert #

wpa_supplicant is also need dhcient to get IP from DHCP server.


yantai:/home/shell.albert # dhclient -v wlp5s0
Internet Systems Consortium DHCP Client 4.2.6
Copyright 2004-2014 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/wlp5s0/00:26:c7:24:28:f4
Sending on   LPF/wlp5s0/00:26:c7:24:28:f4
Sending on   Socket/fallback
DHCPDISCOVER on wlp5s0 to 255.255.255.255 port 67 interval 2 (xid=0x39b72d2e)
DHCPDISCOVER on wlp5s0 to 255.255.255.255 port 67 interval 4 (xid=0x39b72d2e)
DHCPDISCOVER on wlp5s0 to 255.255.255.255 port 67 interval 11 (xid=0x39b72d2e)
DHCPDISCOVER on wlp5s0 to 255.255.255.255 port 67 interval 20 (xid=0x39b72d2e)
DHCPDISCOVER on wlp5s0 to 255.255.255.255 port 67 interval 7 (xid=0x39b72d2e)
DHCPDISCOVER on wlp5s0 to 255.255.255.255 port 67 interval 12 (xid=0x39b72d2e)
DHCPREQUEST on wlp5s0 to 255.255.255.255 port 67 (xid=0x39b72d2e)
DHCPOFFER from 192.168.0.1
DHCPACK from 192.168.0.1 (xid=0x39b72d2e)
bound to 192.168.0.148 -- renewal in 2921 seconds.

yantai:/home/shell.albert # echo $?
0
yantai:/home/shell.albert #



Come on baby,read the following script carefully! Enjoy it !


#!/bin/bash

#this script is used to control wpa_supplicant daemon server.
#for EAVCapture iMX6 project.
#by zhangshaoyan at May 26,2015.

WPABIN=wpa_supplicant
WIRELESSNAME=wlp5s0 #wlan0
CONFILE=zwpa.conf
PIDFILE=/var/run/zwpa.pid
DHCLIENT=dhclient
#usage:
#zwpactl.sh initial:install driver.
#zwpactl.sh start: start wpa_supplicant.
#zwpactl.sh stop: stop wpa_supplicant.
function usage()
{
    echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
    echo "Usage:$0 <commands>."
    echo "supported commands:"
    echo "initial: install WiFi driver module."
    echo "start: start wpa_supplicant daemon server."
    echo "stop: stop wpa_supplicant."
    echo "by 13522296239"
    echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
}

#generate .conf file.
#here is bug exist,do not keep space or Tab before EOF.
function generateConfFile()
{
cat<<EOF >zwpa.conf
ctrl_interface=/var/run/wpa_supplicant
network={
    ssid="$1"
    psk="$2"
}
EOF
}

#at least need 1 parameter.
if [ $# -lt 1 ];then
    usage
    exit -1
fi

if [ $1 = "initial" ];then
    #insmod wifi driver.
    #this should be done at the OS starts before GUI starts.
    insmod rtl8188.ko
elif [ $1 = "start" ];then
    if [ $# -lt 3 ];then
        echo "ZSY:need ssid and psk"
        exit -1
    fi
    #dump out a config file.
    generateConfFile $2 $3
    #start the daemon server.
    ${WPABIN} -i${WIRELESSNAME} -c${CONFILE} -P${PIDFILE} -B
    if [ $? -eq 0 ];then
        echo "ZSY:start success"
        
        #start dhcp client.
        ${DHCLIENT} -v ${WIRELESSNAME}
        if [ $? -ne 0 ];then
            echo "ZSY:dhclient fails"
            exit -1
        fi    
    else
        echo "ZSY:start failed!"
        exit -1
    fi    
elif [ $1 = "stop" ];then
    if [ -x $PIDFILE ];then
        kill `cat $PIDFILE`
        rm -rf $PIDFILE
    fi
else
    echo "ZSY:Unknown command"
    exit -1
fi

#success here.
echo "ZSY:wpa ctrl ends with success"
exit 0

#the end of file by zhangshaoyan.





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值