gpt4 book ai didi

linux - 期望脚本 - 期望 2 个条件并从 bluetoothctl 中提取 MAC 添加

转载 作者:太空宇宙 更新时间:2023-11-04 11:53:27 25 4
gpt4 key购买 nike

我正在使用带有蓝牙的树莓派零 w。目前,我可以使用 bluetoothctl 手动执行我想执行的操作,但我正在尝试自动执行此操作,以便 rpi 可以单独执行此任务数周。

目标是让 rpi 可以在蓝牙中被发现,并让某人无需密码即可自动连接到它。建立连接后,我想将信息命令发送到配对的 mac 地址以检索有关设备的更多信息。最终我想添加能够将文本文件发送到设备(笔记本电脑、Android 手机等)的功能,但这可能会在以后出现。最后,我想将信息(带有时间戳的 mac 和信息结果)记录在一个单独的文件中。

让我声明一下,我对此知之甚少,但正在努力实现它。如果您对如何进行有任何想法,那可能会更容易,我洗耳恭听。目前我一直在开发一个 expect 脚本来完成这项工作。到目前为止我有这个:

#!/usr/bin/expect -f

set prompt "#"
log_user 1
set timeout -1
send_user "Please enter bluetooth signal name: "
expect_user -re "(.*)\n"
set btname $expect_out(1,string)
send_user "BT signal will be $btname\n"

send_user "\nEnter Log Name: "
expect_user -re "(.*)\n"
set logname $expect_out(1,string)
send_user "You entered $logname\n"
log_file -a $logname

sleep 2

spawn sudo bluetoothctl
sleep 2
expect -re $prompt
send "system-alias $btname\r"
sleep 2
expect -re $prompt
send "scan on\r"
sleep 8
expect -re $prompt
send "discoverable on\r"
sleep 2
expect

所以我的问题是,当设备配对时,以下是信号:

[bluetooth]# discoverable on
Changing discoverable on succeeded
[CHG] Controller B8:27:EB:03:68:F1 Discoverable: yes
[NEW] Device F8:F1:B6:E2:BB:06 DROID RAZR M
[CHG] Device F8:F1:B6:E2:BB:06 Modalias: bluetooth:v0008pB02Dd0000
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 00001103-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 00001105-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 00001106-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 0000110a-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 0000110c-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 0000110e-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 00001112-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 00001116-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 0000111f-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 0000112d-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 0000112f-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 00001132-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 00001200-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 00001800-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 00001801-0000-1000-8000-00805f9b34fb
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 453994d5-d58b-96f9-6616-b37f586ba2ec
[CHG] Device F8:F1:B6:E2:BB:06 UUIDs: 936da01f-9abd-4d9d-80c7-02af85c822a8
[CHG] Device F8:F1:B6:E2:BB:06 ServicesResolved: yes
[CHG] Device F8:F1:B6:E2:BB:06 Paired: yes
[CHG] Device F8:F1:B6:E2:BB:06 ServicesResolved: no
[CHG] Device F8:F1:B6:E2:BB:06 Connected: no
[bluetooth]# info F8:F1:B6:E2:BB:06
Device F8:F1:B6:E2:BB:06
Name: DROID RAZR M
Alias: DROID RAZR M
Class: 0x5a020c
Icon: phone
Paired: yes
Trusted: no
Blocked: no
Connected: no
LegacyPairing: no
UUID: Dialup Networking (00001103-0000-1000-8000-00805f9b34fb)
UUID: OBEX Object Push (00001105-0000-1000-8000-00805f9b34fb)
UUID: OBEX File Transfer (00001106-0000-1000-8000-00805f9b34fb)
UUID: Audio Source (0000110a-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control (0000110e-0000-1000-8000-00805f9b34fb)
UUID: Headset AG (00001112-0000-1000-8000-00805f9b34fb)
UUID: NAP (00001116-0000-1000-8000-00805f9b34fb)
UUID: Handsfree Audio Gateway (0000111f-0000-1000-8000-00805f9b34fb)
UUID: SIM Access (0000112d-0000-1000-8000-00805f9b34fb)
UUID: Phonebook Access Server (0000112f-0000-1000-8000-00805f9b34fb)
UUID: Message Access Server (00001132-0000-1000-8000-00805f9b34fb)
UUID: PnP Information (00001200-0000-1000-8000-00805f9b34fb)
UUID: Generic Access Profile (00001800-0000-1000-8000-00805f9b34fb)
UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
UUID: Vendor specific (453994d5-d58b-96f9-6616-b37f586ba2ec)
UUID: Vendor specific (936da01f-9abd-4d9d-80c7-02af85c822a8)
Modalias: bluetooth:v0008pB02Dd0000

所以我的问题是 - 我如何才能创建一个“预期”来检查有效 MAC 的正则表达式和字符串“Paired: yes”并将该 MAC 从缓冲区中拉出。我可以期待“expect ( regex && string "Paired: yes") { do}”吗?我对如何执行此操作以及将实际 MAC 保存到变量并在下一个信息命令中使用它有点迷茫。我猜它正在操纵 expect_out(buffer) 但这超出了我的技能范围。

最佳答案

你问如何从期望匹配的数据中提取一些信息。然而,您在脚本的第一部分正是这样做的,您从用户那里获得了输入。

现在可以使用 gets 更简单地完成该部分。但是当从 bluetoothctl 接收数据时,使用 expect_out(#,string) 来挑选相关部分效果很好。

此外,如果您只是期望正确的事情,那么没有理由执行所有这些 sleep 命令。

#!/usr/bin/expect -f

# Use command-line arguments if provided
lassign $argv btname logname

# Prompt for any missing arguments
if {$argc < 1} {
send_user "Please enter bluetooth signal name: "
gets stdin btname
}
send_user "BT signal will be $btname\n"

if {$argc < 2} {
send_user "\nEnter Log Name: "
gets stdin logname
}
log_file -a $logname

set prompt "#"
spawn bluetoothctl

expect -ex $prompt
send "system-alias $btname\r"
expect -ex "Changing $btname succeeded"

expect -ex $prompt
send "power on\r"
expect -ex "Changing power on succeeded"

expect -ex $prompt
send "scan on\r"

expect -ex $prompt
send "discoverable on\r"

expect {
-timeout 120
-re {Device ([0-9A-F:]+) Paired: yes} {
# Get info on newly paired device
expect -ex $prompt
send "info $expect_out(1,string)\r"
}
-re {Controller ([0-9A-F:]+) Discoverable: no} {
# Re-arm discoverability
send "discoverable on\r"
exp_continue
}
timeout {
exit 1
}
}

expect -ex $prompt
# Device info is now available in $expect_out(buffer)

关于linux - 期望脚本 - 期望 2 个条件并从 bluetoothctl 中提取 MAC 添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55290444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com