歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

Ubuntu下wpa_supplicant使用

目的:

  遠端(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

Copyright © Linux教程網 All Rights Reserved