在本机bash中读取/解析BTMON的流式输出



我正在尝试实时解析btmon的流输出。lescan进程正在后台运行:sudo hcitool lescan &

经过大量测试,我无话可说,因为下面的脚本,在第一个会话中由root执行时:

while read -r line; do
echo "$line"
done < <(btmon 2>&1)

不打印与相同的行

sudo btmon 2>&1

从单独的shell会话执行时。从第一行开始打印的行数较少,输出有时会滞后几分钟。

发生了什么事?有什么意见或建议吗?


EDIT:示例输出。所有三个会话都是同时进行的,预期结果是相同的输出。所有这些都同时被叫停。

sudo hcitool lescan

LE Scan ...
49:D3:E5:61:AE:36 (unknown)
49:D3:E5:61:AE:36 (unknown)
28:39:5E:49:DB:23 (unknown)
28:39:5E:49:DB:23 [TV] Samsung 9 Series (65)
5D:C2:ED:93:59:1F (unknown)
6A:46:64:F2:25:57 (unknown)
6A:46:64:F2:25:57 (unknown)
78:D0:8A:A5:8B:43 (unknown)
78:D0:8A:A5:8B:43 (unknown)
33:CA:22:86:F5:4D (unknown)
C8:69:CD:6A:89:2A (unknown)
6A:4E:F6:F6:BB:8C (unknown)
6A:4E:F6:F6:BB:8C (unknown)
C8:69:CD:6A:89:2A (unknown)
5D:C2:ED:93:59:1F (unknown)
7C:D1:C3:DA:91:14 (unknown)
7C:D1:C3:DA:91:14 (unknown)
6D:D2:11:18:37:F1 (unknown)
6D:D2:11:18:37:F1 (unknown)
F8:77:B8:8A:01:C1 (unknown)
FF:FF:C0:22:6B:36 (unknown)

sudo btmon 2>&1

