是否有将 MAC 地址链接到 IP 地址/DNS 名称的文件,用于 Linux 上的 wakeonlan?像这样:
$ cat /etc/machosts
00:1f:d0:34:e0:ea 192.168.0.5
00:1f:d0:34:a1:06 192.168.0.7
$ cat /etc/hosts
192.168.0.5 mypc
$ wakeonlan mypc
谢谢。
答案1
一些小问题:
$ cat /path/to/machosts
macs[mypc1]=00:1f:d0:34:e0:ea
macs[mypc2]=00:1f:d0:34:a1:06
$ cat wakeonlan.sh
#!/bin/bash
. /path/to/machosts
echo wakeonlan ${macs[$1]}
$ ./wakeonlan.sh mypc1
wakeonlan 00:1f:d0:34:a1:06
这使用 bash 数组:http://tldp.org/LDP/abs/html/arrays.html
unix.stackexchange.com 网站可能可以提供更多有关执行此脚本和处理 arp 输出的脚本的帮助。
答案2
这以太唤醒实用程序(上游似乎已死)可以从/etc/ethers
(或中指定的另一种ethers
数据库/etc/nsswitch.conf
)读取 MAC 地址。
答案3
我为其创建了自己的 bash 脚本:
#!/bin/bash
die () {
echo >&2 "$@"
exit 1
}
# we need one parameter - the hostname or IP address
[ "$#" -eq 1 ] || die "1 argument required, $# provided"
if [[ ! -f "/etc/machosts" ]]; then
die "Can't find /etc/machosts file!"
fi
host="$1"
# if argument isn't an IPv4 address, try to resolve it
if [[ ! $host =~ ^([0-2]?[0-9]{1,2}\.){3}([0-2]?[0-9]{1,2})$ ]]; then
echo "Attempting to identify IP from name: $host..."
host=$(getent ahosts $host | head -n 1 | cut -d" " -f1)
fi
if [[ ! $host =~ ^([0-2]?[0-9]{1,2}\.){3}([0-2]?[0-9]{1,2})$ ]]; then
die "Invalid hostname"
fi
mac=""
# read /etc/machosts line by line
while read line
do
if [[ !(${line:0:1} == "#") && ( -n "$line" ) ]]; then
ip=$(echo $line | cut -d" " -f2)
addr=$(echo $line | cut -d" " -f1)
if [[ $ip == $host ]]; then
mac=$addr
break
fi
fi
done < "/etc/machosts"
if [[ -z $mac ]]; then
die "No MAC address asociated with that host!"
fi
wakeonlan $mac
exit 0
我的 /etc/machosts 如下所示:
# here the MAC hosts are defined
#
# e.g. 50:e7:24:ab:c0:d3 10.3.12.5
50:e5:49:1a:8c:9c 192.168.0.4
00:1f:d0:34:e0:ea 192.168.0.5
00:25:22:b1:f6:be 192.168.0.6
00:1f:d0:34:a1:06 192.168.0.7
答案4
可以处理大部分此类问题的工具是arpwatch
。默认情况下(至少在 Debian 上)它会写入。每次停止/var/lib/arpwatch/arp.dat
时都会刷新并更新此文件。arpwatch
该文件包含以下形式的条目:
52:54:00:aa:bb:cc 192.168.1.2 1452252063 somehostname eth0
该/etc/ethers
文件只需要 MAC 地址和 IP 地址或可解析的主机名:
52:54:00:aa:bb:cc 192.168.1.2
然后,通过每天运行的一个小脚本就可以很简单地保持/etc/ethers
更新和同步crontab
:
#!/bin/bash
# Flush arp.dat
service arpwatch restart
# Save a copy
test -f /etc/ethers || touch /etc/ethers
cp -fp /etc/ethers /etc/ethers.old
# Check to see if anything new has arrived. If so rebuild the file
(
echo '# This file is updated automatically from /var/lib/arpwatch/arp.dat'
echo '# Take care when editing'
echo '#'
(
awk '{print $1,$2}' /var/lib/arpwatch/arp.dat
grep -v '^#' /etc/ethers.old
) |
sort -u
) >/etc/ethers.tmp
# Update ethers with the new file
cmp -s /etc/ethers.tmp /etc/ethers || cat /etc/ethers.tmp >/etc/ethers
rm -f /etc/ethers.tmp
# All done
exit 0