[xhr4412][extension 7] hostapd-2.9 dhcp-4.4.2 xhr4412 AP 模式 ,自动分配/获取IP地址

  • Post author:
  • Post category:其他




一、hostapd



1. build

因为前面已经移植好了 openssl 和 libnl,所以编译 hostapd 是很轻松的一件事。

额外打开配置:

  • CONFIG_ACS=y
tar zxvf hostapd-2.9.tar.gz
cd hostapd-2.9/hostapd/
cp -p defconfig .config
echo "CC=arm-none-linux-gnueabihf-gcc -L/home/xhr/iTop4412/xhr4412/ssh/openssl/openssl-1.1.1h/out/install/lib -L/home/xhr/iTop4412/xhr4412/wifi/libnl/libnl-3.2.25/__install/lib" >> .config
echo "CFLAGS += -I/home/xhr/iTop4412/xhr4412/wifi/libnl/libnl-3.2.25/__install/include/libnl3" >> .config
echo "LIBS += -L/home/xhr/iTop4412/xhr4412/wifi/libnl/libnl-3.2.25/__install/lib" >> .config
echo "CFLAGS += -I/home/xhr/iTop4412/xhr4412/ssh/openssl/openssl-1.1.1h/out/install/include" >> .config
echo "LIBS += -L/home/xhr/iTop4412/xhr4412/ssh/openssl/openssl-1.1.1h/out/install/lib" >> .config
make

这个报错不用理会:

Package libnl-3.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `libnl-3.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libnl-3.0' found

编译好后生成了

hostapd



hostapd_cli

,将他们拷贝到 xhr4412

/usr/bin/

目录。



2. conf

配置文件:

/etc/hostapd.conf

也可以从源码中拷贝。

interface=wlan0
ssid=xhr4412wlan0
channel=0
wpa=2
wpa_passphrase=12345678
max_num_sta=8



3. use



hostapd /etc/hostapd.conf -B

wlan0: ACS-COMPLETED freq=2412 channel=1
Using interface wlan0 with hwaddr 70:f1:1c:27:dd:3a and ssid "xhr4412wlan0"
[ 7902.128687] IPv6: ADDRCONF(NETDEV_CHANGE): wlan0: link becomes ready
wlan0: interface state ACS->ENABLED
wlan0: AP-ENABLED 
wlan0: STA 94:0e:6b:f5:0f:81 IEEE 802.11: authenticated
wlan0: STA 94:0e:6b:f5:0f:81 IEEE 802.11: associated (aid 1)
wlan0: AP-STA-CONNECTED 94:0e:6b:f5:0f:81
wlan0: STA 94:0e:6b:f5:0f:81 RADIUS: starting accounting session 22DB3A5DB12E566F
wlan0: STA 94:0e:6b:f5:0f:81 WPA: pairwise key handshake completed (RSN)


xhr4412 ping 平板:

[root@xhr4412]~# ping 192.168.188.3 -c 8
PING 192.168.188.3 (192.168.188.3): 56 data bytes
64 bytes from 192.168.188.3: seq=0 ttl=64 time=9.943 ms
64 bytes from 192.168.188.3: seq=1 ttl=64 time=2.998 ms
64 bytes from 192.168.188.3: seq=2 ttl=64 time=6.835 ms
64 bytes from 192.168.188.3: seq=3 ttl=64 time=9.052 ms
64 bytes from 192.168.188.3: seq=4 ttl=64 time=4.122 ms
64 bytes from 192.168.188.3: seq=5 ttl=64 time=3.953 ms
64 bytes from 192.168.188.3: seq=6 ttl=64 time=3.417 ms
64 bytes from 192.168.188.3: seq=7 ttl=64 time=3.010 ms

--- 192.168.188.3 ping statistics ---
8 packets transmitted, 8 packets received, 0% packet loss
round-trip min/avg/max = 2.998/5.416/9.943 ms


平板 ping xhr4412:

在这里插入图片描述



二、dhcp

前面虽然可以通过手机平板等连接 xhr4412 AP,但需要手动设置静态 IP 地址,很麻烦,所以需要 dhcp 自动的获取和设置 IP 。



1. build

tar zxvf dhcp-4.4.2.tar.gz
cd dhcp-4.4.2/
export BUILD_CC=arm-none-linux-gnueabihf-gcc
./configure  --host=arm-none-linux-gnueabihf --prefix=$PWD/tmp --with-randomdev=/dev/urandom
vim bind/bind-9.11.14/lib/dns/Makefile.in
make
make install
  • bind/bind-9.11.14/lib/dns/Makefile.in:
186 gen: gen.c
187         ${BUILD_CC} ${BUILD_CFLAGS} -I${top_srcdir}/lib/isc/include \

改为:

186 gen: gen.c
187         gcc ${BUILD_CFLAGS} -I${top_srcdir}/lib/isc/include \

报错:

1.
configure: error:  need --with-randomdev=PATH or --with-randomdev=no
xhr4412 有 /dev/urandom

2.
configure: WARNING: using cross tools not prefixed with host triplet
configure: error: BUILD_CC not set
export BUILD_CC=arm-none-linux-gnueabihf-gcc

3.
/bin/bash: ./gen: cannot execute binary file: Exec format error
Configuring BIND libraries for DHCP.
configure: WARNING: using cross tools not prefixed with host triplet
修改:bind/bind-9.11.14/lib/dns/Makefile.in



2. conf


make install

后在

tmp

文件夹下,就生成了所需文件。将

tmp/sbin

里的应用程序拷贝到 xhr4412

/usr/sbin

下,将

tmp/etc

里的配置文件拷贝到 xhr4412

/etc/

下,并删除

.example

后缀。



2.1 dhcpd


dhcpd

是 DHCP 服务器,它为其他计算机分配 IP 地址。



(1) vim /etc/dhcpd.conf

subnet 192.168.188.0 netmask 255.255.255.0 {
  range 192.168.188.128 192.168.188.254;
  option domain-name-servers xhr4412.dhcpd.server;
  option domain-name "xhr4412.dhcpd.name";
  option routers 192.168.188.1;
  option broadcast-address 192.168.188.255;
  default-lease-time 600;
  max-lease-time 7200;
}



(2) mkdir -p /var/db



(3) touch /var/db/dhcpd.leases



(4) dhcpd



使用 dhcpd 后,通过手机平板直接连接

xhr4412wlan0

AP 即可,可以看到第一个连接的设备 IP 为 192.168.188.128 ,测试两个方向的 ping ,没有问题。



2.2 dhclient


dhclient

是 DHCP 客户端,它可从其他 DHCP 服务器获取 IP 地址,再配置网卡。



(1) connet to AP



wpa_supplicant -Dnl80211 -iwlan0 -c/etc/wpa_supplicant.conf -B

需要干净的网卡,或是将静态 IP 删除:


  • ip addr delete 192.168.188.1 dev wlan0:0

  • ip addr flush dev wlan0



(2) vim dhclient.conf

send host-name = gethostname();
send dhcp-lease-time 3600;
request subnet-mask, broadcast-address, time-offset, routers,
        domain-name, domain-name-servers, host-name;
require subnet-mask, domain-name-servers;
timeout 60;
retry 60;



(3) cp client/scripts/linux


  • cp -p /mnt/xhr4412/wifi/dhcp/dhcp-4.4.2/client/scripts/linux /sbin/dhclient-script
  • #! /bin/sh



(3) dhclient wlan0 & > /dev/null



可以看到 wlan0 网卡已经设置好 IP 地址,可以 ping 外网了。



参考



版权声明:本文为u012849539原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。