PDA

View Full Version : Perl: find names of computers on local network


Kraetos
2008-05-13, 14:30
The title of this one is pretty self explanatory. I need a perl script to just list the names of all of the computers on the local network. I feel like this shouldn't be difficult, but I'm getting nowhere with it.

My first thought was to run netstat from the script, pipe the input to a text file and regex it for the names, but, running netstat from the shell gives me an incomplete list, so that's no good.

I poked around in the perldoc and found gethostbyaddr(), but it doesn't seem to be working. I feed it an ip address and it gives me nothing back.

I'm pretty lost on this one :( Any ideas?

chucker
2008-05-13, 14:46
Something like this?

$ nmap -sP 192.168.2.*

Starting Nmap 4.53 ( http://insecure.org ) at 2008-05-13 21:43 CEST
Host foo (192.168.2.1) appears to be up.
Host bar (192.168.2.21) appears to be up.
Host baz (192.168.2.24) appears to be up.
Host burp (192.168.2.27) appears to be up.
Host belch (192.168.2.30) appears to be up.
Nmap done: 256 IP addresses (5 hosts up) scanned in 1.135 seconds

$ nmap -sP 192.168.2.* | grep 'appears' | cut -d ' ' -f2
foo
bar
baz
burp
belch

Kraetos
2008-05-13, 14:54
I am unfamiliar with Nmap. Do I need to install it?

chucker
2008-05-13, 14:58
nmap is unrelated to Perl. (In fact, the above is just shell scripting; no Perl at all — but then so is netstat…)

You cannot rely on any *nix system to have nmap.