Obtaining hostnames and mac addresses from ip

Discussion in 'Mac Programming' started by Denarius, Apr 18, 2008.

  1. Denarius macrumors 6502a

    Denarius

    Joined:
    Feb 5, 2008
    Location:
    Gironde, France
    #1
    I have a bit of a puzzle that I'm stuck on. What I'm trying to do is obtain the hostnames and mac addresses of all computers on our LAN. I'm starting off with arp -a to obtain the ip addresses on the network and then hoping to tie those up with netstat -r. The problem is netstat -r will reliably tally up the ip with a mac address on the internet table, but it isn't always giving me the hostname against the MAC address on the internet 6 table. What's particularly vexing is that the host names must be somewhere as remote computers are showing up in the finder window. Does anyone have any thoughts about how I can force them to show up or whether there's an alternative strategy for collecting this information?
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    are you needing to do this programatically or do you just need to be able to do it "by hand"?
    I can say that I had a lot of trouble getting the MAC programatically and ended up having to have the remote program get it and send it.
    if you just need it by hand I'd peruse network utility. It consolidates a lot of the network tools.
    -Lee
     
  3. Denarius thread starter macrumors 6502a

    Denarius

    Joined:
    Feb 5, 2008
    Location:
    Gironde, France
    #3
    Sadly, I need it programatically. I'm working in Ruby (attempting to at any rate), but I was looking towards piping unix commands from the script.

    Strangely enough, macs aren't proving a problem, arp -a seems to do that without a hitch, it's host names that are the major sticking point, as their presence in ARP seems to be a bit hit and miss.

    heres an example of my arp -a output

    ? (192.168.1.1) at 0:14:15:23:90:4b on en1 [ethernet]
    ? (192.168.1.2) at 0:a:92:ab:ba:81 on en1 [ethernet]
    ? (192.168.1.255) at (incomplete) on en1 [ethernet]

    Mac addresses have been changed to protect the guilty. :) I'm wondering if hosts names are supposed to be where those question marks are?
     
  4. Denarius thread starter macrumors 6502a

    Denarius

    Joined:
    Feb 5, 2008
    Location:
    Gironde, France
    #4
    mDNS to the rescue..

    Ahhhhh.... Bonjour is the answer apparently, just discovered the mDNS -B command and even a ruby gem for dealing with mDNS. Lovely job.
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    does that work for Windows and Linux machines as well, or just the Macs on your network?
     
  6. Denarius thread starter macrumors 6502a

    Denarius

    Joined:
    Feb 5, 2008
    Location:
    Gironde, France
    #6
    That's a good question. It certainly won't work out of the box with Linux and Windows, but I don't think it's an unreasonable requirement to have Bonjour installed on windows or mDNSresponder on a Linux box. That said if someone can come up with getting a better solution for retrieving this without a DNS or mDNS on non-apple boxes then I'd love to hear it.

    Incidentally, I'm actually having a bit of a fight with mDNS now. I'm doing mDNS -B _ipp._tcp which is returning all available printers on the LAN, but I also want to check for broadcasting of apple file sharing, which as far as I can see from the services list would be mDNS -B _afpovertcp._tcp. That's not returning anything. Have I got it wrong?
     
  7. Denarius thread starter macrumors 6502a

    Denarius

    Joined:
    Feb 5, 2008
    Location:
    Gironde, France
    #7
    Don't know if anyones interested, but mDNS seems to be a bit outdated as a command and dns-sd seems to be the replacement.

    On the back of this, I think I've found out a reliable method for getting all the info I originally wanted without a DNS on the LAN, using mDNS.

    You can get computers hosting printers with:
    dns-sd -B _ipp._tcp
    and computers with apple file sharing with:
    dns-sd -B _afpovertcp._tcp

    Having got computer names, it seems a bit hit and miss getting mac addresses from the arp command (sometimes the mac address is missing), but I've discovered a single ping to the target computer resolves this and ARP will provide you with the IP and the MAC address happily. Is anyone aware of any arp related methods in ruby or maybe a gem somewhere? Amazing the gems I've stumbled across trying to pull this project together: one for reading and writing apple plists, one for mDNS, really cool stuff.

    Lee, you got me thinking on the windows and linux front and I reckon smbclient might be a way to go there for starters? Not too worried about that until I've got the pure mac solution working. :)
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    Good work digging up the relevant resources.

    If you're looking for just smb shares I'm sure smbclient would be able to handle it, but you might have a tough time finding all of the machines. If you have a list of subnets, you may be able to get away with:
    ping -t 10 XXX.YYY.ZZZ.255

    for each subnet. It gives 10 seconds for all responses, which should be more than enough on any worthwhile network. Afterwards you can hit the arp, but I'm not sure about the hostnames.

    From a windows machine I can get away with nbtstat -a XXX.YYY.ZZZ.AAA
    to get a windows machines netbios name from an IP. I'm not sure if there is a similar smbclient command or not.

    Linux, strangely enough, is the one I'm least sure of. nslookup would work if you had dns and could do a reverse lookup, but it sounds like you don't. I found a few VB examples, but that won't help. Maybe someone else can chime in on this.

    Good luck.

    -Lee
     
  9. Denarius thread starter macrumors 6502a

    Denarius

    Joined:
    Feb 5, 2008
    Location:
    Gironde, France
    #9
    Hmmm, I might as well come clean on what I'm trying to do. The original brief was a system for waking up computers over the LAN when you have a print job you want to send to the computer. I've got a skeleton system up and running that does wake the target computer (mac and broadcast ip hard-coded), but I want the system to be able to collect all info about potential target servers and compare them to available LAN printers(obviously do this when they're all awake for future reference from a Plist). I want to avoid DNS as, although I'm sure I could set one up on my LAN, the plan is to have a package that anyone could install on their system and have it do the business without having to get into customising their LAN for the purpose.

    Interested in what you had to say on the broadcast address. I understood that 255.255.255.255 would address computers on the same subnet and that say (for example) 192.168.1.255 (that's the broadcast on my router) would traverse all subnets. I may have misunderstood though.
     
  10. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #10
    on the subnet 192.168.1.0/24 192.168.1.255 is the broadcast address for the whole subnet.

    As for your problem set, it seems much more difficult to deal with locally attached printers vs. Network printers. There may be nothing you can do about that, but having wifi or Ethernet attatched printers would be a lot easier. If you have a few USB printers, a few airport express units could be used to share them. You could then set all of the printers up on a central machine that shares them.

    Otherwise you should only really need to map once. If you are intending to sell this software, obviously this would need to be automated. If this is more of an IT project for your company, there are probably better IT ways of dealing with this as opposed to a programming solution.

    With that said, I respect your gumption in wanting to try to get this done. If you are going to keep at it I would say that the mapping of machines should be kept centrally and rsync'd out or the program could look at the remote datastore. Building the list dynamically on each machine seems error-prone.

    If you need more help post, and I'm sure we will be able to help.

    -Lee
     
  11. Denarius thread starter macrumors 6502a

    Denarius

    Joined:
    Feb 5, 2008
    Location:
    Gironde, France
    #11
    Thank you very much Lee! It's not for a company or anything, it's just a principle that I've got working in our house and I thought it would be fun to make it a bit more flexible, GUIfy it and maybe post it on the web if it works okay.

    You're absolutely right that network printers would be a damned sight easier, but writing a bit of software to overcome the sleep issue is going to be interesting and probably cheaper than buying lots of Ethernet/USB print servers :).

    I think I'll follow your suggestion of a manually created list for starters, I can always attempt the dynamic approach at a later date.
    Right, I reckon the time has come to see if I can apply any of this Ruby I've been learning. Thanks again Lee.
     

Share This Page