目的:
远端(php)控制网络设置(命令行shell)。
而Network Manager会影响wpa_supplicant,所以需要卸载。
使用平台
Ubuntu 10.04 LTS
GNU/Linux i686 2.6.32-24-generic
调用的资源
a) 显示网络状态
/sbin/ifconfig
Cat /etc/resolv.conf
/sbin/route –n
b) 显示无线信号
/sbin/iwlist wlan0 scan
c) 写入地址信息
ifconfig eth0 192.168.1.101 netmask 255.255.255.0
route add default gw 192.168.1.1
echo "nameserver 192.168.1.1 n nameserver192.168.1.1" > /etc/resolv.conf
/etc/init.d/networking restart
d) 写入无线的用户名和密码
把用户名密码写入到/etc/wpa_supplicant.conf
使用前配置:
a) 卸载Network Manager:
apt-get remove NetworkManager
b) 拷贝wpa.sh到/etc/wpa.sh
#!/bin/bash
### BEGIN INIT INFO
# Provides: wpa
# Required-Start: $network $syslog $local_fs
# Required-Stop: $network $syslog $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/stop script for wpa supplicant
# Description: Custom start/stop script for wpa_supplicant.
### END INIT INFO
SELF=`basename $0`
WPA=wpa_supplicant
PROGRAM=/sbin/${WPA}
CONF=/etc/${WPA}.conf
INTERFACE=wlan0
DRIVER=wext
DAEMONMODE="-B"
LOGFILE=/var/log/$WPA.log
function start() {
# TODO: Support multiple interfaces and drivers
OPTIONS="-c $CONF -i $INTERFACE -D $DRIVER $DAEMONMODE"
## You can remove this if you are running 8.10 and up.
# Ubuntu 8.10 and up doesn't need the -w anymore..
# And the logfile option is not valid on 8.04 and lower
local ver=$(lsb_release -sr | sed -e 's/.//g');
[ $ver -lt 810 ] && OPTIONS="$OPTIONS -w" && LOGFILE=""
##
# Log to a file
[ -n "$LOGFILE" ] && OPTIONS="$OPTIONS -f $LOGFILE"
echo " * Starting wpa supplicant"
eval $PROGRAM $OPTIONS
}
function stop() {
echo " * Stopping wpa supplicant"
pkill $PROGRAM
}
function debug() {
stop
DAEMONMODE="-ddd"
start
}
function restart() {
stop
start
}
function status() {
pgrep -lf $PROGRAM
}
function usage() {
echo "Usage: $SELF <start|stop|status|debug>"
return 2
}
case $1 in
start|stop|debug|status) $1 ;;
*) usage ;;
esac
c) 新建配置相关文件/etc/wpa_supplicant.conf
ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=0
eapol_version=2
ap_scan=1
fast_reauth=1
country=NL
network={
ssid="duoleyuan"
psk="xxx"
scan_ssid=1
}
d) 修改/etc/network/interfaces
auto lo
iface lo inet loopback
auto wlan0
iface wlan0 inet dhcp
pre-up /etc/wpa.sh start
post-down /etc/wpa.sh stop
/etc/init.d/networking restart
重启网卡报错
Failed to bring up wlan0
发现重启网络时,wpa还没有关闭,所以起不来。
/etc/init.d/networking stop
sleep 5
/etc/init.d/networking start
问题暂时解决
权限问题:
chmod u+s /sbin/iwlist
chmod u+s /sbin/route
chmod u+s /sbin/ifconfig
太多了,害怕列举不玩。并且可能有安全问题。
针对www-data用户,sudo免输密码
visudo
www-data ALL=(ALL) NOPASSWD: NOPASSWD: ALL