Linux 无线WIFI搜索带信号强度,使用nmcli或iw

方法1:nmcli

root@xxxxxx:~# sudo nmcli dev

DEVICE TYPE STATE CONNECTION

eth0 ethernet connected Wired connection 1

wlan0 wifi disconnected --

sit0 iptunnel unmanaged --

lo loopback unmanaged --

root@xxxxxx:~# sudo nmcli dev wifi

IN-USE SSID MODE CHAN RATE SIGNAL BARS SECURITY

Robot Infra 1 130 Mbit/s 55 ▂▄__ WPA2

charger Infra 1 270 Mbit/s 12 ▂___ WPA2

root@xxxxxx:~#

方法2:iw

root@ChargerV3:~# iw dev
phy#0
Interface wlan0
ifindex 7
wdev 0x2
addr 40:d6:3c:2c:bc:4e
ssid charger
type AP
channel 1 (2412 MHz), width: 20 MHz, center1: 2412 MHz
txpower 20.00 dBm
multicast TXQ:
qsz-byt qsz-pkt flows   drops   marks   overlmt hashcol tx-bytes        tx-packets
0       0       110912  0       0       0       9       29130312                114505
root@ChargerV3:~# iw wlan0 scan | grep 'SSID|freq|signal|capability|BSS'
BSS f2:c8:14:51:7a:aa(on wlan0)
freq: 2412
capability: ESS (0x0431)
signal: -57.00 dBm
SSID: Robot
BSS f0:c8:14:51:5a:78(on wlan0)
freq: 2412
capability: ESS (0x0431)
signal: -26.00 dBm
SSID: charger

nmcli 

一、概述

以往我都是在linux的系统中都是通过配置文件来配置和修改ip的,今天我们就来说一说nmcli命令配置网络。

目前所有的linux系统配置网卡等信息,均已经推行Network-Manager服务去管理。nmcli 是 NetworkManager 的命令行工具。nm 代表 NetworkManager,cli 代表 Command-Line 命令行。

二、常用参数使用

我们可以看help查看参数,man nmcli查看参数详细使用

xu@xu-pc:~$ nmcli help
Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }

OPTIONS
  -a, --ask                                ask for missing parameters
  -c, --colors auto|yes|no                 whether to use colors in output
  -e, --escape yes|no                      escape columns separators in values
  -f, --fields <field,...>|all|common      specify fields to output
  -g, --get-values <field,...>|all|common  shortcut for -m tabular -t -f
  -h, --help                               print this help
  -m, --mode tabular|multiline             output mode
  -o, --overview                           overview mode
  -p, --pretty                             pretty output
  -s, --show-secrets                       allow displaying passwords
  -t, --terse                              terse output
  -v, --version                            show program version
  -w, --wait <seconds>                     set timeout waiting for finishing operations

OBJECT
  g[eneral]       NetworkManager's general status and operations
  n[etworking]    overall networking control
  r[adio]         NetworkManager radio switches
  c[onnection]    NetworkManager's connections
  d[evice]        devices managed by NetworkManager
  a[gent]         NetworkManager secret agent or polkit agent
  m[onitor]       monitor NetworkManager changes

xu@xu-pc:~$

常用参数如下:

常用参数

释义

g[eneral]

NetworkManager的一般状态和操作

n[etworking]

网络整体控制

r[adio]

NetworkManager无线交换机

c[onnection]

NetworkManager无线交换机

d[evice]

通过NetworkManager管理设备

a[gent]

NetworkManager秘密代理或polkit代理

m[onitor]

监控NetworkManager更改

2.1 nmcli networking

1.显示NM是否接管网络
xu@xu-pc:~$ nmcli networking
enabled
xu@xu-pc:~$
或者
xu@xu-pc:~$ nmcli n
enabled
xu@xu-pc:~$
2.查看网络连接状态
xu@xu-pc:~$ nmcli n connectivity
full
xu@xu-pc:~$
或者
xu@xu-pc:~$ nmcli n c
full
xu@xu-pc:~$

五种状态:

  • full:联网
  • limited:联网,但无法上网
  • portal:联网,但需要登录认证
  • none:没网
  • unknown:未知
3.开/关网络连接
nmcli n on

nmcli n off

2.2 general

xu@xu-pc:~$ nmcli general help
Usage: nmcli general { COMMAND | help }

COMMAND := { status | hostname | permissions | logging }

  status

  hostname [<hostname>]

  permissions

  logging [level <log level>] [domains <log domains>]

xu@xu-pc:~$
1.显示系统网络状态
nmcli general status
#按照之前所说的简写规则,可以简写为
nmcli g s
  • STATE:网络是否连接
  • CONNECTIVITY:同nmcli n c
  • WIFI-HW:WIFI硬件开关
  • WIFI:WIFI软件开关
  • WWAN-HW:WWAN硬件开关
  • WWAN:WWAN软件开关