Bluetooth monitor ver 5.43
= Note: Linux version 4.14.71+ (armv6l)                                                                      0.563610
= Note: Bluetooth subsystem version 2.22                                                                     0.563625
= New Index: B8:27:EB:C8:9E:A6 (Primary,UART,hci0)                                                    [hci0] 0.563632
= Open Index: B8:27:EB:C8:9E:A6                                                                       [hci0] 0.563634
= Index Info: B8:27:EB:C8:9E:A6 (Broadcom Corporation)                                                [hci0] 0.563638
@ MGMT Open: bluetoothd (privileged) version 1.14                                                   {0x0001} 0.563644
@ MGMT Open: btmon (privileged) version 1.14                                                        {0x0002} 0.563788
@ MGMT Open: btmon (privileged) version 1.14                                                        {0x0003} 2.149663
@ RAW Open: hcitool (privileged) version 2.22                                                      {0x0004} 11.269034
@ RAW Close: hcitool                                                                               {0x0004} 11.270311
@ RAW Open: hcitool (privileged) version 2.22                                               {0x0004} [hci0] 11.271277
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7                                           [hci0] 11.272202
Type: Active (0x01)
Interval: 10.000 msec (0x0010)
Window: 10.000 msec (0x0010)
Own address type: Public (0x00)
Filter policy: Accept all advertisement (0x00)
> HCI Event: Command Complete (0x0e) plen 4                                                          [hci0] 11.272708
LE Set Scan Parameters (0x08|0x000b) ncmd 1
Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2                                               [hci0] 11.275396
Scanning: Enabled (0x01)
Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4                                                          [hci0] 11.276126
LE Set Scan Enable (0x08|0x000c) ncmd 1
Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 35                                                            [hci0] 11.287680
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 49:D3:E5:61:AE:36 (Resolvable)
Data length: 23
Flags: 0x06
LE General Discoverable Mode
BR/EDR Not Supported
Company: Apple, Inc. (76)
Type: Unknown (12)
Data: 0063169caf541000a9cd5d9975f7
RSSI: -86 dBm (0xaa)
> HCI Event: LE Meta Event (0x3e) plen 12                                                            [hci0] 11.288789
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 49:D3:E5:61:AE:36 (Resolvable)
Data length: 0
RSSI: -85 dBm (0xab)
> HCI Event: LE Meta Event (0x3e) plen 43                                                            [hci0] 11.301135
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: 28:39:5E:49:DB:23 (OUI 28-39-5E)
Data length: 31
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Samsung Electronics Co. Ltd. (117)
Data: 420401017e28395e49db232a395e49db2201000000000000
RSSI: -71 dBm (0xb9)
> HCI Event: LE Meta Event (0x3e) plen 40                                                            [hci0] 11.305925
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Public (0x00)
Address: 28:39:5E:49:DB:23 (OUI 28-39-5E)
Data length: 28
Name (short): [TV] Samsung 9 Series (65)
RSSI: -72 dBm (0xb8)
> HCI Event: LE Meta Event (0x3e) plen 26                                                            [hci0] 11.311464
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 5D:C2:ED:93:59:1F (Resolvable)
Data length: 14
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 071c6824b9
RSSI: -92 dBm (0xa4)
> HCI Event: LE Meta Event (0x3e) plen 34                                                            [hci0] 11.329588
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 6A:46:64:F2:25:57 (Resolvable)
Data length: 22
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (14)
Data: 01004f000702
Type: Unknown (16)
Data: 0a1cca57b2
RSSI: -77 dBm (0xb3)
> HCI Event: LE Meta Event (0x3e) plen 12                                                            [hci0] 11.332246
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 6A:46:64:F2:25:57 (Resolvable)
Data length: 0
RSSI: -77 dBm (0xb3)
> HCI Event: LE Meta Event (0x3e) plen 26                                                            [hci0] 11.373918
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 78:D0:8A:A5:8B:43 (Resolvable)
Data length: 14
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 031c429af9
RSSI: -88 dBm (0xa8)
> HCI Event: LE Meta Event (0x3e) plen 12                                                            [hci0] 11.376067
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 78:D0:8A:A5:8B:43 (Resolvable)
Data length: 0
RSSI: -87 dBm (0xa9)
> HCI Event: LE Meta Event (0x3e) plen 27                                                            [hci0] 11.385151
LE Advertising Report (0x02)
Num reports: 1
Event type: Non connectable undirected - ADV_NONCONN_IND (0x03)
Address type: Random (0x01)
Address: 33:CA:22:86:F5:4D (Non-Resolvable)
Data length: 15
Flags: 0x1b
LE Limited Discoverable Mode
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Apple TV (9)
Data: 032bc0a8012b
RSSI: -68 dBm (0xbc)
> HCI Event: LE Meta Event (0x3e) plen 23                                                            [hci0] 11.400609
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: C8:69:CD:6A:89:2A (Apple, Inc.)
Data length: 11
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 0104
RSSI: -80 dBm (0xb0)
> HCI Event: LE Meta Event (0x3e) plen 32                                                            [hci0] 11.486780
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 6A:4E:F6:F6:BB:8C (Resolvable)
Data length: 20
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (13)
Data: 53d559a3
Type: Unknown (16)
Data: 031cf2b2a6
RSSI: -85 dBm (0xab)
> HCI Event: LE Meta Event (0x3e) plen 12                                                            [hci0] 11.489229
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 6A:4E:F6:F6:BB:8C (Resolvable)
Data length: 0
RSSI: -87 dBm (0xa9)
> HCI Event: LE Meta Event (0x3e) plen 12                                                            [hci0] 11.585662
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Public (0x00)
Address: C8:69:CD:6A:89:2A (Apple, Inc.)
Data length: 0
RSSI: -82 dBm (0xae)
> HCI Event: LE Meta Event (0x3e) plen 12                                                            [hci0] 11.855660
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 5D:C2:ED:93:59:1F (Resolvable)
Data length: 0
RSSI: -90 dBm (0xa6)
> HCI Event: LE Meta Event (0x3e) plen 23                                                            [hci0] 11.999393
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: 7C:D1:C3:DA:91:14 (Apple, Inc.)
Data length: 11
Flags: 0x06
LE General Discoverable Mode
BR/EDR Not Supported
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 0b08
RSSI: -97 dBm (0x9f)
> HCI Event: LE Meta Event (0x3e) plen 12                                                            [hci0] 12.191914
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Public (0x00)
Address: 7C:D1:C3:DA:91:14 (Apple, Inc.)
Data length: 0
RSSI: -87 dBm (0xa9)
> HCI Event: LE Meta Event (0x3e) plen 26                                                            [hci0] 12.772156
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 6D:D2:11:18:37:F1 (Resolvable)
Data length: 14
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 0198b24d86
RSSI: -80 dBm (0xb0)
> HCI Event: LE Meta Event (0x3e) plen 12                                                            [hci0] 12.773961
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 6D:D2:11:18:37:F1 (Resolvable)
Data length: 0
RSSI: -80 dBm (0xb0)
> HCI Event: LE Meta Event (0x3e) plen 40                                                            [hci0] 13.347879
LE Advertising Report (0x02)
Num reports: 1
Event type: Non connectable undirected - ADV_NONCONN_IND (0x03)
Address type: Public (0x00)
Address: F8:77:B8:8A:01:C1 (Samsung Electronics Co.,Ltd)
Data length: 28
Company: Samsung Electronics Co. Ltd. (117)
Data: 4204018060f877b88a01c1fa77b88a01c001000000000000
RSSI: -100 dBm (0x9c)
> HCI Event: LE Meta Event (0x3e) plen 26                                                            [hci0] 14.016707
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: FF:FF:C0:22:6B:36 (OUI FF-FF-C0)
Data length: 14
Flags: 0x05
LE Limited Discoverable Mode
BR/EDR Not Supported
TX power: 0 dBm
Appearance: Keyboard (0x03c1)
16-bit Service UUIDs (partial): 1 entry
Unknown (0xffe0)
RSSI: -90 dBm (0xa6)

