2020年6月18日 星期四

STA上面,使用 CLI 來連接 Wi-Fi AP

盒子上沒有GUI,要如何連接上Wi-Fi? 基本上,也不難,透過 wpa_cli 即可:

第一步

基本上要有兩個Deamon程式:wpa_supplicant 與 dhclient wifi0

# ps | grep -e wpa_supplicant <Enter>
 1782 root      4248 S    /usr/sbin/wpa_supplicant -B -q -iwifi0 -bbr0 -Dmadwif

# ps | grep -e dhclient <Enter>
  449 root      1560 S    dhclient -4 br0 -cf /tmp/dhclient.conf -lf /tmp/dhcli

如果有,就可以使用 wpa_cli 。

# wpa_cli <Enter>
help
> commands:
  status [verbose] = get current WPA/EAPOL/EAP status
  ifname = get current interface name
  ping = pings wpa_supplicant
  relog = re-open log-file (allow rolling logs)
  note <text> = add a note to wpa_supplicant debug log
  mib = get MIB variables (dot1x, dot11)
  help [command] = show usage help
  interface [ifname] = show interfaces/select interface
  level <debug level> = change debug level
  license = show full wpa_cli license
  quit = exit wpa_cli
  set = set variables (shows list of variables when run without arguments)
  dump = dump config variables
  get <name> = get information
  driver_flags = list driver flags
  logon = IEEE 802.1X EAPOL state machine logon
  logoff = IEEE 802.1X EAPOL state machine logoff
  pmksa = show PMKSA cache
  pmksa_flush = flush PMKSA cache entries
  reassociate = force reassociation
  reattach = force reassociation back to the same BSS
  preauthenticate <BSSID> = force preauthentication
  identity <network id> <identity> = configure identity for an SSID
  password <network id> <password> = configure password for an SSID
  new_password <network id> <password> = change password for an SSID
  pin <network id> <pin> = configure pin for an SSID
  otp <network id> <password> = configure one-time-password for an SSID
  passphrase <network id> <passphrase> = configure private key passphrase for an SSID
  sim <network id> <pin> = report SIM operation result
  bssid <network id> <BSSID> = set preferred BSSID for an SSID
  blacklist <BSSID> = add a BSSID to the blacklist
  blacklist clear = clear the blacklist
  blacklist del <BSSID> = del a BSSID from the blacklist
  blacklist = display the blacklist
  log_level <level> [<timestamp>] = update the log level/timestamp
  log_level = display the current log level and log options
  list_networks = list configured networks
  select_network <network id> = select a network (disable others)
  enable_network <network id> = enable a network
  disable_network <network id> = disable a network
  add_network = add a network
  remove_network <network id> = remove a network
  set_network <network id> <variable> <value> = set network variables (shows list of variables when run without arguments)
  get_network <network id> <variable> = get network variables
  dup_network <src network id> <dst network id> <variable> = duplicate network variables
  list_creds = list configured credentials
  add_cred = add a credential
  remove_cred <cred id> = remove a credential
  set_cred <cred id> <variable> <value> = set credential variables
  get_cred <cred id> <variable> = get credential variables
  save_config = save the current configuration
  disconnect = disconnect and wait for reassociate/reconnect command before connecting
  reconnect [noscan] | [scan delay time] = an optional argument is used to
  indicated if scan is required or the scan delay time, like reassociate,
  but only takes effect if already disconnected
  scan = request new BSS scan
  scan_results = get latest scan results
  abort_scan = request ongoing scan to be aborted
  bss <<idx> | <bssid>> = get detailed scan result info
  get_capability <eap/pairwise/group/key_mgmt/proto/auth_alg/channels/freq/modes> = get capabilities
  reconfigure = force wpa_supplicant to re-read its configuration file
  terminate = terminate wpa_supplicant
  interface_add <ifname> <confname> <driver> <ctrl_interface> <driver_param>
    <bridge_name> <create> <type> = adds new interface, all parameters but
    <ifname> are optional. Supported types are station ('sta') and AP ('ap')
  interface_remove <ifname> = removes the interface
  interface_list = list available interfaces
  ap_scan <value> = set ap_scan parameter
  scan_interval <value> = set scan_interval parameter (in seconds)
  bss_expire_age <value> = set BSS expiration age parameter
  bss_expire_count <value> = set BSS expiration scan count parameter
  bss_flush <value> = set BSS flush age (0 by default)
  ft_ds <addr> = request over-the-DS FT with <addr>
  wps_pbc [BSSID] = start Wi-Fi Protected Setup: Push Button Configuration
  wps_pin <BSSID> [PIN] = start WPS PIN method (returns PIN, if not hardcoded)
  wps_check_pin <PIN> = verify PIN checksum
  wps_cancel Cancels the pending WPS operation
  wps_reg <BSSID> <AP PIN> = start WPS Registrar to configure an AP
  wps_ap_pin [params..] = enable/disable AP PIN
  wps_er_start [IP address] = start Wi-Fi Protected Setup External Registrar
  wps_er_stop = stop Wi-Fi Protected Setup External Registrar
  wps_er_pin <UUID> <PIN> = add an Enrollee PIN to External Registrar
  wps_er_pbc <UUID> = accept an Enrollee PBC using External Registrar
  wps_er_learn <UUID> <PIN> = learn AP configuration
  wps_er_set_config <UUID> <network id> = set AP configuration for enrolling
  wps_er_config <UUID> <PIN> <SSID> <auth> <encr> <key> = configure AP
  ibss_rsn <addr> = request RSN authentication with <addr> in IBSS
  suspend = notification of suspend/hibernate
  resume = notification of resume/thaw
  roam <addr> = roam to the specified BSS
  vendor_elem_add <frame id> <hexdump of elem(s)> = add vendor specific IEs to frame(s)
    0: Probe Req (P2P), 1: Probe Resp (P2P) , 2: Probe Resp (GO), 3: Beacon (GO), 4: PD Req, 5: PD Resp, 6: GO Neg Req, 7: GO Neg Resp, 8: GO Neg Conf, 9: Inv Req, 10: Inv Resp, 11: Assoc Req (P2P), 12: Assoc Resp (P2P)
  vendor_elem_get <frame id> = get vendor specific IE(s) to frame(s)
    0: Probe Req (P2P), 1: Probe Resp (P2P) , 2: Probe Resp (GO), 3: Beacon (GO), 4: PD Req, 5: PD Resp, 6: GO Neg Req, 7: GO Neg Resp, 8: GO Neg Conf, 9: Inv Req, 10: Inv Resp, 11: Assoc Req (P2P), 12: Assoc Resp (P2P)
  vendor_elem_remove <frame id> <hexdump of elem(s)> = remove vendor specific IE(s) in frame(s)
    0: Probe Req (P2P), 1: Probe Resp (P2P) , 2: Probe Resp (GO), 3: Beacon (GO), 4: PD Req, 5: PD Resp, 6: GO Neg Req, 7: GO Neg Resp, 8: GO Neg Conf, 9: Inv Req, 10: Inv Resp, 11: Assoc Req (P2P), 12: Assoc Resp (P2P)
  sta_autoconnect <0/1> = disable/enable automatic reconnection
  tdls_discover <addr> = request TDLS discovery with <addr>
  tdls_setup <addr> = request TDLS setup with <addr>
  tdls_teardown <addr> = tear down TDLS with <addr>
  tdls_link_status <addr> = TDLS link status with <addr>
  wmm_ac_addts <uplink/downlink/bidi> <tsid=0..7> <up=0..7> [nominal_msdu_size=#] [mean_data_rate=#] [min_phy_rate=#] [sba=#] [fixed_nominal_msdu] = add WMM-AC traffic stream
  wmm_ac_delts <tsid> = delete WMM-AC traffic stream
  wmm_ac_status = show status for Wireless Multi-Media Admission-Control
  tdls_chan_switch <addr> <oper class> <freq> [sec_channel_offset=] [center_freq1=] [center_freq2=] [bandwidth=] [ht|vht] = enable channel switching with TDLS peer
  tdls_cancel_chan_switch <addr> = disable channel switching with TDLS peer <addr>
  signal_poll = get signal parameters
  signal_monitor = set signal monitor parameters
  pktcnt_poll = get TX/RX packet counters
  reauthenticate = trigger IEEE 802.1X/EAPOL reauthentication
  wnm_sleep <enter/exit> [interval=#] = enter/exit WNM-Sleep mode
  wnm_bss_query <query reason> [list] [neighbor=<BSSID>,<BSSID information>,<operating class>,<channel number>,<PHY type>[,<hexdump of optional subelements>] = Send BSS Transition Management Query
  raw <params..> = Sent unprocessed command
  flush = flush wpa_supplicant state
  radio_work = radio_work <show/add/done>
  vendor <vendor id> <command id> [<hex formatted command argument>] = Send vendor command
  neighbor_rep_request [ssid=<SSID>] [lci] [civic] = Trigger request to AP for neighboring AP report (with optional given SSID in hex or enclosed in double quotes, default: current SSID; with optional LCI and location civic request)
  erp_flush = flush ERP keys
  mac_rand_scan <scan|sched|pno|all> enable=<0/1> [addr=mac-address mask=mac-address-mask] = scan MAC randomization
  get_pref_freq_list <interface type> = retrieve preferred freq list for the specified interface type
  p2p_lo_start <freq> <period> <interval> <count> = start P2P listen offload
  p2p_lo_stop = stop P2P listen offload
  dpp_qr_code report a scanned DPP URI from a QR Code
  dpp_bootstrap_gen type=<qrcode> [chan=..] [mac=..] [info=..] [curve=..] [key=..] = generate DPP bootstrap information
  dpp_bootstrap_remove *|<id> = remove DPP bootstrap information
  dpp_bootstrap_get_uri <id> = get DPP bootstrap URI
  dpp_bootstrap_info <id> = show DPP bootstrap information
  dpp_auth_init peer=<id> [own=<id>] = initiate DPP bootstrapping
  dpp_listen <freq in MHz> = start DPP listen
  dpp_stop_listen = stop DPP listen
  dpp_configurator_add [curve=..] [key=..] = add DPP configurator
  dpp_configurator_remove *|<id> = remove DPP configurator
  dpp_configurator_get_key <id> = Get DPP configurator's private key
  dpp_configurator_sign  conf=<role> configurator=<id> = generate self DPP configuration
  dpp_pkex_add add PKEX code
  dpp_pkex_remove *|<id> = remove DPP pkex information
  ignore_hw_pbc <0/1> = accept/ignore hw pbc event
  add_wps_pbc_ssid_filter <SSID sub-string> = add SSID sub-string to match
  del_wps_pbc_ssid_filter <SSID sub-string> = del SSID sub-string
  show_wps_pbc_ssid_filters = show SSID filters
  wps_status = get WPS status
  wps_timeout <timeout> = set WPS timeout value
  specific_scan <value> = set specific_scan parameter
  scan_buf_max_size <value> = scan results buffer max size

這功能也太多了,包山包海啊!!!
所以 Interactive mode 也有點困難。所以還是用Command line模式。

第二步:

# wpa_cli list_network <Enter>
Selected interface 'wifi0' 
network id  ssid /bssid /  flags
0                 7531_5G---      any

這裡是說,在網路 wifi0 有看到一個基地台,SSID叫做 7531_5G--- 

接下若沒有可以用add_network來加入。先用

# ifconfig <Enter> 這裡會列出所有網路的介面:

- eth1_0  // Gigabit Ethernet Port 0
- eth1_1 //  Gigabit Ethernet Port 0
- lo :       // Loopback port
- wifi0   // 5GHz uplink Wi-Fi Port
- wifi1   // 5Ghz access point Wi-Fi Port
- wlan0 // 2.4GHz Wi-Fi LAN port

如果沒有看到上面的命令的結果,可以用下面的命令,增加 

# wpa_cli add_network

加入後會取得一個ID,之後set_network要指定network ID
也可以用remove_network來移除網路。原先Network_Id 已經是 0,再設一次,就變成 1。

# wpa_cli remove_network

network可以同時間有好幾個,但enable只能有一個。

步驟三:

搜尋AP,並設定protocols key....
  
# wpa_cli scan <Enter>
OK
# wpa_cli scan_result <Enter>
Selected interface 'wifi0'
xx:xx:xx:xx:xx:xx       channel quality signal  noise   snr     intf    ch_util sta_cnt cascade rptype  rphyrat htcap   maxbw   nss     mcs cur_chan gap_40     gap_80  bf      phyrate thpt    flags / ssid
AC:6F:BB:99:32:8F       157     34      -56     -86     32      111     3       1       0       0       0       2       2       4       8       157     0       0       1       1386    1386    [WPA2-PSK-CCMP][WPS][ESS]  7531-NoRework-5G
1C:3B:F3:DC:E9:B3       157     18      -72     -86     16      111     0       0       0       0       0       2       2       3       3       0       0       0       0       346     346     [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS]  TP-Link_E9B4
84:0B:7C:5D:C9:57       149     5       -85     -87     3       4       0       0       0       0       0       2       2       4       0       0       0       0       0       129     129     [WPA2-PSK-CCMP][ESS]       5880-5G
04:D4:C4:2D:5E:7C       36      3       -87     -86     1       0       0       0       0       0       0       2       2       2       0       0       0       0       0       65      65      [WPA2-PSK-CCMP][ESS]       pai_5G
接下來些AP中的其中之一 (以下以 Wizelink-5G 為例)
# wpa_cli -i wifi0 ssid ".Wizelink'" key_mgmt WPA2-PSK psk '"00000000"'

假設新wifi的網絡ID為0 
設置wifi的ssid 
ssid需要添加雙引號,雙引號還要加斜槓或者單引號 
設置加密方式,WPA-PSK代表:WPA-PSK/WPA2-PSK 
psk : 設置wifi密碼,和帳號一樣需要雙引號 psk '"12345678"' 

這個步驟只是將連線資料設定起來,要連線還要下一個命令:

# wpa_cli scan
# wpa_cli scan_result
Selected interface 'wifi0'
bssid / frequency / signal level / rate / protocol caps / flags / ssid
xx:xx:xx:xx:xx:xx 2412 -45 54M 802.11b/g BW20 [WPA2-PSK-TKIP][ESS] MyWifiAP
# wpa_cli -i wifi0 remove_network 0
# wpa_cli -i wifi0 add_network
0
# wpa_cli -i wifi0 set_network 0 ssid '"MyWifiAP"'
OK
# wpa_cli -i wifi0 set_network 0 key_mgmt WPA-PSK
OK
# wpa_cli -i wifi0 set_network 0 psk '"Password"'
OK
# wpa_cli -i wifi0 set_network 0 pairwise TKIP
OK
# wpa_cli -i wifi0 set_network 0 group TKIP
OK
# wpa_cli -i wifi0 set_network 0 proto RSN
OK
# wpa_cli -i wifi0 enable_network 0
OK
# wpa_cli -i wifi0 select_network 0
OK
# wpa_cli -i wifi0 status
wpa_state=SCANNING
address=AC:6F:BB:AC:35:88
uuid=3cf09dec-a853-5ddd-bd9b-1eb7417afd58

再次查看狀態,查看是否已經連接網絡。wpa_state=COMPLETED表示完成連接,但是如果signal_level比較小時表示實際沒有連接 

# wpa_cli -i wlan0 status 

保存wifi配置,最好在啟用wifi後再保存,否則保存的wifi 預設為禁用的 

# wpa_cli -i wlan0 save_config
  
之後可以用ifconfig 或iwconfig來確認是否有抓到IP。

沒有留言:

張貼留言