スマートプラグなどのホスト名がない通信デバイスにホスト名をつけたいと思ったことはないだろうか。IPアドレスが固定ならホスト名はavahiのhostsファイルに設定するだけでいいしDHCPやarp-scanからMACアドレスとIPアドレスの対応もわかる。あとはMACアドレスにホスト名を関連付ければ自動設定もできるし管理も楽になりそうということでスクリプトを書いてみた。なにより、あちこちにIPアドレス直打ちすることなく一個のhostsファイルで集中管理できるのが良いかも。
※IPアドレスが変わる可能性があるデバイスがあるなら定期的にスキャンし直すかDHCPで固定IPにすること。
【arp-scanのインストール】
sudo apt install arp-scan
【デバイス・リスト(device.list)】
MACアドレスとホスト名を関連付けするための設定ファイル
1 2 3 4 5 6 7 8 |
0c:80:63:56:ba:63 hs105-1.local コメント(省略可) ac:84:c6:4a:f5:f4 hs105-2.local 5c:e9:31:21:f8:86 p110m-1.local 5c:e9:31:f4:02:0c p110m-2.local 8c:90:2d:4e:6f:cc p110m-3.local 60:83:e7:0c:40:01 p110m-4.local 3c:84:6a:dc:35:4f p105.local 3c:64:cf:39:d2:60 l510.local |
【検出したデバイスを/etc/avahi/hostsに自動設定する】
sudo ./device-scan.sh で実行。ときどき抜けてしまうデバイスがあるので完全に検出できるまで繰り返し実行してみるべし。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#!/bin/sh parse() { parse=$(echo "$1" | sed -E "s/^([^[:space:]]+)[[:space:]]+([^[:space:]]+)[[:space:]]+(.*)/\\$2/") } cdir=$(cd $(dirname $0) && pwd) temp=$(mktemp) mark=#scaned-device# hosts_file=/etc/avahi/hosts device_file=$cdir/device.list echo "$(cat $hosts_file | sed "/${mark}/d")" > $temp device_data="$(cat $device_file)" parse "$device_data" 1 mac_list="$parse" parse "$device_data" 2 name_list="$parse" arp-scan -l | while read line do parse "$line" 1 ip=$parse parse "$line" 2 mac=$parse found=$(echo "$mac_list" | grep -n $mac 2> /dev/null) if [ $? -eq 0 ]; then num=$(echo "$found" | sed -E "s/^([0-9]+):.*$/\1/") host="$ip $(echo "$name_list" | sed -n ${num}p) ${mark}" echo "$host" >> $temp echo "$host" fi done cp $temp $hosts_file rm $temp |
【/etc/avahi/hostsの自動設定の例】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# This file is part of avahi. # # avahi is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2 of the # License, or (at your option) any later version. # # avahi is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public # License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with avahi; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # USA. # This file contains static ip address <-> host name mappings. These # can be useful to publish services on behalf of a non-avahi enabled # device. Please bear in mind that host names are expected to be # fully qualified domain names, i.e. ending in .local! # See avahi.hosts(5) for more information on this configuration file! # Examples: # 192.168.0.1 router.local # 2001::81:1 test.local 192.168.0.11 p110m-2.local #scaned-device# 192.168.0.25 p110m-4.local #scaned-device# 192.168.0.22 p110m-3.local #scaned-device# 192.168.0.24 p110m-1.local #scaned-device# 192.168.0.122 hs105-1.local #scaned-device# 192.168.0.120 hs105-2.local #scaned-device# |