用shell脚本实现linux系统上wifi模式(STA和soft AP)的转换
转载请注明出处:http://blog.csdn.net/hellomxj1/
功能:在linux系统上实现wifi STA与AP功能的转换
实现成果:1、加入wifipassword账户add_wifi_account。
2、wifi两种模式启动的脚本wifi_start;
3、帮助信息README_WIFI_START;
详细实现步骤例如以下:
加入wifipassword账户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!"
~
该部分主要是实现将账户和password加入到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)的转换的更多相关文章
- Shell脚本查看linux系统性能瓶颈(转)
Shell脚本查看linux系统性能瓶颈(转自:http://blog.51cto.com/lizhenliang/1687612) [root@test ~]# cat show_sys_info. ...
- 使用Shell脚本对Linux系统和进程资源进行监控
ShellLinux脚本 摘要:Shell语言对于接触Linux的人来说都比较熟悉,它是系统的用户界面,提供了用户与内核进行交互操作的一种接口.本文我们以Bash做为实例总结了使用Shell对系统和进 ...
- 使用 shell 脚本对 Linux 系统和进程资源进行监控
Shell 简介 Shell 语言对于接触 LINUX 的人来说都比较熟悉,它是系统的用户界面,提供了用户与内核进行交互操作的一种接口.它接收用户输入的命令并把它送入内核去执行.实际上 Shell 是 ...
- shell脚本监控Linux系统性能指标
2016-11-04 22:41 原作者不详 分类: Linux(7) 在服务器运维过程中,经常需要对服务器的各种资源进行监控, 例如:CPU的负载监控,磁盘的使用率监控,进程数目监控等等,以在系统出 ...
- Shell 脚本实现 Linux 系统监控
一.实验介绍 1.1 实验内容 本课程实现 shell 脚本监控系统的各项参数,并可以将脚本加入系统环境中,可以直接在终端里执行.还添加了几个参数,一个脚本可以执行不同的操作. 1.2 知识点 本实验 ...
- shell脚本之不同系统上ftp交互使用
场景:当公司将有文件要自动将ubuntu系统的文件要上传到windows上面,或者windows上的文件要下载到ubuntu上面,尤其是像什么日志啊,编译结果啊,测试结果啊等等,做个备份或者做分析处理 ...
- Linux系统防CC攻击自动拉黑IP增强版Shell脚本 《Linux系统防CC攻击自动拉黑IP增强版Shell脚本》来自张戈博客
前天没事写了一个防CC攻击的Shell脚本,没想到这么快就要用上了,原因是因为360网站卫士的缓存黑名单突然无法过滤后台,导致WordPress无法登录!虽然,可以通过修改本地hosts文件来解决这个 ...
- Shell脚本查看linux系统性能瓶颈
脚本目的:分析系统资源性能瓶颈 脚本功能: 1.查看CPU利用率与负载(top.vmstat.sar) 2.查看磁盘.Inode利用率与I/O负载(df.iostat.iotop.sar.dstat) ...
- shell脚本监控Linux系统的登录情况
一.登录日志记录 vi /etc/profile 在最后面添加一行: echo "`who`" > /var/log/login.log 二.监控日志文件 #!/bin/ba ...
随机推荐
- selenium对浏览器属性操作的方法
最大化 方法一 //指明ChromeDriver路径 System.setProperty(Src_url_string.Chrome_Driver, Src_url_string.Driver_ad ...
- html中<frameset>标签,框架结构各窗口的父级菜单子级菜单关系
这个问题搞得我头大,并且在查过百度后各位大佬给出的解释我都不能理解,应该是我太白的原因,希望我写的能好理解. 下面文章窗口1.2.3,在代码里分别为chuangkou.chuangkou1.chuan ...
- vuex的状态管理模式
1.store.js Vuex 通过 store 选项,提供了一种机制将状态从根组件“注入”到每一个子组件中(需调用 Vue.use(Vuex)) state:存放数据. mutations:提交状态 ...
- Python FLask 腾讯云服务器部署
CentOs 7.0云服务器部署Python Flask 使用: Python 2.7 Flask nginx gunicorn easy_install python-dev yum install ...
- FLP不可能原理
1. FLP impossibility背景 FLP Impossibility(FLP不可能性)是分布式领域中一个非常著名的结果,该结果在专业领域被称为“定理”,其地位之高可见一斑.该定理的论文是由 ...
- DIV+CSS左右两列自适应高度的方法
我们在用DIV+CSS布局网页的时候,必然会遇到左右两列自适应高度的问题,就是左边列的背景会随着右边列内容的增加也相应的增加高度,下面就教大家DIV+CSS左右两列自适应高度的方法. 下面给出最终的效 ...
- Ad_hoc_polymorphism 备份
https://en.wikipedia.org/wiki/Polymorphism_(computer_science) https://en.wikipedia.org/wiki/Ad_hoc_p ...
- swift pragma mark
众所周知,大家在OC中对代码进行逻辑组织 用的是#pragma mark - ,生成分隔线 用#pragma mark 函数说明,来生成一个函数的说明X 但在swift中,这个语法就不支持了,毕竟它是 ...
- 团体程序设计天梯赛-练习集-L1-028. 判断素数
L1-028. 判断素数 本题的目标很简单,就是判断一个给定的正整数是否素数. 输入格式: 输入在第一行给出一个正整数N(<=10),随后N行,每行给出一个小于231的需要判断的正整数. 输出格 ...
- day28 re(正则)模块
目录 re模块有什么用? re模块的基础使用 元字符 终极 贪婪模式 非贪婪模式 re模块高级 comple match和search re.split() sub和subn 分组 re模块有什么用? ...