Last active
April 11, 2020 23:46
-
-
Save simlei/602422d9edb17f06d128f24fac144e3a to your computer and use it in GitHub Desktop.
copy bluetooth pairing keys from windows for dual boot -- bash script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
#credits \& original idea: https://unix.stackexchange.com/a/255510 | |
if [[ "$1" == "--help" ]]; then | |
echo usage: $0 "<WindowsPartition>/Windows/System32/config" | |
echo if youre already in that directory, you will still have to provide that directory. However, you may then just use \$PWD in its stead. | |
echo this script will read out all your paired bluetooth device keys from the windows registry and show them to you. optionally, it will drop you into VIM at the approximate location at where you will want to edit stuff. This saves you the hassle of browsing the registry by hand with the chntpw command. However, it may be a good idea to read the original stackexchange post \(see below\). This script is written for ubuntu but may work for other distributions. Good luck! | |
echo | |
echo Pain points: | |
echo - Pair all desired devices, first on linux, then on Windows. | |
echo - Have chntpw via your package manager | |
echo - mount your windows drive and supply it as first argument as seen above | |
echo - turn off all your bluetooth devices! | |
echo --- then: run this script. | |
echo --- : at the end you will be asked if you want to be dropped into VIM with a file explorer and the device MAC/keys listed. you can however also proceed manually from there. | |
echo | |
echo credits \& more: https://unix.stackexchange.com/a/255510 | |
exit 0 | |
echo bla? | |
fi | |
which chntpw >/dev/null || { echo 'chntpw must be installed'; exit 1; } | |
windir="$1" | |
if [[ ! -d "$windir" ]]; then | |
echo "not a valid dir: $windir (first arg)" >&2 | |
$0 --help | |
exit 1 | |
fi | |
do_chntpw() ( | |
local dir="${3:-$PWD}" | |
local path=$1 | |
local cmd=$2 | |
local chntpwscript="$(cat <<END | sed '/^#/d' | |
cd $path | |
$cmd | |
q | |
END | |
)" | |
cd "$dir" | |
echo "$chntpwscript" | chntpw -e SYSTEM | |
) | |
btwin-controlset() { | |
do_chntpw '' ls $windir | grep CurrentControlSet >/dev/null | |
[[ $? == 0 ]] && echo "CurrentControlSet" && return 0 | |
do_chntpw '' ls $windir | grep ControlSet001 >/dev/null | |
[[ $? == 0 ]] && echo "ControlSet001" && return 0 | |
echo no control set found in windows registry in directory $windir - make sure it is something like .../Windows/System32/config. the following is theoutput of the root 'ls' of the registry: >&2 | |
do_chntpw '' ls $windir >&2 | |
return 1 | |
} | |
btwin-getdevice() { | |
do_chntpw $1 ls $windir | sed -n 's/^\s*<\([0-9a-f]*\)>.*/\1/p' | head -n 1 | |
} | |
btwin-paired() { | |
do_chntpw $1 ls $windir | sed -n 's/.*REG_BINARY\s*<\([0-9a-f]*\)>.*/\1/p' | |
} | |
btwin-getkey() { | |
local regpath=$1 | |
local pairedaddress=$2 | |
local sedcmd='s/^:[0-9A-F]*\s*\(\([0-9A-F][0-9A-F]\s\)*\).*$/\1/p' | |
do_chntpw $regpath "hex $pairedaddress" $windir | sed -n "$sedcmd" | sed 's/\s*//g' | btwin-toUnixKey | |
} | |
btwin-toUnixMac() { | |
local in | |
while read -r line; do | |
echo "${line^^}" | |
done | |
} | |
btwin-toUnixKey() { | |
sed 's/../&:/g;s/:$//' | |
} | |
btwin-main() { | |
btwin_controlset="$(btwin-controlset)" | |
if [[ -n "$btwin_controlset" ]]; then | |
bthportkeys="$btwin_controlset\\Services\\BTHPORT\\Parameter\\Keys" | |
# echo $bthportkeys | |
# btwin_btmac=btwin-btmac "$\\Services\\BTHPORT\\Parameters" | |
device="$(btwin-getdevice $bthportkeys)" | |
if [[ -n "$device" ]]; then | |
bthdevicekeys="$bthportkeys\\$device" | |
paired=() | |
keys=() | |
while read -r address; do | |
# do_chntpw $bthdevicekeys "hex $address" $windir | |
key="$(btwin-getkey $bthdevicekeys $address)" | |
keys+=($key) | |
unixaddress=$(echo $address | btwin-toUnixMac) | |
paired+=($unixaddress) | |
echo $unixaddress $key | |
done < <(btwin-paired $bthdevicekeys) | |
fi | |
fi | |
echo \# instructions: these are paired MAC/key if everything went right. | |
echo \# go to /var/lib/bluetooth and figure out the rest | |
} | |
btwin-main | |
read -p "launch vim at that location with a document of those keys?" -n 1 -r | |
echo # (optional) move to a new line | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
sudo vim -c 'sp | Explore /var/lib/bluetooth/' - < <(btwin-main) | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment