唤醒LAN主机文件

有没有这样的事情,作为一个文件链接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 

谢谢。

一些小小的bash:

 $ 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输出的脚本。

etherwake实用程序(上游似乎已经死了)可以从/etc/ethers (或/etc/nsswitch.conf指定的另一种ether数据库 )读取MAC地址。

我为它创build了自己的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 # # eg 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 

在Linux上是否有连接MAC地址和IP地址的文件?

是的,有点: / proc / net / arp 。 但当然,这不是一个真正的永久文件。

您的目标标准文件是/ etc / ethers ,但您必须创build并填充它。 手工操作是无聊的,但写一个命令行来为你做这件事很有趣。

你也可以

  • ping所有主机并parsing/ proc / net / arp(或者arp -n如果不在Linux上),

  • 或者您可以使用nmap ( sudo apt-get install nmap ?)为您扫描本地networking,并为您提供IP地址和MAC地址。

第一个解决scheme可能是这样的:

 for i in `seq 1 254`; do ip=192.168.1.$i if ping -c 1 -w 1 $ip >/dev/null; then mac=$(grep "^$ip" /proc/net/arp | awk '{print $4}') echo "$mac $ip" fi done 

nmap解决scheme应该更快,可能是这样的:

 sudo nmap -sP -n -oN - 192.168.1.0/24 | \ perl -nle 'if (/ \b ([0-9\.]{7,15}) \b /x) {$ip=$1} elsif (/ ([0-9A-F:]{17}) /i) {print "$1 $ip"}' 

为了也得到DNS名称,你可以通过类似的东西来pipe理上面的输出

 | while read mac ip; do name=$(dig -x $ip +short | grep -v '^;'); [ -n "$name" ] && ip=$name; echo "$mac $ip"; done 

(使用nmap解决scheme,你也可以删除-n选项,并修改正则expression式来获取名称,如果你真的喜欢正则expression式)。

将处理这个大部分的工具是arpwatch 。 默认情况下(至less在Debian上)是写入/var/lib/arpwatch/arp.dat 。 每次停止arpwatch都会刷新并更新此文件。

该文件包含这种forms的条目:

 52:54:00:aa:bb:cc 192.168.1.2 1452252063 somehostname eth0 

/etc/ethers文件只需要MAC地址以及IP地址或可parsing的主机名:

 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 

arp -a会列出您的系统上的ARPcaching表,该表应显示您最近访问的计算机本地子网上IP地址的MAC地址。 但是,对于您的预期用途,此表可能无用,因为该表中的条目可能会在一段时间后被删除。 这段时间是操作系统TCP堆栈执行依赖。

当然,可以有一个持续运行的脚本,它定期执行arp -a命令,并用检索到的新信息更新文件。

我已经写了一个脚本,从dhcpd.conf中检索mac-address和hostname或ip-address,然后构build唤醒数据包(改进版本的别人的perl脚本)。 如果你想的话,我可以把它放在pastebin.com或者这样的。