wifi工具的使用

一、工具移植

一般Linux中进行wifi扫描、连接需要以下工具wpa_supplicant、Wireless Tools、hostapd。具体的移植参考《工具移植.md》

Wireless Tools:进行wifi网卡的基本配置,网络扫描

wpa_supplicant:进行网络连接

hostapd:创建wifi热点

二、工具使用

1
2
# 在执行命令前需要先启动网卡
ifconfig wlan0 up
  1. 扫描附近的网络

    1
    iwlist wlan0 scanning
  2. 连接指定的网络

    1
    2
    3
    4
    5
    6
    7
    8
    # 创建配置文件
    vi /etc/wpa_supplicant/wpa_supplicant.conf
    network={
    ssid="YourWiFiNetwork"
    psk="YourWiFiPassword"
    }
    # 连接wifi,在后台保持连接
    sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf
  3. 创建wifi热点

    • 创建配置文件/etc/hostapd.conf

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      interface=wlan1
      ctrl_interface=/var/run/hostapd
      ssid=Ebaina-test
      channel=6
      driver=nl80211
      wpa=2
      wpa_passphrase=12345678
      wpa_key_mgmt=WPA-PSK
      beacon_int=100
      hw_mode=g
      ieee80211n=1
      wme_enabled=1
      ht_capab=[SHORT-GI-20][SHORT-GI-40][HT40+]
      wpa_pairwise=CCMP
      #rsn_pairwise=CCMP
      max_num_sta=8
      wpa_group_rekey=86400
      #ignore_broadcast_ssid=0
    • 启动热点,后台执行

      1
      hostapd ./hostapd.config -ddd &
  4. 获取动态IP

    • 创建默认脚本/usr/share/udhcpc/default.script/default.script

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      #!/bin/sh
      # udhcpc script edited by Tim Riker <Tim@Rikers.org>

      RESOLV_CONF="/etc/resolv.conf"

      [ -n "$1" ] || { echo "Error: should be called from udhcpc"; exit 1; }

      NETMASK=""
      if command -v ip >/dev/null; then
      [ -n "$subnet" ] && NETMASK="/$subnet"
      else
      [ -n "$subnet" ] && NETMASK="netmask $subnet"
      fi
      BROADCAST="broadcast +"
      [ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"

      case "$1" in
      deconfig)
      echo "Setting IP address 0.0.0.0 on $interface"
      if command -v ip >/dev/null; then
      ip addr flush dev $interface
      else
      ifconfig $interface 0.0.0.0
      fi
      ;;

      renew|bound)
      echo "Setting IP address $ip on $interface"
      if command -v ip >/dev/null; then
      ip addr add $ip$NETMASK $BROADCAST dev $interface
      else
      ifconfig $interface $ip $NETMASK $BROADCAST
      fi

      if [ -n "$router" ] ; then
      echo "Deleting routers"
      while route del default gw 0.0.0.0 dev $interface ; do
      :
      done

      metric=0
      for i in $router ; do
      echo "Adding router $i"
      if [ "$subnet" = "255.255.255.255" ]; then
      # special case for /32 subnets:
      # /32 instructs kernel to always use routing for all outgoing packets
      # (they can never be sent to local subnet - there is no local subnet for /32).
      # Used in datacenters, avoids the need for private ip-addresses between two hops.
      ip route add $i dev $interface
      fi
      route add default gw $i dev $interface metric $((metric++))
      done
      fi

      echo "Recreating $RESOLV_CONF"
      # If the file is a symlink somewhere (like /etc/resolv.conf
      # pointing to /run/resolv.conf), make sure things work.
      if test -L "$RESOLV_CONF"; then
      # If it's a dangling symlink, try to create the target.
      test -e "$RESOLV_CONF" || touch "$RESOLV_CONF"
      fi
      realconf=$(readlink -f "$RESOLV_CONF" 2>/dev/null || echo "$RESOLV_CONF")
      tmpfile="$realconf-$$"
      > "$tmpfile"
      [ -n "$domain" ] && echo "search $domain" >> "$tmpfile"
      for i in $dns ; do
      echo " Adding DNS server $i"
      echo "nameserver $i" >> "$tmpfile"
      done
      mv "$tmpfile" "$realconf"
      ;;
      esac

      exit 0
    • 获取动态IP

      1
      udhcpc -i wlan0
  5. 允许其它设备从热点获取动态IP(开启DHCP服务)

    • 创建配置文件/etc/udhcpd.conf

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      start           192.168.43.50   
      end 192.168.43.100
      interface wlan1

      #Examles
      opt dns 192.168.43.2 192.168.43.10
      option subnet 255.255.255.0
      opt router 192.168.43.1
      opt wins 192.168.43.10
      option dns 129.219.13.81
      option domain local
      option lease 864000
    • 启动DHCP服务

      1
      udhcpd -S /etc/udhcpd.conf
0%