linux系统通信功率信道修改方法
配置频段
可以先查看有哪些频段:
iwlist channel
可用信道
wlan0 13 channels in total; available frequencies :
Channel 01 : 2.412 GHz
Channel 02 : 2.417 GHz
Channel 03 : 2.422 GHz
Channel 04 : 2.427 GHz
Channel 05 : 2.432 GHz
Channel 06 : 2.437 GHz
Channel 07 : 2.442 GHz
Channel 08 : 2.447 GHz
Channel 09 : 2.452 GHz
Channel 10 : 2.457 GHz
Channel 11 : 2.462 GHz
Channel 12 : 2.467 GHz
Channel 13 : 2.472 GHz
Current Frequency:2.422 GHz (Channel 3)
改变信道
sudo iwconfig wlan1 channel 1
iwlist channel显示:
13 channels in total; available frequencies :
Current Frequency:2.412 GHz (Channel 1)
修改发射功率
sudo iwconfig wlan1 txpower X
Tx 是发射(Transmits)的简称。无线电波的发射功率是指在给定频段范围内的能量,通常有两种衡量或测量标准:
- 功率(W):相对1 瓦(Watts)的线性水准。例如,WiFi 无线网卡的发射功率通常为0.036W ,或者说36mW。
- 增益(dBm):相对1 毫瓦(milliwatt)的比例水准。例如,WiFi 无线网卡的发射增益为15.56dBm。
- 功率单位mW 和dBm 的换算:
- dBm = 10 * lg[ 功率mW] //即,以10为底数取[功率mW]对数,然再乘以10
- mW = 10^[增益dBm / 10dBm] //即,以10为底数,[增益dBm/10dBm]为指数,计算取值
10 W 的无线发射功率为 40 dBm
6.4 W 的无线发射功率为 38 dBm
3.2 W 的无线发射功率为 35 dBm
1.6 W 的无线发射功率为 32 dBm
1000 mW 的无线发射功率为 30 dBm
800 mW 的无线发射功率为 29 dBm
400 mW 的无线发射功率为 26 dBm
200 mW 的无线发射功率为 23 dBm
100 mW 的无线发射功率为 20 dBm
50 mW 的无线发射功率为 17 dBm
25 mW 的无线发射功率为 14 dBm
12.5 mW 的无线发射功率为 11 dBm
10.34 mW 的无线发射功率为 10 dBm
8.26 mW 的无线发射功率为 9 dBm
6.25 mW 的无线发射功率为 8 dBm
3.125 mW 的无线发射功率为 5 dBm
1 mW 的无线发射功率为 0 dBm
经测试树莓派的发射功率最大可以达到31dBm、linux电脑可以达到20dBm。
树莓派可以通过命令行切换信道,linux电脑无法切换信道。
和学长讨论后不知道是不是伪切换信道需要进行测试。
测试是否为伪切换
让树莓派连接到一个固定信道的路由器进行测试。
若无法切换信道则可以推断,树莓派确实能够切换上行信道
若可以切换信道,且可以上传则证明树莓派伪切换信道
实验
准备一个路由器,将信道调整为固定信道1,树莓派尝试进行连接(修改配置文件)。
测试是否连接到固定信道路由器
iwconfig
wlan0 IEEE 802.11bgn ESSID:"CH1"
Mode:Managed Frequency:2.412 GHz Access Point: C8:3A:35:58:A4:D8
Bit Rate=72.2 Mb/s Tx-Power=31 dBm
Retry short limit:7 RTS thr:off Fragment thr:off
Power Management:on
Link Quality=70/70 Signal level=-38 dBm
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:5 Invalid misc:0 Missed beacon:0
ping 192.168.2.100
PING 192.168.2.100 (192.168.2.100) 56(84) bytes of data.
64 bytes from 192.168.2.100: icmp_seq=1 ttl=64 time=641 ms
64 bytes from 192.168.2.100: icmp_seq=2 ttl=64 time=143 ms
64 bytes from 192.168.2.100: icmp_seq=3 ttl=64 time=64.2 ms
64 bytes from 192.168.2.100: icmp_seq=4 ttl=64 time=84.8 ms
64 bytes from 192.168.2.100: icmp_seq=5 ttl=64 time=214 ms
64 bytes from 192.168.2.100: icmp_seq=6 ttl=64 time=141 ms
结论:以上测试证明树莓派有正确连接到路由器
测试树莓派是否能切换信道
iwlist channel
显示:wlan0 11 channels in total; available frequencies : Channel 01 : 2.412 GHz Channel 02 : 2.417 GHz Channel 03 : 2.422 GHz Channel 04 : 2.427 GHz Channel 05 : 2.432 GHz Channel 06 : 2.437 GHz Channel 07 : 2.442 GHz Channel 08 : 2.447 GHz Channel 09 : 2.452 GHz Channel 10 : 2.457 GHz Channel 11 : 2.462 GHz Current Frequency:2.412 GHz (Channel 1)
2.切换信道
sudo iwconfig wlan0 channel 3
iwconfig
wlan0 IEEE 802.11bgn ESSID:"CH1"
Mode:Managed Frequency:2.422 GHz Access Point: Not-Associated
Tx-Power=31 dBm
Retry short limit:7 RTS thr:off Fragment thr:off
Power Management:on
信道被修改了
尝试进行ping
ping 192.168.2.109
PING 192.168.2.109 (192.168.2.109) 56(84) bytes of data.
64 bytes from 192.168.2.109: icmp_seq=1 ttl=128 time=9.72 ms
64 bytes from 192.168.2.109: icmp_seq=2 ttl=128 time=6.33 ms
64 bytes from 192.168.2.109: icmp_seq=3 ttl=128 time=22.0 ms
64 bytes from 192.168.2.109: icmp_seq=4 ttl=128 time=44.1 ms
64 bytes from 192.168.2.109: icmp_seq=5 ttl=128 time=8.24 ms
64 bytes from 192.168.2.109: icmp_seq=6 ttl=128 time=7.62 ms
传输正常…
结论:树莓派很有可能是伪切换上行信道,但是我没有办法排除固定信道路由器无法接受非该信道信号的这个可能性。
进一步实验
如果修改信道可能xmu201_1的信道会改变。
iwlist scan
Cell 05 - Address: D4:EE:07:49:19:FC
Channel:3
Frequency:2.422 GHz (Channel 3)
Quality=70/70 Signal level=-39 dBm
Encryption key:on
ESSID:"xmu201_1"
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 9 Mb/s
18 Mb/s; 36 Mb/s; 54 Mb/s
Bit Rates:6 Mb/s; 12 Mb/s; 24 Mb/s; 48 Mb/s
Mode:Master
Extra:tsf=0000000000000000
Extra: Last beacon: 27270ms ago
IE: Unknown: 0008786D753230315F31
IE: Unknown: 010882848B961224486C
IE: Unknown: 030103
IE: Unknown: 32040C183060
IE: Unknown: 0706434E20010D14
IE: Unknown: 33082001020304050607
IE: Unknown: 33082105060708090A0B
IE: Unknown: 050400010000
IE: WPA Version 1
Group Cipher : CCMP
Pairwise Ciphers (1) : CCMP
Authentication Suites (1) : PSK
IE: IEEE 802.11i/WPA2 Version 1
Group Cipher : CCMP
Pairwise Ciphers (1) : CCMP
Authentication Suites (1) : PSK
IE: Unknown: 2A0106
IE: Unknown: 2D1AAC0117FFFF000000000000000000000000000000000000000000
IE: Unknown: 3D1603000400000000000000000000000000000000000000
IE: Unknown: DD180050F2020101000003A4000027A4000042435E0062322F00
IE: Unknown: 0B0502000B127A
IE: Unknown: DD07000C4300000000
IE: Unknown: DD06D4EE07010100
进行测试
sudo iwconfig wlan0 channel 3
iwconfig
wlan0 IEEE 802.11bgn ESSID:"CH1"
Mode:Managed Frequency:2.422 GHz Access Point: Not-Associated
Tx-Power=31 dBm
Retry short limit:7 RTS thr:off Fragment thr:off
Power Management:on
重新进行信道扫描
iwlist scan
Cell 05 - Address: D4:EE:07:49:19:FC
Channel:3
Frequency:2.422 GHz (Channel 3)
Quality=70/70 Signal level=-39 dBm
Encryption key:on
ESSID:"xmu201_1"
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 9 Mb/s
18 Mb/s; 36 Mb/s; 54 Mb/s
Bit Rates:6 Mb/s; 12 Mb/s; 24 Mb/s; 48 Mb/s
Mode:Master
Extra:tsf=0000000000000000
Extra: Last beacon: 27270ms ago
IE: Unknown: 0008786D753230315F31
IE: Unknown: 010882848B961224486C
IE: Unknown: 030103
IE: Unknown: 32040C183060
IE: Unknown: 0706434E20010D14
IE: Unknown: 33082001020304050607
IE: Unknown: 33082105060708090A0B
IE: Unknown: 050400010000
IE: WPA Version 1
Group Cipher : CCMP
Pairwise Ciphers (1) : CCMP
Authentication Suites (1) : PSK
IE: IEEE 802.11i/WPA2 Version 1
Group Cipher : CCMP
Pairwise Ciphers (1) : CCMP
Authentication Suites (1) : PSK
IE: Unknown: 2A0106
IE: Unknown: 2D1AAC0117FFFF000000000000000000000000000000000000000000
IE: Unknown: 3D1603000400000000000000000000000000000000000000
IE: Unknown: DD180050F2020101000003A4000027A4000042435E0062322F00
IE: Unknown: 0B0502000B127A
IE: Unknown: DD07000C4300000000
IE: Unknown: DD06D4EE07010100
信道没有发生变化。
结论
树莓派的 sudo iwconfig wlan0 channel 3
极有可能是伪修改:即树莓派修改了某个寄存器的数值,但是并没有实际修改信道,这个可以通过进一步的研究进行验证。
树莓派开启热点功能
研究了一下树莓派的开启热点方法,都有点不科学,最后找到一个比较稳定且合适的方法,需要使用HDMI线和鼠标键盘(因为断掉wifi后树莓派会和电脑断连接、若有插网线则可以无视)具体操作如下:
创建WiFi热点使用的GitHub上一个开源项目:
https://github.com/oblique/create_ap
下载开源项目包
sudo git clone https://github.com/oblique/create_ap
cd create_ap
sudo make install
可能会存在说树莓派连接不上github的可能性(ps:我就遇到了)
- 参考以下步骤:
- 用电脑从github下载zip格式文件
- 通过xftp将zip文件转存入树莓派
- unzip xxx.zip文件
cd create_ap sudo make install
运用该开源项目
No passphrase (open network):
create_ap wlan0 eth0 MyAccessPoint
WPA + WPA2 passphrase:
create_ap wlan0 eth0 MyAccessPoint MyPassPhrase
AP without Internet sharing:
create_ap -n wlan0 MyAccessPoint MyPassPhrase
Bridged Internet sharing:
create_ap -m bridge wlan0 eth0 MyAccessPoint MyPassPhrase
Bridged Internet sharing (pre-configured bridge interface):
create_ap -m bridge wlan0 br0 MyAccessPoint MyPassPhrase
Internet sharing from the same WiFi interface:
create_ap wlan0 wlan0 MyAccessPoint MyPassPhrase
Choose a different WiFi adapter driver
create_ap --driver rtl871xdrv wlan0 eth0 MyAccessPoint MyPassPhrase
No passphrase (open network) using pipe:
echo -e "MyAccessPoint" | create_ap wlan0 eth0
WPA + WPA2 passphrase using pipe:
echo -e "MyAccessPoint\nMyPassPhrase" | create_ap wlan0 eth0
Enable IEEE 802.11n
create_ap --ieee80211n --ht_capab '[HT40+]' wlan0 eth0 MyAccessPoint MyPassPhrase
Client Isolation:
create_ap --isolate-clients wlan0 eth0 MyAccessPoint MyPassPhrase
Systemd service
Using the persistent systemd service
Start service immediately:
systemctl start create_ap
Start on boot:
systemctl enable create_ap
License
FreeBSD
个人热点尝试的操作
根据原开源项目内容设置自己需要的wifi
- 创建一个没有输入网络的热点
sudo create_ap -n wlan0 pi_wifi MyPassWord
- 遇到错误
ERROR: Your adapter can not be a station (i.e. be connected) and an AP at the same time
分析:应该是目前网络正在连接wifi,需要断开,turn off wifi ,重新进行尝试。
- 重新
sudo create_ap -n wlan0 pi_wifi MyPassWord
报错:RTNETLINK answers: Operation not possible due to RF-kill
分析:应该是因为wifi被我之间断开,不能之间关闭只需要断开连接就行了
- test:
这时用rfkill list
查看所有无线设备
0: phy0: Wireless LAN
Soft blocked: yes
Hard blocked: no
发现wlan0软件关闭了。
运行命令rfkill unblock wlan
再查看无线设备
0: phy0: Wireless LAN
Soft blocked: no
Hard blocked: no
wlan0软件开启。
- 再运行上面的
sudo create_ap -n wlan0 pi_wifi MyPassWord
出现:
wlan0: AP-ENABLED
成功开启热点
设置wifi的信道
但是不要忘记了我们的目的,查了很久都没有人修改过热点的信道!!!可能大家不需要该热点的信道,但是作为搞无线网络通信的,我们不能只追求这个,我们要改信道!
思考
方法1:使用命令行修改通信信道
sudo iwconfig wlan0 channel 3
方法2:查看该开源项目的内部文件,找到设置信道的方法
方法3:更换其他方式设置热点的信道
实验
命令行修改信道
sudo iwconfig wlan0 channel 3
Error for wireless request "Set Frequency" (8B04) :
SET failed on device wlan0 ; Operation not supported.
直接报错,无法修改信道。
iwconfig
wlan0 IEEE 802.11bgn Mode:Master Tx-Power=20 dBm
Retry short limit:7 RTS thr:off Fragment thr:off
Power Management:off
lo no wireless extensions.
eth0 no wireless extensions.
这说明什么,命令行在ap状态下是无效的,没办法直接这样修改,该方案不可行。
查看该开源项目的内部文件
这个热点开源项目一共有以下文件
bash_completion create_ap.conf create_ap.service LICENSE README.md
create_ap create_ap.openrc howto Makefile
于是一个一个的找(只花了几分钟而已哈哈)
项目源代码
usage() {
echo "Usage: "$PROGNAME" [options] <wifi-interface> [<interface-with-internet>] [<access-point-name> [<passphrase>]]"
echo
echo "Options:"
echo " -h, --help Show this help"
echo " --version Print version number"
echo " -c <channel> Channel number (default: 1)"
echo " -w <WPA version> Use 1 for WPA, use 2 for WPA2, use 1+2 for both (default: 1+2)"
echo " -n Disable Internet sharing (if you use this, don't pass"
echo " the <interface-with-internet> argument)"
echo " -m <method> Method for Internet sharing."
echo " Use: 'nat' for NAT (default)"
echo " 'bridge' for bridging"
echo " 'none' for no Internet sharing (equivalent to -n)"
echo " --psk Use 64 hex digits pre-shared-key instead of passphrase"
echo " --hidden Make the Access Point hidden (do not broadcast the SSID)"
echo " --mac-filter Enable MAC address filtering"
echo " --mac-filter-accept Location of MAC address filter list (defaults to /etc/hostapd/hostapd.accept)"
echo " --redirect-to-localhost If -n is set, redirect every web request to localhost (useful for public information networks)"
echo " --hostapd-debug <level> With level between 1 and 2, passes arguments -d or -dd to hostapd for debugging."
echo " --isolate-clients Disable communication between clients"
echo " --ieee80211n Enable IEEE 802.11n (HT)"
echo " --ieee80211ac Enable IEEE 802.11ac (VHT)"
echo " --ht_capab <HT> HT capabilities (default: [HT40+])"
echo " --vht_capab <VHT> VHT capabilities"
echo " --country <code> Set two-letter country code for regularity (example: US)"
echo " --freq-band <GHz> Set frequency band. Valid inputs: 2.4, 5 (default: 2.4)"
echo " --driver Choose your WiFi adapter driver (default: nl80211)"
echo " --no-virt Do not create virtual interface"
echo " --no-haveged Do not run 'haveged' automatically when needed"
echo " --fix-unmanaged If NetworkManager shows your interface as unmanaged after you"
echo " close create_ap, then use this option to switch your interface"
echo " back to managed"
echo " --mac <MAC> Set MAC address"
echo " --dhcp-dns <IP1[,IP2]> Set DNS returned by DHCP"
echo " --daemon Run create_ap in the background"
echo " --pidfile <pidfile> Save daemon PID to file"
echo " --logfile <logfile> Save daemon messages to file"
echo " --stop <id> Send stop command to an already running create_ap. For an <id>"
echo " you can put the PID of create_ap or the WiFi interface. You can"
echo " get them with --list-running"
echo " --list-running Show the create_ap processes that are already running"
echo " --list-clients <id> List the clients connected to create_ap instance associated with <id>."
echo " For an <id> you can put the PID of create_ap or the WiFi interface."
echo " If virtual WiFi interface was created, then use that one."
echo " You can get them with --list-running"
echo " --mkconfig <conf_file> Store configs in conf_file"
echo " --config <conf_file> Load configs from conf_file"
echo
echo "Non-Bridging Options:"
echo " --no-dns Disable dnsmasq DNS server"
echo " --no-dnsmasq Disable dnsmasq server completely"
echo " -g <gateway> IPv4 Gateway for the Access Point (default: 192.168.12.1)"
echo " -d DNS server will take into account /etc/hosts"
echo " -e <hosts_file> DNS server will take into account additional hosts file"
echo
echo "Useful informations:"
echo " * If you're not using the --no-virt option, then you can create an AP with the same"
echo " interface you are getting your Internet connection."
echo " * You can pass your SSID and password through pipe or through arguments (see examples)."
echo " * On bridge method if the <interface-with-internet> is not a bridge interface, then"
echo " a bridge interface is created automatically."
echo
echo "Examples:"
echo " "$PROGNAME" wlan0 eth0 MyAccessPoint MyPassPhrase"
echo " echo -e 'MyAccessPoint\nMyPassPhrase' | "$PROGNAME" wlan0 eth0"
echo " "$PROGNAME" wlan0 eth0 MyAccessPoint"
echo " echo 'MyAccessPoint' | "$PROGNAME" wlan0 eth0"
echo " "$PROGNAME" wlan0 wlan0 MyAccessPoint MyPassPhrase"
echo " "$PROGNAME" -n wlan0 MyAccessPoint MyPassPhrase"
echo " "$PROGNAME" -m bridge wlan0 eth0 MyAccessPoint MyPassPhrase"
echo " "$PROGNAME" -m bridge wlan0 br0 MyAccessPoint MyPassPhrase"
echo " "$PROGNAME" --driver rtl871xdrv wlan0 eth0 MyAccessPoint MyPassPhrase"
echo " "$PROGNAME" --daemon wlan0 eth0 MyAccessPoint MyPassPhrase"
echo " "$PROGNAME" --stop wlan0"
}
这一行明显及其关键,一看就是可以利用的代码。
其中:
echo " -c <channel> Channel number (default: 1)"
应该可以通过sudo create_ap -n wlan0 pi_wifi MyPassWord -c 2
修改信道数值。
WARN: brmfmac driver doesn't work properly with virtual interfaces and
it can cause kernel panic. For this reason we disallow virtual
interfaces for your adapter.
For more info: https://github.com/oblique/create_ap/issues/203
WARN: Your adapter does not fully support AP virtual interface, enabling --no-virt
Config dir: /tmp/create_ap.wlan0.conf.d6khWCUL
PID: 4794
wlan0应该是被占用了
修改信道
sudo create_ap --s wlan0
通过sudo create_ap -n wlan0 pi_wifi MyPassWord -c 1
修改信道数值。
通过其他设备进行测试’iwlist scan’
扫描信道结果:
wlan0 Scan completed :
Cell 01 - Address: 18:64:72:92:EC:81
Channel:1
Frequency:2.412 GHz (Channel 1)
Quality=46/70 Signal level=-64 dBm
Encryption key:on
ESSID:"pi_wifi"
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 6 Mb/s; 9 Mb/s
11 Mb/s; 12 Mb/s; 18 Mb/s
Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
Mode:Master
Extra:tsf=0000000000000000
Extra: Last beacon: 200ms ago
IE: Unknown: 0007656475726F616D
IE: Unknown: 010882840B0C12161824
IE: Unknown: 030101
IE: Unknown: 2A0102
IE: Unknown: 32043048606C
IE: IEEE 802.11i/WPA2 Version 1
Group Cipher : CCMP
Pairwise Ciphers (1) : CCMP
Authentication Suites (1) : 802.1x
IE: Unknown: 2D1AED111BFFFFFF0000000000000000000000000000000 000000000
IE: Unknown: 3D1601000A0000000000000000000000000000000000000 0
IE: Unknown: 7F080000080000000000
IE: Unknown: DD180050F2020101800003A4000027A4000042435E00623 22F00
通过
sudo create_ap -n wlan0 pi_wifi MyPassWord -c 11
修改信道数值。通过其他设备进行测试’iwlist scan’
扫描信道结果:
Cell 12 - Address: 18:64:72:92:EC:81
Channel:11
Frequency:2.462 GHz (Channel 11)
Quality=66/70 Signal level=-44 dBm
Encryption key:off
ESSID:"pi_wifi"
Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
9 Mb/s; 12 Mb/s; 18 Mb/s
Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
Mode:Master
Extra:tsf=0000000000000000
Extra: Last beacon: 17620ms ago
IE: Unknown: 0003434832
IE: Unknown: 010882848B960C121824
IE: Unknown: 03010B
IE: Unknown: 2A0100
IE: Unknown: 32043048606C
IE: Unknown: 2D1A2C0003FF00000000000000000000000000000000000000000000
IE: Unknown: 3D160B001100000000000000000000000000000000000000
IE: Unknown: DD180050F2020101000003A4000027A4000042435E0062322F00
IE: Unknown: DD05000AEB0100
IE: Unknown: DD8E0050F204104A0001101044000102103B0001031047001000000000000010000000FCD7330F3A461021000754502D4C494E4B10230009544C2D57523734324E10240003372E3010420007312E312E312E331054000800060050F204000110110018576972656C657373204E20526F757465722057523734324E100800022288103C0001011049000600372A000120
成功修改信道!
总结
综上所示:
- 可以通过命令行修改功率
sudo iwconfig wlan1 txpower X
- 通过开源项目切换信道
sudo create_ap --s wlan0 sudo create_ap -n wlan0 pi_wifi MyPassWord -c 11
生成ssh
ssh-keygen -t rsa -C "kp@mmrsl.cn"
ssh-agent -s
ssh-add ~/.ssh/id_rsa
扩展:功率修改测试及方案
- 开启热点
sudo create_ap --s wlan0 sudo create_ap -n wlan0 pi_wifi MyPassWord -c 11
- 修改功率
sudo iwconfig wlan1 txpower 30
- 检测功率