2.显示主机名
nmcli general host

nmcli g h
3.更改主机名
nmcli general hostname newhostname
nmcli g h newhostname

主机名存放在/etc/hostname中,修改后需要重启NM。

2.3 nmcli connection

xu@xu-pc:~$ nmcli connection help
Usage: nmcli connection { COMMAND | help }

COMMAND := { show | up | down | add | modify | clone | edit | delete | monitor | reload | load | import | export }

  show [--active] [--order <order spec>]
  show [--active] [id | uuid | path | apath] <ID> ...

  up [[id | uuid | path] <ID>] [ifname <ifname>] [ap <BSSID>] [passwd-file <file with passwords>]

  down [id | uuid | path | apath] <ID> ...

  add COMMON_OPTIONS TYPE_SPECIFIC_OPTIONS SLAVE_OPTIONS IP_OPTIONS [-- ([+|-]<setting>.<property> <value>)+]

  modify [--temporary] [id | uuid | path] <ID> ([+|-]<setting>.<property> <value>)+

  clone [--temporary] [id | uuid | path ] <ID> <new name>

  edit [id | uuid | path] <ID>
  edit [type <new_con_type>] [con-name <new_con_name>]

  delete [id | uuid | path] <ID>

  monitor [id | uuid | path] <ID> ...

  reload

  load <filename> [ <filename>... ]

  import [--temporary] type <type> file <file to import>

  export [id | uuid | path] <ID> [<output file>]

xu@xu-pc:~$
1.显示所有网络连接
nmcli connection show

nmcli c s

nmcli c #show是nmcli c的默认项,因此可以省略

显示当前启动的连接

1

2

nmcli c s --active

nmcli c s -a #因为s后边有参数,因此不能省略

2.显示某个网卡的详细信息

1

nmcli c s 设备名

3.启动/关闭指定连接

1

2

3

4

5

# 如果某个设备原本就处于连接状态,那么该命令会重启它

nmcli c up 设备名

  

#  关闭之后,再用nmcli c查看,DEVICE项将显示--。

nmcli c down 设备名

4.修改

1

2

nmcli c modify 设备名 [+ | -]选项 选项值

nmcli c m 设备名 [+ | -]选项 选项值

常用修改示例

1

2

3

4

5

6

7

8

nmcli c m ens192 ipv4.address 192.168.2.201/24   # 修改 IP 地址和子网掩码

nmcli c m ens192 +ipv4.addresses 192.168.2.100/24

nmcli c m ens192 ipv4.method manual             # 修改为静态配置,默认是 auto

nmcli c m ens192 ipv4.gateway 192.168.2.2       # 修改默认网关

nmcli c m ens192 ipv4.dns 192.168.2.2           # 修改 DNS

nmcli c m ens192 +ipv4.dns 114.114.114.114      # 添加一个 DNS

nmcli c m ens192 ipv6.method ignored            # 将 IPv6 禁用,针对CentOS8,三个可选项:disabled、auto、manual

nmcli c m ens192 connection.autoconnect yes     # 开机启动

注意在修改method前,要先修改address

使用空引号""代替选项值,可以将选项设回默认值

1

nmcli c m ens33 ipv4.method ""

如下修改主机IP地址

1

nmcli connection modify ens192 ipv4.address 192.168.2.201/24

5.新增连接

1

2

nmcli c add type 连接类型 选项 选项值 

nmcli c a type 连接类型 选项 选项值

通常用的type是802-3-ethernet(别名 ethernet)。

选项同之前修改连接的选项,其中有几个重要选项:

1

2

3

4

5

connection.id(别名 con-name):连接名;

  

connection.interface-name(别名 ifname):设备名;

  

connection.autoconnect(别名 autoconnect):是否开机自启,其值只能是 yes 或 no,默认 yes

举例

1

nmcli c a type ethernet con-name ens33 ifname ens33

6.删除连接

1

2

nmcli c delete ens33 

nmcli c de ens33      # delete 不可简写为 d,否则与 down 冲突,但可以简写为 de

7.重载

1

2

3

4

5

6

7

8

9

nmcli c reload  # 或

nmcli c r

  

nmcli c load ifcfg-ens33  # 或

nmcli c l ifcfg-ens33

  

  

## 指定文件

nmcli connection  load  /etc/sysconfig/network-scripts/ifcfg-ens192

8.监控连接

1

2

# 监控ens192

nmcli connection   monitor ffb0dc33-75d6-42b7-8cd4-509c465448e8

2.4 nmcli device

xu@xu-pc:~$ nmcli device h
Usage: nmcli device { COMMAND | help }

