This answer uses the nmap command to gather information of active hosts in the network.
Nmap ("Network Mapper") is an open source tool for network exploration
and security auditing. It was designed to rapidly scan large networks,
although it works fine against single hosts. Nmap uses raw IP packets
in novel ways to determine what hosts are available on the network,
what services (application name and version) those hosts are offering,
what operating systems (and OS versions) they are running, what type
of packet filters/firewalls are in use, and dozens of other
characteristics.
Assuming you need to scan the 192.168.0.X range, you can try:
nmap -v -sP 192.168.0.0/24
Where 192.168.0.0 is the network address and /24 is the network mask equivalent to 255.255.255.0. Thus the above command will scan 256 hosts.
To collect the active IP addresses, one can use the following line:
It actually concatenates the list of active IP addresses (filtered by grep) into a variable called IPS_UP:
nmap is run with switches -n (no name resolution), -sP (ping scan) and -oG to output a grep processable output onto the standard output (-).
grep filters only lines containing the word "Up" at the end of line ("$").
awk prints the second column in the list output by nmap, which is the IP address, and appends a space.
The $() command substitution allows the output of the chain of commands to be assigned to the IPS_UP variable.
The Network Mapper can be installed by using sudo apt-get install nmap.
Note nmap might discover more hosts if run by a privileged user. This is because different kind of packets are sent to scan a host. By modifying the above line to read sudo nmap ... allows to run the nmap command as root.
Install arp-scan (
sudo apt-get install arp-scan
) and add the following line to the script:Now you have all the active IP addresses in the
IPs
variable.Note: this will only work on a directly connected network, i.e. not accessed through a router.
PS: If you install
gawk
the command can be shortened to (thanks belacqua):This answer uses the
nmap
command to gather information of active hosts in the network.Assuming you need to scan the 192.168.0.X range, you can try:
Where
192.168.0.0
is the network address and/24
is the network mask equivalent to255.255.255.0
. Thus the above command will scan 256 hosts.To collect the active IP addresses, one can use the following line:
It actually concatenates the list of active IP addresses (filtered by
grep
) into a variable calledIPS_UP
:nmap
is run with switches-n
(no name resolution),-sP
(ping scan) and-oG
to output a grep processable output onto the standard output (-
).grep
filters only lines containing the word "Up" at the end of line ("$").awk
prints the second column in the list output bynmap
, which is the IP address, and appends a space.$()
command substitution allows the output of the chain of commands to be assigned to theIPS_UP
variable.The Network Mapper can be installed by using
sudo apt-get install nmap
.Note
nmap
might discover more hosts if run by a privileged user. This is because different kind of packets are sent to scan a host. By modifying the above line to readsudo nmap ...
allows to run thenmap
command as root.Obviously this is a bad idea, but I gave it a try