用shell脚本实现linux系统上wifi模式(STA和soft AP)的转换

转载请注明出处:http://blog.csdn.net/hellomxj1/

功能:在linux系统上实现wifi STA与AP功能的转换

实现成果:1、添加wifi密码账户add_wifi_account;

2、wifi两种模式启动的脚本wifi_start;

3、帮助信息README_WIFI_START;

具体实现过程如下:

添加wifi密码账户add_wifi_account

  1 #!/bin/sh
  2
  3 echo "Add Wifi Account ..."
  4
  5 if [ ! -e /usr/firmware/wifi/wpa_supplicant.conf]
  6 then
  7     echo "There is not wpa_supplicant.conf!!!"
  8     exit
  9 fi
 10     echo network={ >>/etc/wifi/wpa_supplicant.conf
 11     echo ssid=\"$1\" >>/etc/wifi/wpa_supplicant.conf
 12     echo psk=\"$2\" >>/etc/wifi/wpa_supplicant.conf
 13     echo key_mgmt=$3 >>/etc/wifi/wpa_supplicant.conf
 14     echo } >>/etc/wifi/wpa_supplicant.conf
 15
 16
 17     echo "ssid=\"$1\""
 18     echo "psk=\"$2\""
 19     echo "key_mgmt=$3"
 20
 21
 22 echo "Finshed!"
~                    

该部分主要是实现将账户和密码添加到wpa的配置文件,以便于使用sta模式启动能自动连接网络,若果该配置文件中有多个账户信息,将会从依次连接,如果其中一个成功,将不会往下执行。

wifi两种模式启动的脚本wifi_start