COMMAND := { status | show | set | connect | reapply | modify | disconnect | delete | monitor | wifi | lldp }

  status

  show [<ifname>]

  set [ifname] <ifname> [autoconnect yes|no] [managed yes|no]

  connect <ifname>

  reapply <ifname>

  modify <ifname> ([+|-]<setting>.<property> <value>)+

  disconnect <ifname> ...

  delete <ifname> ...

  monitor <ifname> ...

  wifi [list [ifname <ifname>] [bssid <BSSID>] [--rescan yes|no|auto]]

  wifi connect <(B)SSID> [password <password>] [wep-key-type key|phrase] [ifname <ifname>]
                         [bssid <BSSID>] [name <name>] [private yes|no] [hidden yes|no]

  wifi hotspot [ifname <ifname>] [con-name <name>] [ssid <SSID>] [band a|bg] [channel <channel>] [password <password>]

  wifi rescan [ifname <ifname>] [[ssid <SSID to scan>] ...]

  wifi show-password [ifname <ifname>]

  lldp [list [ifname <ifname>]]

xu@xu-pc:~$
1.显示所有网络接口设备状态

1

2

3

nmcli device status

nmcli d s

nmcli d

2.显示所有设备详细信息

1

2

3

4

5

nmcli d show 

nmcli d sh         # show 不可简写为 s,否则与 status 冲突,但可以简写为 sh

  

# 显示指定设备详细信息

nmcli d sh ens192

3.连接设备

1

2

nmcli d connect ens192  # 或

nmcli d c ens33

4.断开

1

2

nmcli d disconnect ens192  # 或

nmcli d d ens33

5.更新设备

1

2

nmcli d reapply ens192  # 或

nmcli d r ens33

iw

iw 是一种新的基于 nl80211 的用于无线设备的CLI配置实用程序。它支持最近已添加到内核所有新的驱动程序。采用无线扩展接口的旧工具iwconfig已被废弃,强烈建议切换到 iw 和 nl80211。

xu@xu-pc:~$ iw help
Usage:  iw [options] command
Options:
        --debug         enable netlink debugging
        --version       show version (5.16)