最后,脚本:

Bluetooth monitor ver 5.43
= Note: Linux version 4.14.71+ (armv6l)                                0.149535
= Note: Bluetooth subsystem version 2.22                               0.149551
= New Index: B8:27:EB:C8:9E:A6 (Primary,UART,hci0)              [hci0] 0.149556
= Open Index: B8:27:EB:C8:9E:A6                                 [hci0] 0.149559
= Index Info: B8:27:EB:C8:9E:A6 (Broadcom Corporation)          [hci0] 0.149563
@ MGMT Open: btmon (privileged) version 1.14                  {0x0002} 0.149569
@ MGMT Open: bluetoothd (privileged) version 1.14             {0x0001} 0.149573
@ MGMT Open: btmon (privileged) version 1.14                  {0x0003} 0.149663
@ RAW Open: hcitool (privileged) version 2.22                 {0x0004} 9.269034
@ RAW Close: hcitool                                          {0x0004} 9.270311
@ RAW Open: hcitool (privileged) version 2.22          {0x0004} [hci0] 9.271277
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7      [hci0] 9.272202
Type: Active (0x01)
Interval: 10.000 msec (0x0010)
Window: 10.000 msec (0x0010)
Own address type: Public (0x00)
Filter policy: Accept all advertisement (0x00)
> HCI Event: Command Complete (0x0e) plen 4                     [hci0] 9.272708
LE Set Scan Parameters (0x08|0x000b) ncmd 1
Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2          [hci0] 9.275396
Scanning: Enabled (0x01)
Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4                     [hci0] 9.276126
LE Set Scan Enable (0x08|0x000c) ncmd 1
Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 35                       [hci0] 9.287680
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 49:D3:E5:61:AE:36 (Resolvable)
Data length: 23
Flags: 0x06
LE General Discoverable Mode
BR/EDR Not Supported
Company: Apple, Inc. (76)
Type: Unknown (12)
Data: 0063169caf541000a9cd5d9975f7
RSSI: -86 dBm (0xaa)
> HCI Event: LE Meta Event (0x3e) plen 12                       [hci0] 9.288789
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 49:D3:E5:61:AE:36 (Resolvable)
Data length: 0
RSSI: -85 dBm (0xab)
> HCI Event: LE Meta Event (0x3e) plen 43                       [hci0] 9.301135
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: 28:39:5E:49:DB:23 (OUI 28-39-5E)
Data length: 31
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Samsung Electronics Co. Ltd. (117)
Data: 420401017e28395e49db232a395e49db2201000000000000
RSSI: -71 dBm (0xb9)
> HCI Event: LE Meta Event (0x3e) plen 40                       [hci0] 9.305925
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Public (0x00)
Address: 28:39:5E:49:DB:23 (OUI 28-39-5E)
Data length: 28
Name (short): [TV] Samsung 9 Series (65)
RSSI: -72 dBm (0xb8)
> HCI Event: LE Meta Event (0x3e) plen 26                       [hci0] 9.311464
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 5D:C2:ED:93:59:1F (Resolvable)
Data length: 14
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 071c6824b9
RSSI: -92 dBm (0xa4)
> HCI Event: LE Meta Event (0x3e) plen 34                       [hci0] 9.329588
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 6A:46:64:F2:25:57 (Resolvable)
Data length: 22
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (14)
Data: 01004f000702
Type: Unknown (16)
Data: 0a1cca57b2
RSSI: -77 dBm (0xb3)
> HCI Event: LE Meta Event (0x3e) plen 12                       [hci0] 9.332246
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 6A:46:64:F2:25:57 (Resolvable)
Data length: 0
RSSI: -77 dBm (0xb3)
> HCI Event: LE Meta Event (0x3e) plen 26                       [hci0] 9.373918
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 78:D0:8A:A5:8B:43 (Resolvable)
Data length: 14
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 031c429af9
RSSI: -88 dBm (0xa8)
> HCI Event: LE Meta Event (0x3e) plen 12                       [hci0] 9.376067
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 78:D0:8A:A5:8B:43 (Resolvable)
Data length: 0
RSSI: -87 dBm (0xa9)
> HCI Event: LE Meta Event (0x3e) plen 27                       [hci0] 9.385151
LE Advertising Report (0x02)
Num reports: 1
Event type: Non connectable undirected - ADV_NONCONN_IND (0x03)
Address type: Random (0x01)
Address: 33:CA:22:86:F5:4D (Non-Resolvable)
Data length: 15
Flags: 0x1b
LE Limited Discoverable Mode
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Apple TV (9)
Data: 032bc0a8012b
RSSI: -68 dBm (0xbc)
> HCI Event: LE Meta Event (0x3e) plen 23                       [hci0] 9.400609
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: C8:69:CD:6A:89:2A (Apple, Inc.)
Data length: 11
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 0104
RSSI: -80 dBm (0xb0)
> HCI Event: LE Meta Event (0x3e) plen 32                       [hci0] 9.486780
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 6A:4E:F6:F6:BB:8C (Resolvable)
Data length: 20
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (13)
Data: 53d559a3
Type: Unknown (16)
Data: 031cf2b2a6
RSSI: -85 dBm (0xab)
> HCI Event: LE Meta Event (0x3e) plen 12                       [hci0] 9.489229
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 6A:4E:F6:F6:BB:8C (Resolvable)
Data length: 0
RSSI: -87 dBm (0xa9)

如图所示,脚本明显滞后。如果我等到hcitool稳定下来,脚本就无法跟上。仅输出。。。停止。

我很困惑。

这里似乎有三个独立的问题:

  • stdout默认情况下是缓冲的——也就是说,内容由标准C库缓冲,并且只有在写入非TTY目的地时才以块形式刷新。BashFAQ#9对此进行了详细讨论
  • 因为在运行read之前没有清除IFS,所以会丢弃前导和尾随空白。BashFAQ#1中讨论了使用while read的最佳实践
  • 因为您使用的是echo,所以带有反斜杠的内容的行为是未定义的,并且会根据活动(编译时和运行时(shell配置而变化。(例如,当posixxpg_echo标志都被启用时,echo 't'将发出一个文本选项卡;如果没有它们,它将发出两个字符串t;这两个都是POSIX规范允许的(。参见上述规范http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html,特别是应用程序使用部分

为了看起来也是TTY而不是btmon的管道,下面还使用了unbuffer(作为expect的一部分提供(,而不是开销较低的GNU核心实用工具stdbuf -o0:

考虑一下:

while IFS= read -r line; do
printf '%sn' "$line"
done < <(unbuffer btmon 2>&1)

相关内容

  • 没有找到相关文章

最新更新