开发者

Resolve mac address by host name

开发者 https://www.devze.com 2023-03-04 11:54 出处:网络
I\'ve wrote several perl scripts during my internship, and I would like to simplify the use of them. The scripts asks in arg, a mac address, and returns which switch is connected, speed...etc.

I've wrote several perl scripts during my internship, and I would like to simplify the use of them. The scripts asks in arg, a mac address, and returns which switch is connected, speed...etc. Instead of giving a mac address, I would like to give a host name of a computer. So, how can I resolve the hostname to mac address ?

Thanks, bye.

Edit -> Solution could be : bash command or perl module 开发者_如何学Cor something powerfull like that...


Does this help?

[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 ~]$

In 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

in 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
}


The ethers file on a UNIX system maps Ethernet address to IP-number (or hostname). If your /etc/ethers is properly maintained, you can look it up in there.

0

精彩评论

暂无评论...
验证码 换一张
取 消