在实习期间,我编写了一些perl脚本,我想简化它们的使用。脚本在arg中询问一个mac地址,并返回所连接的交换机,速度...等。
除了提供Mac地址,我还想提供计算机的主机名。那么,如何将主机名解析为mac地址?
谢谢再见。

编辑->解决方案可能是:bash命令或perl模块或类似的功能强大的东西...

最佳答案

这有帮助吗?

[mpenning@Bucksnort ~]$ arp -an
? (4.121.8.3) at 08:00:27:f5:5b:6b [ether] on eth0
? (4.121.8.4) at 08:00:27:f5:5b:6b [ether] on eth0
? (4.121.8.1) at 00:1b:53:6b:c9:c4 [ether] on eth0
[mpenning@Bucksnort ~]$


在python中...

#!/usr/bin/env python
import subprocess
import re

def parse_arpline(line, hosts):
    match = re.search(r'\((\S+?)\)\s+at\s+(\S+)', line)
    if match is not None:
        ipaddr = match.group(1)
        mac = match.group(2)
        hosts.append((ipaddr, mac))
    return hosts

SUBNET = '192.168.1.0/24'  # Insert your subnet here
subprocess.Popen([r"nmap","-sP", SUBNET],stdout=subprocess.PIPE).communicate()
p = subprocess.Popen([r"arp","-an"],stdout=subprocess.PIPE).communicate()[0].split('\n')
hosts = []
ii = 0
for line in p:
    hosts = parse_arpline(line, hosts)
    ii +=1
# Iterate and do something with the hosts list
print hosts


在Perl ...

my $SUBNET = '192.168.1.0/24';  # Insert your subnet here
`nmap -sP $SUBNET`;
my $p = `arp -an`;
for my $line (split('\n', $p)) {
    $line=~/\((\S+?)\)\s+at\s+(\S+)/;
    $ipaddr = $1;
    $mac = $2;
    # do something with with each mac and ip address
}

关于python - 通过主机名解析mac地址,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5936781/

10-12 21:03