linux下移植wifi之安装DHCP及其操作(七)

1、 下载、解压
    输入dh看有哪些应用程序,输入dhclient -v查看一下相关信息,源码获取的网址:https://www.isc.org/software/dhcp/
    dhcp : 使得WIFI网卡动态获取IP
    下载地址:
    tar -xzf dhcp-4.4.1.tar.gz
    cd dhcp-4.4.1
    
2、 编译、安装
    ./configure --host=arm-linux ac_cv_file__dev_random=yes --with-randomdev=no

    cd bind
    修改Makefile:
    ./configure BUILD_CC=gcc ac_cv_file__dev_random=yes --host=arm-linux --disable-kqueue

    tar xzf bind.tar.gz
    cd bind-9.8.4-P2
    修改 lib/export/dns/Makefile.in 
    gen: ${srcdir}/gen.c
            ${CC} ${ALL_CFLAGS} ${LDFLAGS} -o $@ ${srcdir}/gen.c ${LIBS}
    改为
    gen: ${srcdir}/gen.c
            ${BUILD_CC} ${ALL_CFLAGS} ${LDFLAGS} -o $@ ${srcdir}/gen.c ${LIBS}

    cd ../..
    make DESTDIR=$PWD/tmp install

    把文件复制到NFS
    cd tmp/usr/local
    
    cp bin/* /opt/rootfs/bin/
    cp sbin/* /opt/rootfs/sbin/
    cp etc/dhclient.conf.example /opt/rootfs/etc/dhclient.conf
    cp etc/dhcpd.conf.example /opt/rootfs/etc/dhcpd.conf


    cp client/scripts/linux /opt/rootfs/etc/dhclient-script
    chmod +x /opt/rootfs/etc/dhclient-script
    并修改 
    #!/bin/bash
    改为 /*linux 下是sh*/
    #!/bin/sh               


3、 使用命令:
    mkdir -p /var/db
    wpa_supplicant -B -c/etc/wpa_wpa2.conf  -iwlan0
    dhclient wlan0

4、将命令写成脚本去执行
4.1、 wpa_action.sh:
    vim /sbin/wpa_action.sh 
    #!/bin/sh
    IFNAME=$1
    CMD=$2
    if [ "$CMD" = "CONNECTED" ]; then
       echo connect $IFNAME, dhclient for it > /dev/console
       dhclient $IFNAME
    fi
    if [ "$CMD" = "DISCONNECTED" ]; then
       echo disconnect $IFNAME, kill dhclient for it > /dev/console
       killall dhclient
    fi
    
4.2、auto_wifi.sh 
    vim /sbin/auto_wifi.sh
    #!/bin/sh
    if [ $ACTION = "add" ];
    then                                              
       wpa_supplicant -B -c/etc/wpa_wpa2.conf  -iwlan0
       wpa_cli -a/sbin/wpa_action.sh -B
    else                     
       killall wpa_supplicant
       killall wpa_cli 
       killall dhclient
    fi

    chmod +x /sbin/auto_wifi.sh 
    
    
4.3、    
    一接入WIFI网卡,就自动执行wpa_supplicant、wpa_cli、dhclient。

    vim /etc/mdev.conf 
    wlan0 0:0 777 * /sbin/auto_wifi.sh
    

注意点: wlan0没有ip的时候,dhclient wlan0 -d 可以查看调试信息。-d, 调试参数

扩展:
wpa_cli command line options

wpa_cli [-p] [-i] [-hvB] [-a] \
        [-P] [-g]  [command..]
  -h = help (show this usage text)
  -v = shown version information
  -a = run in daemon mode executing the action file based on events from
       wpa_supplicant
  -B = run a daemon in the background
  default path: /var/run/wpa_supplicant
  default interface: first interface found in socket path


Using wpa_cli to run external program on connect/disconnect
-----------------------------------------------------------

wpa_cli can used to run external programs whenever wpa_supplicant
connects or disconnects from a network. This can be used, e.g., to
update network configuration and/or trigget DHCP client to update IP
addresses, etc.

One wpa_cli process in "action" mode needs to be started for each
interface. For example, the following command starts wpa_cli for the
default interface (-i can be used to select the interface in case of
more than one interface being used at the same time):

wpa_cli -a/sbin/wpa_action.sh -B

The action file (-a option, /sbin/wpa_action.sh in this example) will
be executed whenever wpa_supplicant completes authentication (connect
event) or detects disconnection). The action script will be called
with two command line arguments: interface name and event (CONNECTED
or DISCONNECTED). If the action script needs to get more information
about the current network, it can use 'wpa_cli status' to query
wpa_supplicant for more information.

Following example can be used as a simple template for an action
script:

#!/bin/sh

IFNAME=$1
CMD=$2

if [ "$CMD" = "CONNECTED" ]; then
    SSID=`wpa_cli -i$IFNAME status | grep ^ssid= | cut -f2- -d=`
    # configure network, signal DHCP client, etc.
fi

if [ "$CMD" = "DISCONNECTED" ]; then
    # remove network configuration, if needed
    SSID=
fi

你可能感兴趣的:(移植wifi)