解析 tcl/expect 中的多行
Parse multiple lines in tcl/expect
我正在做一个执行 bluetoothctl 的 expect 脚本来读取周围的一些 ble 设备。
为此我编写了下一个脚本:
#!/usr/bin/expect
send_user "\nStarting scanning\n\r"
spawn bluetoothctl
expect -re "#"
expect "Agent registered"
send_user "\nConfiguring scan filter\n\r"
send "menu scan\r"
expect -re "#"
send "uuids 0x1802\r"
expect -re "#"
send "back\r"
expect -re "#"
send_user "\nScanning devices\n\r"
send "scan on\r"
sleep 10
send "devices\r"
expect -re {Device ([0-9A-F:]+)*} {
expect -re "#"
set devices $expect_out(1,string)
}
puts "Devices = $devices"
foreach device $devices {
send "info $device\r"
expect -re {UUID: Vendor specific ([0-9A-F-]+)*} {
expect -re "#"
set uuid $expect_out(1,string)
}
puts "UUID obtained: $uuid"
}
当我执行它时我有 2 个错误:
错误 1 - 多行 returned。如果命令 send "devices\r" returns 2 行或更多行,我只能阅读第一个 returned 但我必须保存所有内容:
[bluetooth]# devices
Device 4F:BF:9A:F6:77:32 4F-BF-9A-F6-77-32
Device 04:D1:3A:D7:71:5E Redmi
[bluetooth]# Devices = 4F:BF:9A:F6:77:32
错误 2 - 在 foreach 中(对于获得的每个设备)我需要解析 de info 命令输出以获取“UUID:供应商特定”行的 UUID:
[bluetooth]# info 4F:BF:9A:F6:77:32
Device 4F:BF:9A:F6:77:32 (random)
Alias: 4F-BF-9A-F6-77-32
Paired: no
Trusted: no
Blocked: no
Connected: no
LegacyPairing: no
UUID: Immediate Alert (00001802-0000-1000-8000-00805f9b34fb)
UUID: Vendor specific (0a4dadab-66d6-42b7-8d01-bc43b618b6aa)
我只需要获取下一个字符串“0a4dadab-66d6-42b7-8d01-bc43b618b6aa”但我无法找到解决方案...
我必须将所有 UUID(对于在扫描中获得的所有设备)保存到一个数组中,然后 return 将它们保存到父脚本中。
有人可以帮我解决这个问题吗?
谢谢!
我不认为 bluetoothctl
旨在以这种方式使用。 BlueZ 开发人员一直在更改 bluetoothctl,如果您让它正常工作,它会破坏您的脚本。
更好的方法是使用 BlueZ 提供的 D-BusAPI
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txt
要执行此操作,您需要知道:
- BlueZ 服务是 'org.bluez'
- 适配器设备的 D-Bus 对象路径通常为“/org/bluez/hci0”
- 适配器的 DBus 接口是 'org.bluez.Adapter1'
我正在做一个执行 bluetoothctl 的 expect 脚本来读取周围的一些 ble 设备。
为此我编写了下一个脚本:
#!/usr/bin/expect
send_user "\nStarting scanning\n\r"
spawn bluetoothctl
expect -re "#"
expect "Agent registered"
send_user "\nConfiguring scan filter\n\r"
send "menu scan\r"
expect -re "#"
send "uuids 0x1802\r"
expect -re "#"
send "back\r"
expect -re "#"
send_user "\nScanning devices\n\r"
send "scan on\r"
sleep 10
send "devices\r"
expect -re {Device ([0-9A-F:]+)*} {
expect -re "#"
set devices $expect_out(1,string)
}
puts "Devices = $devices"
foreach device $devices {
send "info $device\r"
expect -re {UUID: Vendor specific ([0-9A-F-]+)*} {
expect -re "#"
set uuid $expect_out(1,string)
}
puts "UUID obtained: $uuid"
}
当我执行它时我有 2 个错误:
错误 1 - 多行 returned。如果命令 send "devices\r" returns 2 行或更多行,我只能阅读第一个 returned 但我必须保存所有内容:
[bluetooth]# devices
Device 4F:BF:9A:F6:77:32 4F-BF-9A-F6-77-32
Device 04:D1:3A:D7:71:5E Redmi
[bluetooth]# Devices = 4F:BF:9A:F6:77:32
错误 2 - 在 foreach 中(对于获得的每个设备)我需要解析 de info 命令输出以获取“UUID:供应商特定”行的 UUID:
[bluetooth]# info 4F:BF:9A:F6:77:32
Device 4F:BF:9A:F6:77:32 (random)
Alias: 4F-BF-9A-F6-77-32
Paired: no
Trusted: no
Blocked: no
Connected: no
LegacyPairing: no
UUID: Immediate Alert (00001802-0000-1000-8000-00805f9b34fb)
UUID: Vendor specific (0a4dadab-66d6-42b7-8d01-bc43b618b6aa)
我只需要获取下一个字符串“0a4dadab-66d6-42b7-8d01-bc43b618b6aa”但我无法找到解决方案...
我必须将所有 UUID(对于在扫描中获得的所有设备)保存到一个数组中,然后 return 将它们保存到父脚本中。
有人可以帮我解决这个问题吗?
谢谢!
我不认为 bluetoothctl
旨在以这种方式使用。 BlueZ 开发人员一直在更改 bluetoothctl,如果您让它正常工作,它会破坏您的脚本。
更好的方法是使用 BlueZ 提供的 D-BusAPI
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/device-api.txt
要执行此操作,您需要知道:
- BlueZ 服务是 'org.bluez'
- 适配器设备的 D-Bus 对象路径通常为“/org/bluez/hci0”
- 适配器的 DBus 接口是 'org.bluez.Adapter1'