Commands:
        dev <devname> ap start
                <SSID> <control freq> [5|10|20|40|80|80+80|160] [<center1_freq> [<center2_freq>]] <beacon interval in TU> <DTIM period> [hidden-ssid|zeroed-ssid] head <beacon head in hexadecimal> [tail <beacon tail in hexadecimal>] [inactivity-time <inactivity time in seconds>] [key0:abcde d:1:6162636465]


        dev <devname> ap stop
                Stop AP functionality


        phy <phyname> coalesce enable <config-file>
                Enable coalesce with given configuration.
                The configuration file contains coalesce rules:
                  delay=<delay>
                  condition=<condition>
                  patterns=<[offset1+]<pattern1>,<[offset2+]<pattern2>,...>
                  delay=<delay>
                  condition=<condition>
                  patterns=<[offset1+]<pattern1>,<[offset2+]<pattern2>,...>
                  ...
                delay: maximum coalescing delay in msec.
                condition: 1/0 i.e. 'not match'/'match' the patterns
                patterns: each pattern is given as a bytestring with '-' in
                places where any byte may be present, e.g. 00:11:22:-:44 will
                match 00:11:22:33:44 and 00:11:22:33:ff:44 etc. Offset and
                pattern should be separated by '+', e.g. 18+43:34:00:12 will
                match '43:34:00:12' after 18 bytes of offset in Rx packet.


        phy <phyname> coalesce disable
                Disable coalesce.

        phy <phyname> coalesce show
                Show coalesce status.

        dev <devname> disconnect
                Disconnect from the current network.

        dev <devname> connect [-w] <SSID> [<freq in MHz>] [<bssid>] [auth open|shared] [key 0:abcde d:1:6162636465] [mfp:req/opt/no]
                Join the network with the given SSID (and frequency, BSSID).
                With -w, wait for the connect to finish or fail.

        dev <devname> auth <SSID> <bssid> <type:open|shared> <freq in MHz> [key 0:abcde d:1:6162636465]
                Authenticate with the given network.


        dev <devname> cqm rssi <threshold|off> [<hysteresis>]
                Set connection quality monitor RSSI threshold.


        event [-t|-T|-r] [-f]
                Monitor events from the kernel.
                -t - print timestamp
                -T - print absolute, human-readable timestamp
                -r - print relative timestamp
                -f - print full frame for auth/assoc etc.

        dev <devname> ftm get_stats
                Get FTM responder statistics.


        dev <devname> ftm start_responder [lci=<lci buffer in hex>] [civic=<civic buffer in hex>]
                Start an FTM responder. Needs a running ap interface


        phy <phyname> hwsim getps


        phy <phyname> hwsim setps <value>


        phy <phyname> hwsim stopqueues


        phy <phyname> hwsim wakequeues


        dev <devname> ibss leave
                Leave the current IBSS cell.

        dev <devname> ibss join <SSID> <freq in MHz> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz] [fixed-freq] [<fixed bssid>] [beacon-interval <TU>] [basic-rates <rate in Mbps,rate2,...>] [mcast-rate <rate in Mbps>] [key d:0:abcde]
                Join the IBSS cell with the given SSID, if it doesn't exist create
                it on the given frequency. When fixed frequency is requested, don't
                join/create a cell on a different frequency. When a fixed BSSID is
                requested use that BSSID and do not adopt another cell's BSSID even
                if it has higher TSF and the same SSID. If an IBSS is created, create
                it with the specified basic-rates, multicast-rate and beacon-interval.

        phy <phyname> info
                Show capabilities for the specified wireless device.

        list
                List all wireless devices and their capabilities.

        phy
        commands
                list all known commands and their decimal & hex value

        features


        phy <phyname> interface add <name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*] [addr <mac-addr>]
                Add a new virtual interface with the given configuration.
                Valid interface types are: managed, ibss, monitor, mesh, wds.

                The flags are only used for monitor interfaces, valid flags are:
                none:     no special flags
                fcsfail:  show frames with FCS errors
                control:  show control frames
                otherbss: show frames from other BSSes
                cook:     use cooked mode
                active:   use active mode (ACK incoming unicast packets)
                mumimo-groupid <GROUP_ID>: use MUMIMO according to a group id
                mumimo-follow-mac <MAC_ADDRESS>: use MUMIMO according to a MAC address

                The mesh_id is used only for mesh mode.

        dev <devname> interface add <name> type <type> [mesh_id <meshid>] [4addr on|off] [flags <flag>*] [addr <mac-addr>]
        dev <devname> del
                Remove this virtual interface

        dev <devname> info
                Show information for this interface.

        dev
                List all network interfaces for wireless hardware.

        dev <devname> switch freq <freq> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz] [beacons <count>] [block-tx]
        dev <devname> switch freq <control freq> [5|10|20|40|80|80+80|160] [<center1_freq> [<center2_freq>]] [beacons <count>] [block-tx]
                Switch the operating channel by sending a channel switch announcement (CSA).

        dev <devname> switch channel <channel> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz] [beacons <count>] [block-tx]
        help [command]
                Print usage for all or a specific command, e.g.
                "help wowlan" or "help wowlan enable".

        dev <devname> link
                Print information about the current link, if any.

        dev <devname> measurement ftm_request <config-file> [timeout=<seconds>] [randomise[=<addr>/<mask>]]
                Send an FTM request to the targets supplied in the config file.
                Each line in the file represents a target, with the following format:
                <addr> bw=<[20|40|80|80+80|160]> cf=<center_freq> [cf1=<center_freq1>] [cf2=<center_freq2>] [ftms_per_burst=<samples per burst>] [ap-tsf] [asap] [bursts_exp=<num of bursts exponent>] [burst_period=<burst period>] [retries=<num of retries>] [burst_duration=<burst duration>] [preamble=<legacy,ht,vht,dmg>] [lci] [civic] [tb] [non_tb]

        dev <devname> mesh join <mesh ID> [[freq <freq in MHz> <NOHT|HT20|HT40+|HT40-|80MHz>] [basic-rates <rate in Mbps,rate2,...>]], [mcast-rate <rate in Mbps>] [beacon-interval <time in TUs>] [dtim-period <value>] [vendor_sync on|off] [<param>=<value>]*
                Join a mesh with the given mesh ID with frequency, basic-rates,
                mcast-rate and mesh parameters. Basic-rates are applied only if
                frequency is provided.

        dev <devname> mesh leave
                Leave a mesh.

        dev <devname> mesh_param dump
                List all supported mesh parameters

        dev <devname> mgmt dump frame <type as hex ab> <pattern as hex ab:cd:..> [frame <type> <pattern>]* [count <frames>]
                Register for receiving certain mgmt frames and print them.
                Frames are selected by their type and pattern containing
                the first several bytes of the frame that should match.

                Example: iw dev wlan0 mgmt dump frame 40 00 frame 40 01:02 count 10


        dev <devname> mpath probe <destination MAC address> frame <frame>
                Inject ethernet frame to given peer overriding the next hop
                lookup from mpath table.
                .Example: iw dev wlan0 mpath probe xx:xx:xx:xx:xx:xx frame 01:xx:xx:00


        dev <devname> mpath get <MAC address>
                Get information on mesh path to the given node.

        dev <devname> mpath del <MAC address>
                Remove the mesh path to the given node.

        dev <devname> mpath new <destination MAC address> next_hop <next hop MAC address>
                Create a new mesh path (instead of relying on automatic discovery).

        dev <devname> mpath set <destination MAC address> next_hop <next hop MAC address>
                Set an existing mesh path's next hop.

        dev <devname> mpath dump
                List known mesh paths.

        dev <devname> mpp get <MAC address>
                Get information on mesh proxy path to the given node.

        dev <devname> mpp dump
                List known mesh proxy paths.

        wdev <idx> nan start pref <pref> [bands [2GHz] [5GHz]]


        wdev <idx> nan stop


        wdev <idx> nan config [pref <pref>] [bands [2GHz] [5GHz]]


        wdev <idx> nan rm_func cookie <cookie>


        wdev <idx> nan add_func type <publish|subscribe|followup> [active] [solicited] [unsolicited] [bcast] [close_range] name <name> [info <info>] [flw_up_id <id> flw_up_req_id <id> flw_up_dest <mac>] [ttl <ttl>] [srf <include|exclude> <bf|list> [bf_idx] [bf_len] <mac1;mac2...>] [rx_filter <str1:str2...>] [tx_filter <str1:str2...>]


        dev <devname> ocb join <freq in MHz> <5MHz|10MHz>
                Join the OCB mode network.

        dev <devname> ocb leave
                Leave the OCB mode network.

        dev <devname> offchannel <freq> <duration>
                Leave operating channel and go to the given channel for a while.

        wdev <idx> p2p start


        wdev <idx> p2p stop


        phy <phyname> channels
                Show available channels.

        dev <devname> cac channel <channel> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz]
        dev <devname> cac freq <freq> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz]
        dev <devname> cac freq <control freq> [5|10|20|40|80|80+80|160] [<center1_freq> [<center2_freq>]]
        dev <devname> cac trigger channel <channel> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz]
        dev <devname> cac trigger freq <frequency> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz]
        dev <devname> cac trigger freq <frequency> [5|10|20|40|80|80+80|160] [<center1_freq> [<center2_freq>]]
                Start or trigger a channel availability check (CAC) looking to look for
                radars on the given channel.

        reg set <ISO/IEC 3166-1 alpha2>
                Notify the kernel about the current regulatory domain.

        reg get
                Print out the kernel's current regulatory domain information.

        phy <phyname> reg get
                Print out the devices' current regulatory domain information.

        reg reload
                Reload the kernel's regulatory database.

        dev <devname> roc start <freq> <time in ms>


        dev <devname> scan [-u] [freq <freq>*] [duration <dur>] [ies <hex as 00:11:..>] [meshid <meshid>] [lowpri,flush,ap-force,duration-mandatory] [randomise[=<addr>/<mask>]] [ssid <ssid>*|passive]
                Scan on the given frequencies and probe for the given SSIDs
                (or wildcard if not given) unless passive scanning is requested.
                If -u is specified print unknown data in the scan results.
                Specified (vendor) IEs must be well-formed.

        dev <devname> scan dump [-u]
                Dump the current scan results. If -u is specified, print unknown
                data in scan results.

        dev <devname> scan trigger [freq <freq>*] [duration <dur>] [ies <hex as 00:11:..>] [meshid <meshid>] [lowpri,flush,ap-force,duration-mandatory,coloc] [randomise[=<addr>/<mask>]] [ssid <ssid>*|passive]
                Trigger a scan on the given frequencies with probing for the given
                SSIDs (or wildcard if not given) unless passive scanning is requested.
                Duration(in TUs), if specified, will be used to set dwell times.


        dev <devname> scan abort
                Abort ongoing scan

        dev <devname> scan sched_start [interval <in_msecs> | scan_plans [<interval_secs:iterations>*] <interval_secs>] [delay <in_secs>] [freqs <freq>+] [matches [ssid <ssid>]+]] [active [ssid <ssid>]+|passive] [randomise[=<addr>/<mask>]] [coloc] [flush]
                Start a scheduled scan at the specified interval on the given frequencies
                with probing for the given SSIDs (or wildcard if not given) unless passive
                scanning is requested.  If matches are specified, only matching results
                will be returned.

        dev <devname> scan sched_stop
                Stop an ongoing scheduled scan.

        dev <devname> get mesh_param [<param>]
                Retrieve mesh parameter (run command without any to see available ones).

        phy <phyname> get txq
                Get TXQ parameters.

        dev <devname> get power_save
                Retrieve power save state.

        dev <devname> set bitrates [legacy-<2.4|5> <legacy rate in Mbps>*] [ht-mcs-<2.4|5> <MCS index>*] [vht-mcs-<2.4|5> [he-mcs-<2.4|5|6> <NSS:MCSx,MCSy... | NSS:MCSx-MCSy>*] [sgi-2.4|lgi-2.4] [sgi-5|lgi-5] [he-gi-<2.4|5|6> <0.8|1.6|3.2>] [he-ltf-<2.4|5|6> <1|2|4>]
                Sets up the specified rate masks.
                Not passing any arguments would clear the existing mask (if any).

        dev <devname> set monitor <flag>*
                Set monitor flags. Valid flags are:
                none:     no special flags
                fcsfail:  show frames with FCS errors
                control:  show control frames
                otherbss: show frames from other BSSes
                cook:     use cooked mode
                active:   use active mode (ACK incoming unicast packets)
                mumimo-groupid <GROUP_ID>: use MUMIMO according to a group id
                mumimo-follow-mac <MAC_ADDRESS>: use MUMIMO according to a MAC address

        dev <devname> set meshid <meshid>
        dev <devname> set type <type>
                Set interface type/mode.
                Valid interface types are: managed, ibss, monitor, mesh, wds.

        dev <devname> set 4addr <on|off>
                Set interface 4addr (WDS) mode.

        dev <devname> set noack_map <map>
                Set the NoAck map for the TIDs. (0x0009 = BE, 0x0006 = BK, 0x0030 = VI, 0x00C0 = VO)

        dev <devname> set peer <MAC address>
                Set interface WDS peer.

        dev <devname> set mcast_rate <rate in Mbps>
                Set the multicast bitrate.

        dev <devname> set tidconf [peer <MAC address>] tids <mask> [override] [sretry <num>] [lretry <num>] [ampdu [on|off]] [amsdu [on|off]] [noack [on|off]] [rtscts [on|off]][bitrates <type [auto|fixed|limit]> [legacy-<2.4|5> <legacy rate in Mbps>*] [ht-mcs-<2.4|5> <MCS index>*] [vht-mcs-<2.4|5> <NSS:MCSx,MCSy... | NSS:MCSx-MCSy>*] [sgi-2.4|lgi-2.4] [sgi-5|lgi-5]]
                Setup per-node TID specific configuration for TIDs selected by bitmask.
                If MAC address is not specified, then supplied TID configuration
                applied to all the peers.
                Examples:
                  $ iw dev wlan0 set tidconf tids 0x1 ampdu off
                  $ iw dev wlan0 set tidconf tids 0x5 ampdu off amsdu off rtscts on
                  $ iw dev wlan0 set tidconf tids 0x3 override ampdu on noack on rtscts on
                  $ iw dev wlan0 set tidconf peer xx:xx:xx:xx:xx:xx tids 0x1 ampdu off tids 0x3 amsdu off rtscts on
                  $ iw dev wlan0 set tidconf peer xx:xx:xx:xx:xx:xx tids 0x2 bitrates auto
                  $ iw dev wlan0 set tidconf peer xx:xx:xx:xx:xx:xx tids 0x2 bitrates limit vht-mcs-5 4:9


        dev <devname> set mesh_param <param>=<value> [<param>=<value>]*
                Set mesh parameter (run command without any to see available ones).

        phy <phyname> set name <new name>
                Rename this wireless device.

        phy <phyname> set freq <freq> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz|160MHz]
        phy <phyname> set freq <control freq> [5|10|20|40|80|80+80|160] [<center1_freq> [<center2_freq>]]
                Set frequency/channel the hardware is using, including HT
                configuration.

        dev <devname> set freq <freq> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz|160MHz]
        dev <devname> set freq <control freq> [5|10|20|40|80|80+80|160] [<center1_freq> [<center2_freq>]]
        phy <phyname> set channel <channel> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz|160MHz]
        dev <devname> set channel <channel> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz|160MHz]
        phy <phyname> set frag <fragmentation threshold|off>
                Set fragmentation threshold.

        phy <phyname> set rts <rts threshold|off>
                Set rts threshold.

        phy <phyname> set retry [short <limit>] [long <limit>]
                Set retry limit.

        phy <phyname> set netns { <pid> | name <nsname> }
                Put this wireless device into a different network namespace:
                    <pid>    - change network namespace by process id
                    <nsname> - change network namespace by name from /run/netns
                               or by absolute path (man ip-netns)


        phy <phyname> set coverage <coverage class>
                Set coverage class (1 for every 3 usec of air propagation time).
                Valid values: 0 - 255.

        phy <phyname> set distance <auto|distance>
                Enable ACK timeout estimation algorithm (dynack) or set appropriate
                coverage class for given link distance in meters.
                To disable dynack set valid value for coverage class.
                Valid values: 0 - 114750

        phy <phyname> set txpower <auto|fixed|limit> [<tx power in mBm>]
                Specify transmit power level and setting type.

        dev <devname> set txpower <auto|fixed|limit> [<tx power in mBm>]
                Specify transmit power level and setting type.

        phy <phyname> set antenna <bitmap> | all | <tx bitmap> <rx bitmap>
                Set a bitmap of allowed antennas to use for TX and RX.
                The driver may reject antenna configurations it cannot support.

        phy <phyname> set txq limit <packets> | memory_limit <bytes> | quantum <bytes>
                Set TXQ parameters. The limit and memory_limit are global queue limits
                for the whole phy. The quantum is the DRR scheduler quantum setting.
                Valid values: 1 - 2**32

        dev <devname> set power_save <on|off>
                Set power save state to on or off.

        phy <phyname> set sar_specs <sar type> <range index:sar power>*
                Set SAR specs corresponding to SAR capa of wiphy.

        dev <devname> survey dump
                List all gathered channel survey data

        dev <devname> vendor send <oui> <subcmd> <filename|-|hex data>


        dev <devname> vendor recv <oui> <subcmd> <filename|-|hex data>


        dev <devname> vendor recvbin <oui> <subcmd> <filename|-|hex data>


        phy <phyname> wowlan enable [any] [disconnect] [magic-packet] [gtk-rekey-failure] [eap-identity-request] [4way-handshake] [rfkill-release] [net-detect [interval <in_msecs> | scan_plans [<interval_secs:iterations>*] <interval_secs>] [delay <in_secs>] [freqs <freq>+] [matches [ssid <ssid>]+]] [active [ssid <ssid>]+|passive] [randomise[=<addr>/<mask>]] [coloc] [flush]] [tcp <config-file>] [patterns [offset1+]<pattern1> ...]
                Enable WoWLAN with the given triggers.
                Each pattern is given as a bytestring with '-' in places where any byte
                may be present, e.g. 00:11:22:-:44 will match 00:11:22:33:44 and
                00:11:22:33:ff:44 etc.
                Offset and pattern should be separated by '+', e.g. 18+43:34:00:12 will match '43:34:00:12' after 18 bytes of offset in Rx packet.

                The TCP configuration file contains:
                  source=ip[:port]
                  dest=ip:port@mac
                  data=<hex data packet>
                  data.interval=seconds
                  [wake=<hex packet with masked out bytes indicated by '-'>]
                  [data.seq=len,offset[,start]]
                  [data.tok=len,offset,<token stream>]

                Net-detect configuration example:
                 iw phy0 wowlan enable net-detect interval 5000 delay 30 freqs 2412 2422 matches ssid foo ssid bar

        phy <phyname> wowlan disable
                Disable WoWLAN.

        phy <phyname> wowlan show
                Show WoWLAN status.

        dev <devname> station get <MAC address>
                Get information for a specific station.

        dev <devname> station del <MAC address> [subtype <subtype>] [reason-code <code>]
                Remove the given station entry (use with caution!)
                Example subtype values: 0xA (disassociation), 0xC (deauthentication)

        dev <devname> station dump [-v]
                List all stations known, e.g. the AP on managed interfaces

        dev <devname> station set <MAC address> txpwr <auto|limit> [<tx power dBm>]
                Set Tx power for this station.

        dev <devname> station set <MAC address> airtime_weight <weight>
                Set airtime weight for this station.

        dev <devname> station set <MAC address> mesh_power_mode <active|light|deep>
                Set link-specific mesh power mode for this station

        dev <devname> station set <MAC address> vlan <ifindex>
                Set an AP VLAN for this station.

        dev <devname> station set <MAC address> plink_action <open|block>
                Set mesh peer link action for this station (peer).


