期望脚本 - 预期 2 个条件并从蓝牙 ctl 中提取 MAC 添加



我正在开发带有蓝牙的覆盆子派零w。目前,我可以使用蓝牙ctl手动执行我想做的事情,但是我正在尝试将其自动化,以便rpi可以一次单独执行此任务数周。

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

让我声明我在这方面的知识非常有限,但正在努力实现它。如果您对如何进行有任何想法,那可能会更容易,我全都听见了。目前,我一直在开发一个期望脚本来完成这项工作。到目前为止,我有这个:

#!/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 $btnamen"
send_user "nEnter Log Name: "
expect_user -re "(.*)n"
set logname $expect_out(1,string)
send_user "You entered $lognamen"
log_file -a $logname
sleep 2
spawn sudo bluetoothctl
sleep 2
expect -re $prompt
send "system-alias $btnamer"
sleep 2
expect -re $prompt
send "scan onr"
sleep 8
expect -re $prompt
send "discoverable onr"
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 的正则表达式和字符串"配对:是"并将该 MAC 从缓冲区中拉出。我可以期望"期望(正则表达式&&字符串"配对:是"){do}"吗?我对如何执行此操作并拉动实际 MAC 以将其保存到变量并在下一个信息命令中使用它有点迷茫。我猜这是在操纵expect_out(缓冲区),但这远远超出了我的技能。

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

现在,可以使用gets更轻松地完成该部分。但是当从蓝牙ctl接收数据时,使用expect_out(#,string)挑选出相关部分效果很好。

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

#!/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 $btnamen"
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 $btnamer"
expect -ex "Changing $btname succeeded"
expect -ex $prompt
send "power onr"
expect -ex "Changing power on succeeded"
expect -ex $prompt
send "scan onr"
expect -ex $prompt
send "discoverable onr"
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 onr"
exp_continue
}
timeout {
exit 1
}
}
expect -ex $prompt
# Device info is now available in $expect_out(buffer)

最新更新