315

Using Ubuntu 10.10 command line, how can I list all IPs connected to my home network?

Ideally, it needs to be a CLI command as I will be running it from C++.

11 Answers11

319

Check out the arp-scan command - you will probably have to install it eg:

sudo apt-get install arp-scan

http://manpages.ubuntu.com/manpages/hardy/man1/arp-scan.1.html

And to give further detail:

sudo arp-scan --interface=eth0 --localnet

Where eth0 is your device. You can find your device with:

ifconfig
Linker3000
  • 28,240
149

Use nmap. example: nmap -sn 10.10.10.0/24 The arp cache will only tell you those that you have tried to contact recently.

Keith
  • 8,293
30

In windows this would be arp -a an equivalent of that in Linux would be arp -e.

From the man page for arp:

arp with no mode specifier will print the current content of the table.
-e : Use default Linux style output format (with fixed columns).
Giacomo1968
  • 58,727
David
  • 7,393
23

If your network is 192.168.0.0/24, make an executable file with the following code; Change the 192.168.0 to your actual network.

#!/bin/bash
for ip in 192.168.0.{1..254}; do
  ping -c 1 -W 1 $ip | grep "64 bytes" &
done
Giacomo1968
  • 58,727
18

Try installing nmap (sudo apt-get install nmap) and type nmap 192.168.1.0/24 substituting 192.168.1 with the first three parts of your ip address (find out using ip addr).

You can also get a slightly less accurate (in my experience) map of a network by running ping 192.168.1.255 (again substituting 192.168.1), which should issue a ping to every machine on the network, but, in my experience, does not always function correctly.

squirl
  • 291
11

For a more compact list of connected devices:

nmap -sL 192.168.0.* | grep \(1

Explanation.

nmap -sL 192.168.0.* will list all IPs in subnetwork and mark those, that have name:

Nmap scan report for 192.168.0.0
Nmap scan report for Dlink-Router.Dlink (192.168.0.1)
Nmap scan report for 192.168.0.2
...
Nmap scan report for android-473e80f183648322.Dlink (192.168.0.53)
...
Nmap scan report for 192.168.0.255

As all interesting records start with parenthesis ( and digit 1, we filter for that with | grep \(1 (backslash is needed to escape parenthesis)

Quirk
Beware that if two devices have the same name, nmap will show only the one, that was connected to router last

5

Came up with the following on a nexus using tmux as arp-scan isn't in the repo but nmap came pre-installed, displays just the ip addresses:

nmap -sn 192.168.1.1-254/24 | egrep "scan report" | awk '{print $5}'
zentek
  • 51
  • 1
  • 1
3

This Answer determines the subnet by itself while in the other answers you need to provide it.

The script uses arp -a or ip -o -f inet addr show to find the subnet.

I've build the script elaborating on @anders-larsson and @mathieu-caroff 's answers. I avoid the use of 'nmap', but the script is easily amended to use nmap.

Basically, $baseip is built using bash replacement macros in the second part of the script if no parameter is provided on the command line. Otherwise it will scan the provided subnet (style: 192.1.5 without the third dot last byte of the IP).

#!/bin/bash
function scan ()
{
    for ip in $1.{1..254}; do
        ping -c 1 -W 1 $ip &
    done | sed -nE 's:^.* from ([0-9.]+).*time=(.*s)$:\1 (\2):p'
    wait
}

if [ $1 ]; then for baseip; do scan $baseip done else baseip=$(arp -a) && baseip=${baseip%%)} && baseip=${baseip##(} if [ $baseip"" == "" ] ; then baseip=$(ip -o -f inet addr show|grep "scope global") && baseip=${baseip##* inet} && baseip=${baseip%%/} fi baseip=${baseip%.} scan $baseip fi

le_top
  • 452
2

Ellaborating on Anders Larrson's answer -

#!/bin/bash
function scan ()
{
    for ip in $1.{1..254}; do
        ping -c 1 -W 1 $ip &
    done | sed -nE 's:^.* from ([0-9.]+).*time=(.*s)$:\1 (\2):p'
}

if [ $1 ]; then
    for baseip; do
        scan $baseip
    done
else
    scan 192.168.1
fi
0

Here another solutions in terminal:

nmap -sn 192.168.1.0/24

(if your router ip is starting with 192.168.1._ )

or

ip n

or

ip -r n
PAPPL
  • 1
0

This works for my purposes.

#!/bin/bash
# Ex : write as ~/bin/netscan.sh; chmod +x ~/bin/netscan.sh; ~/bin/netscan.sh 192.168.1
# cat /sys/class/net/*/address ; cat /sys/class/net/$(ip route show default | awk '/default/ {print $5}')/address

scan () { for IP in "${1}."{1..254}; do ping -c1 $IP | grep "64 bytes" & done; wait ;}

wait for receive or time - sleep 6; : ; sleep 1 "

sleep 2 # for some sleepers

scannet () { Devices=$(scan "${1}" 2>/dev/null ); echo "$Devices"; echo "# Done" ;}

########## find_cam () { Find="$1"

Find_dev=$(ip neighbour | grep -i "$Find" ) if [ $? -eq 0 ] then read IP_dev null dev null laddr state <<< "$Find_dev" echo ${IP_dev} ${dev} ${laddr} ${state} else IP_dev='' fi }

play_cam () { Player="vlc" Protocol="rtsp://" Cam_ip="$1" Port=":554" Stream="/0/video1" Url_Stream="${Protocol}${Cam_ip}${Port}${Stream}" echo "# ${Player} $Url_Stream" nohup ${Player} "$Url_Stream" > /dev/null 2>&1 & }

play_ip_arp_cam () { Cam_mac_array=("AA:BB:CC:DD:EE:FF" "AA:BB:CC:DD:EE:FF") for Cmaera in ${Cam_mac_array[*]} do find_cam ${Cmaera} if [[ ${IP_dev} != '' ]]; then play_cam ${IP_dev}; else echo "# Camera ${Cmaera} not found"; fi done

############## Ex=$(cat << EOF Using VLC to test camera stream rtsp://<username>:<password>@<ip>:<port>/cam/realmonitor?channel=<channelNo>&subtype=<typeNo> <ip> - the IP address of the IP Camera. <port> - the default port is 554. It can be omitted. <channelNo> - the channel number. It starts from 1. <typeNo> - the stream type. The <typeNo> of main stream is 0, extra stream 1 is 1, extra stream 2 is 2. Example URL: rtsp://admin:12345scw@192.168.1.210:554/cam/realmonitor?channel=1&subtype=1 will bring up substream for channel 1

we need to rebuild the arp cache...

ip -s -s neighbour flush all scannet() { for IP in "${1}."{1..254}; do ping -c1 $IP | grep "64 bytes" & done; wait ;} EOF ) ############## }

if [[ $1 = '' ]]; then

output=$(ip route list exact default)

ip route show default

echo "# ip route list: $output" read null null default null dev null <<< "$output" srcvalue=${null##src } srcvalue=${srcvalue%% } value=${default%.} echo "##################################################" echo "default = $dev $srcvalue $default '${value}.0/24'" echo "### Ex : netscan.sh $value ; netscan.sh 192.168.1 -"'[${1}].{1..254}' echo "### ip route list exact default" echo "### ip -resolve show" echo "### ip -resolve neighb show {IP}" echo "### sudo nmap -O -n '${value}.' ###" echo echo "# Wait - ping -c1 ${value}"'.{1..254} :' scannet ${value} echo PresentIP=$(ip neighb show | grep "lladdr") SAVEIFS=$IFS; IFS=$'\n' IP_ARRAY=(${PresentIP}); IFS=$SAVEIFS echo "# Net : $dev $srcvalue : ip neighb show :" echo "IP ARRAY : ${#IP_ARRAY[@]}" (IFS=$'\n'; echo "${IP_ARRAY[*]}") exit else echo "### ping -c1 ${1}.{1..254}" echo "### Ex : ip route list exact default ; ip -resolve neigh show ; sudo nmap -O ${1}. ###" echo "# Wait - ping -c1 ${1}"'.{1..254} :' scannet ${1} PresentIP=$(ip neighb show | grep "lladdr") SAVEIFS=$IFS; IFS=$'\n' IP_ARRAY=(${PresentIP}); IFS=$SAVEIFS echo "# ip neighb show :" echo "IP ARRAY : ${#IP_ARRAY[@]}" echo "$PresentIP" if [[ $2 = 'play' ]]; then play_ip_arp_cam; fi

exit fi

Ex : write as ~/bin/netscan.sh # netscan.sh 192.168.1