Commands that use the netdev ('dev') can also be given the
'wdev' instead to identify the device.

You can omit the 'phy' or 'dev' if the identification is unique,
e.g. "iw wlan0 info" or "iw phy0 info". (Don't when scripting.)

Do NOT screenscrape this tool, we don't consider its output stable.

xu@xu-pc:~$
# iw help    # 帮助
# iw list    # 获得所有设备的功能,如带宽信息(2.4GHz,和5GHz),和802.11n的信息
# iw dev wlan0 scan    # 扫描
# iw event    # 监听事件
# iw dev wlan0 link    # 获得链路状态
# iw wlan0 connect foo    # 连接到已禁用加密的AP,这里它的SSID是foo
# iw wlan0 connect foo 2432  # 假设你有两个AP SSID 都是 foo ,你知道你要连接的是在 2432 频道
# iw wlan0 connect foo keys 0:abcde d:1:0011223344    # 连接到使用WEP的AP
# iw dev wlan1 station dump    # 获取station 的统计信息
# iw dev wlan1 station get     # 获得station对应的peer统计信息
# iw wlan0 set bitrates legacy-2.4 12 18 24    # 修改传输比特率
# iw dev wlan0 set bitrates mcs-5 4    # 修改tx HT MCS的比特率
# iw dev wlan0 set bitrates mcs-2.4 10  
# iw dev wlan0 set bitrates mcs-5    # 清除所有 tx 比特率和设置的东西来恢复正常
# iw dev  set txpower  []   #设置传输功率
# iw phy  set txpower  []   #设置传输功率
# iw dev wlan0 set power_save on  #设置省电模式
# iw dev wlan0 get power_save  #查询当前的节电设定
# iw phy phy0 interface add moni0 type monitor  #添加一个 monitor 接口

基本使用

  1. 扫描网络:通过“iw dev wlan0 scan”命令,可以实时扫描周围的无线网络,并显示各网络的详细信息,如SSID、信号强度和频率等。
  2. 获取连接状态:使用“iw dev wlan0 link”命令,可以获得设备的连接状态信息,但不包括IP地址。
  3. 设置与查询功率:可以通过“iw dev wlan0 set txpower”和“iw dev wlan0 get txpower”分别设置和查询设备的传输功率。
  4. 连接网络:通过“iw wlan0 connect foo”命令,可以连接到指定的网络(在此例中为SSID为“foo”的网络)。如果需要指定频道,可以使用“iw wlan0 connect foo 2432”,在有两个相同SSID的情况下特别有用。
  5. 修改传输比特率:可以使用“iw dev wlan0 set bitrates legacy-2.4 12 18 24”来修改设备的传输比特率,以适应不同的网络环境。

高级功能

  1. 监听网络事件:通过“iw event”命令,可以获取所有网络设备的工作日志信息,有助于了解设备运行状况。
  2. 设置监视模式:可以将无线接口设置为监控模式,命令为“iw phy phy0 interface add moni0 type monitor”,这在网络监控和数据分析时非常有用。
  3. 省电模式:为了节省电量,可以使用“iw dev wlan0 set power_save on”来开启省电模式,并通过“iw dev wlan0 get power_save”来查询当前的节电设定。
  • 20
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Qt 中,可以使用 QSystemNetworkInfo 类来获取当前设备的网络信息。QSystemNetworkInfo 类可以提供有关设备的网络连接状态和网络类型的信息。但是,它不提供有关具体 WiFi 网络的信息,例如信号强度。 要获取 WiFi 信号强度,需要使用操作系统特定的 API。在 Windows 平台上,可以使用 Windows API 来获取 WiFi 信号强度。以下是一个使用 Windows API 获取附近 WiFi 网络信号强度的示例代码: ```cpp #include <windows.h> #include <wlanapi.h> #pragma comment(lib, "wlanapi.lib") #pragma comment(lib, "ole32.lib") void getWifiSignalStrength() { HANDLE clientHandle; DWORD negotiatedVersion; DWORD dwResult = WlanOpenHandle(2, NULL, &negotiatedVersion, &clientHandle); if (dwResult != ERROR_SUCCESS) { return; } GUID interfaceGuid; WLAN_INTERFACE_INFO_LIST* pInterfaceList = NULL; dwResult = WlanEnumInterfaces(clientHandle, NULL, &pInterfaceList); if (dwResult != ERROR_SUCCESS) { WlanCloseHandle(clientHandle, NULL); return; } interfaceGuid = pInterfaceList->InterfaceInfo[0].InterfaceGuid; WLAN_AVAILABLE_NETWORK_LIST* pAvailableNetworkList = NULL; dwResult = WlanGetAvailableNetworkList(clientHandle, &interfaceGuid, 0, NULL, &pAvailableNetworkList); if (dwResult != ERROR_SUCCESS) { WlanFreeMemory(pInterfaceList); WlanCloseHandle(clientHandle, NULL); return; } for (DWORD i = 0; i < pAvailableNetworkList->dwNumberOfItems; i++) { WLAN_AVAILABLE_NETWORK network = pAvailableNetworkList->Network[i]; int signalStrength = network.wlanSignalQuality; // do something with signalStrength } WlanFreeMemory(pAvailableNetworkList); WlanFreeMemory(pInterfaceList); WlanCloseHandle(clientHandle, NULL); } ``` 在 Linux 平台上,可以使用 iwlib 库来获取 WiFi 信号强度。以下是一个使用 iwlib 库获取附近 WiFi 网络信号强度的示例代码: ```cpp #include <iwlib.h> void getWifiSignalStrength() { int sockfd = iw_sockets_open(); if (sockfd < 0) { return; } char* ifname = "wlan0"; // replace with your interface name wireless_scan_head head; wireless_scan(ifname, sockfd, &head); wireless_scan* scan = head.result; while (scan != NULL) { char essid[IW_ESSID_MAX_SIZE + 1]; iw_ether_ntop(&scan->ap_addr.sa_data[0], essid); int signalStrength = scan->stats.qual.level; // do something with signalStrength scan = scan->next; } iw_sockets_close(sockfd); } ``` 请注意,这些示例代码仅适用于 Windows 和 Linux 平台,并且可能需要特权权限才能运行。在使用这些代码之前,请仔细阅读并遵循相关操作系统和库的文档和安全建议。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值