1 #!/bin/sh
  2
  3 echo "Start wifi ..."
  4
  5 if [ ! -e /var/run/wpa_supplicant]
  6 then
  7 mkdir -p /var/run/wpa_supplicant
  8 fi
  9
 10 busybox ifconfig wlan0 up
 11
 12 ps -fe|grep wpa_supplicant |grep -v grep
 13
 14 if [ $? -ne 0 ]
 15 then
 16 wpa_supplicant -Dnl80211 -iwlan0 -c/etc/wifi/wpa_supplicant.conf&
 17 fi
 18
 19 echo "######$1"
 20
 21 if [ "$1" = "ap" ]
 22 then
 23
 24     echo "Start wifi AP..."
 25
 26     if [ ! $# == 2 ]
 27     then
 28         echo "Please input : start-wifi ap 192.168.1.xx(1-19)"
 29         exit
 30     fi
 31
 32     if [ ! -e /var/lib/misc ]
 33     then
 34     mkdir -p /var/lib/misc
 35     fi
 36
 37     if [ ! -e /var/lib/misc/udhcpd.leases ]
 38     then
 39     touch /var/lib/misc/udhcpd.leases
 40     fi
 41
 42     ifconfig wlan0 down
 43
 44     result=`cat /sys/module/bcmdhd/parameters/firmware_path`
 45
 46     if [ "$result" != "/usr/firmware/wifi/fw_43341_apsta.bin" ]
 47     then
 48     echo "/usr/firmware/wifi/fw_43341_apsta.bin">/sys/module/bcmdhd/parameters/firmware_path
 49     fi
 50
 51     ifconfig wlan0 $2 up
 52
 53     echo "Start hostapd ..."
 54
 55     ps -fe|grep hostapd |grep -v grep
 56
 57     if [ $? -eq 0 ]
 58     then
 59         ps -ef | grep hostapd | grep -v grep | awk '{print $1}' | sed -e "s/^/kill -9 /g" | sh -
 60     fi
 61
 62     hostapd /etc/wifi/hostapd.conf&
 63
 64     echo "Start udhcpd ..."
 65
 66     ps -fe|grep udhcpd |grep -v grep
 67
 68     if [ $? -eq 0 ]
 69     then
 70         ps -ef | grep udhcpd | grep -v grep | awk '{print $1}' | sed -e "s/^/kill -9 /g" | sh -
 71     fi
 72
 73     udhcpd -fS /etc/udhcpd.conf&
 74
 75     echo "Wifi AP finshed!"
 76
 77 elif [ "$1" = "sta" ]
 78 then
 79     ifconfig wlan0 down
 80
 81     result=`cat /sys/module/bcmdhd/parameters/firmware_path`
 82
 83     if [ "$result" != "/usr/firmware/wifi/fw_43341.bin" ]
 84     then
 85     echo "/usr/firmware/wifi/fw_43341.bin">/sys/module/bcmdhd/parameters/firmware_path
 86     fi
 87
 88     ifconfig wlan0 up
 89
 90     udhcpc -i wlan0
 91
 92 else
 93
 94     echo "Error!! Please input again!"
 95
 96 fi

这里主要分为两个模块,一个是sta模式的启动直接输入./wifi_start sta;即可启动,另一个为soft AP模式直接输入:./wifi_start ap 192.168.2.10;即可进入soft AP模式,考虑到两种模式的不断转换的问题,我在执行的前期会对进程进行检查,将以前开启的进程进行关闭,一边本次开启能够正常运行;

帮助信息README_WIFI_START

  1 <1>Introduction
  2
  3     This document describes how to start wifi,and introduces the function and use methods of add-wifi-account and wifi-start;And add-        wifi-account and wifi-start in the "/etc/init.d" directory.
  4
  5 <2>How to operate
  6
  7 First,Add the available WiFi account
  8
  9     #/etc/init.d/add-wifi-account "JZ_Guest" "#wwwingenic*" "WPA-PSK"
 10
 11     Notes: "JZ_Guest" is the ssid of the network that you want to connect;
 12            "#wwwingenic* is the password of the network;
 13            " WPA-PSK"  is the encryption method of the network;
 14
 15 Second,Start wifi
 16
 17     /*Start wifi with STA model*/
 18
 19     #/etc/init.d/wifi-start sta
 20
 21     /*Start wifi with AP model*/
 22
 23     #/etc/init.d/wifi-start ap 192.168.1.10
 24
 25     Notes: 192.168.1.10 is ip of newton,you can use ip address from 192.168.1.1 <----> 192.168.1.19

这里主要是我的脚本的使用说明,方便使用;

时间比较紧,功能的实现可能会有问题,希望能给大家多多给出建议。

用shell脚本实现linux系统上wifi模式(STA和soft AP)的转换,布布扣,bubuko.com

时间: 2024-08-24 15:52:37

用shell脚本实现linux系统上wifi模式(STA和soft AP)的转换的相关文章

用shell脚本监控linux系统 自动发送邮件

此脚本可以做一个定时的检测,超出设定的值,即往邮箱发送警告 脚本用到bc,sendmail,163邮箱, yum install bc #!/bin/bash #System Monitoring Script while [ 1 ] do #本机需开启postfix或sendmail服务. #报警邮件地址设置 [email protected] [email protected] #设置脚本运行间隔时间.单位(秒). RUNTIME=900 #内存使用率监控设置,单位 (%) MEMTHRE=

Shell脚本查看linux系统性能瓶颈(转)

Shell脚本查看linux系统性能瓶颈(转自:http://blog.51cto.com/lizhenliang/1687612) [[email protected] ~]# cat show_sys_info.sh #!/bin/bash # os_check() { if [ -e /etc/redhat-release ]; then REDHAT=`cat /etc/redhat-release |cut -d' ' -f1` else DEBIAN=`cat /etc/issue

使用Shell脚本对Linux系统和进程资源进行监控

ShellLinux脚本 摘要:Shell语言对于接触Linux的人来说都比较熟悉,它是系统的用户界面,提供了用户与内核进行交互操作的一种接口.本文我们以Bash做为实例总结了使用Shell对系统和进程资源进行监控的一些内容,希望对您能有帮助. Shell语言对于接触Linux的人来说都比较熟悉,它是系统的用户界面,提供了用户与内核进行交互操作的一种接口.它接收用户输入的命 令并把它送入内核去执行.实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核.它没有一般编程语言的“

使用 shell 脚本对 Linux 系统和进程资源进行监控

Shell 简介 Shell 语言对于接触 LINUX 的人来说都比较熟悉,它是系统的用户界面,提供了用户与内核进行交互操作的一种接口.它接收用户输入的命令并把它送入内核去执行.实际上 Shell 是一个命令解释器,它解释由用户输入的命令并且把它们送到内核.它没有一般编程语言的“编译 - 链接 - 运行”过程.不仅如此,Shell 有自己的编程语言用于对命令的编辑,它允许用户编写由 shell 命令组成的程序.Shell 编程语言具有普通编程语言的很多特点,比如它也有循环结构和分支控制结构等,用

shell脚本之不同系统上ftp交互使用

场景:当公司将有文件要自动将ubuntu系统的文件要上传到windows上面,或者windows上的文件要下载到ubuntu上面,尤其是像什么日志啊,编译结果啊,测试结果啊等等,做个备份或者做分析处理等. 下面是shell脚本模板: !/bin/bash #上传文件 ftp -v  -niv  <<EOF                                  #EOF附件没有空格,不然会报错 open  192.168.1.1                            

Shell脚本查看linux系统性能瓶颈(转)

linux服务器敲命令反应慢,网站访问慢,到底什么情况?根据本人的经验,主要原因可能是系统资源到达瓶颈,已经无法处理更多请求.在有监控系统情况下,可以直接通过WEB页面可视化看出是CPU瓶颈?硬盘瓶颈?还是网络瓶颈?如果公司服务器较少或者云服务器,就有可能没有一套监控系统,这时就要登陆到服务器,一条一条的敲命令,查找分析性能瓶颈.命令这么多,咋记得住啊!就算记得住,输入也费劲,于是就有了这个脚本,为了以后自己使用,另外也想分享给博友,学shell朋友能从中得到一丢丢启发.写的比较仓促,内容有点粗

Shell脚本查看linux系统性能瓶颈

linux服务器敲命令反应慢,网站访问慢,到底什么情况?根据本人的经验,主要原因可能是系统资源到达瓶颈,已经无法处理更多请求.在有监控系统情况下,可以直接通过WEB页面可视化看出是CPU瓶颈?硬盘瓶颈?还是网络瓶颈?如果公司服务器较少或者云服务器,就有可能没有一套监控系统,这时就要登陆到服务器,一条一条的敲命令,查找分析性能瓶颈.命令这么多,咋记得住啊!就算记得住,输入也费劲,于是就有了这个脚本,为了以后自己使用,另外也想分享给博友,学shell朋友能从中得到一丢丢启发.写的比较仓促,内容有点粗

shell脚本修改Linux系统中所有IP样例

#!/bin/sh CURR_DIR=$(cd $(dirname $0);pwd) TD_BASE=`su - tduser -c "echo "${TD_BASE}""` function change_app_ip() { if [ $# -ne 3 ];then echo "The param is not correct." exit 1 fi if [ "$1" != "app_ip" ];th

shell脚本监控Linux系统的登录情况

一.登录日志记录 vi /etc/profile 在最后面添加一行: echo "`who`" > /var/log/login.log 二.监控日志文件 #!/bin/bash while true do sleep 2 a=`stat -c %Y /var/log/login.log` b=`date +%s` if [ $(($b-$a)) -le 10 ]; then echo "somebody login in!!!do something!!!"