Skip to content

Instantly share code, notes, and snippets.

@mamemomonga
Last active March 11, 2024 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamemomonga/b6c571b500e7d056fb15a523c942aa29 to your computer and use it in GitHub Desktop.
Save mamemomonga/b6c571b500e7d056fb15a523c942aa29 to your computer and use it in GitHub Desktop.
AirPlayデバイスのホスト名を特定するシェルスクリプト

find-airplay-host.sh

  • AirPlayデバイスのホスト名を特定するシェルスクリプトです。
  • macOS用。
  • カレントディレクトリに list_names.txt, instances.txt が作成されます。

使い方

「屋根裏部屋」がターゲットのAirPlayデバイスだとします。

まず、引数なしで実行し、AirPlayデバイス一覧を取得します

$ ./find-airplay-host.sh
俺 の MacBookPro
俺 の MacBoo
屋根裏部屋

次に、「屋根裏部屋」を引数につけて実行します

$ ./find-airplay-host.sh "屋根裏部屋"
HOSTNAME: WiiM-Mini-FFFF.local
IPADDR:   192.168.0.123

これで、ホスト名とIPアドレスが得られます。

#!/bin/bash
set -eu
TARGET_NAME="${1:-}"
list_names() {
local tmpfile="list_names.txt"
if [ -e "$tmpfile" ]; then rm "$tmpfile"; fi
dns-sd -B _airplay._tcp > "$tmpfile" &
local cpid=$!
sleep 5
kill -HUP $cpid
set +e; wait $cpid 2>/dev/null; set -e
cat "$tmpfile" | sed '1,4d' | perl -nlE 'if(/^(?:(?:[^\s]+)\s+){6}(.+)$/) { say $1 }' | uniq
}
get_hostname() {
local tmpfile="instances.txt"
if [ -e "$tmpfile" ]; then rm "$tmpfile"; fi
dns-sd -L "$TARGET_NAME" _airplay._tcp > "$tmpfile" &
local cpid=$!
sleep 5
kill -HUP $cpid
set +e; wait $cpid 2>/dev/null; set -e
cat "$tmpfile" | perl -nlE 'if(m!\Qcan be reached at \E(.+)\.:!) { say $1 }' | uniq
}
get_ipaddr() {
THOST=$1 perl -E 'say join(".",unpack("C4",gethostbyname($ENV{THOST})))'
}
if [ -z "$TARGET_NAME" ]; then
list_names
else
TARGET_HOST=$(get_hostname)
TARGET_IPADDR=$(get_ipaddr $TARGET_HOST)
echo "HOSTNAME: $TARGET_HOST"
echo "IPADDR: $TARGET_IPADDR"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment