0

I tried the following command to find the local IP of my RPI:

nmap -sP 192.168.1.1-254

But, the only IP I find is my PC. I know that my PI is at 192.168.1.6 which I can SSH into without any problem and I can even ping it. What can I do to get the local IP of my PI from my terminal on my PC?

4
  • What are you running the command on? How is the Pi configured? What is it connected to?
    – Milliways
    Commented Mar 20, 2021 at 4:19
  • @Milliways, I have the newest recently updated version of the box Raspberry OS (desktop version) running on RPI Zero W. Commands are run on Ubuntu terminal on Windows.
    – Clone
    Commented Mar 20, 2021 at 4:41
  • nmap seems to only find about half my devices (pi and non-pi) - probably a problem with nmap rather than the pi Commented Mar 20, 2021 at 4:58
  • Your response is incomplete and confusing. What is "Ubuntu terminal on Windows" (NOTE edit your Question don't answer in Comments). It is unclear why you ask if you know the IP. arp -n raspberrypi.local will show the address of your Pi (assuming it is in contact with the host)
    – Milliways
    Commented Mar 20, 2021 at 5:27

3 Answers 3

3

Here's a script that will find all1 of the Raspberry Pi's on your local network:

#!/bin/sh

: ${1?"Usage: $0 ip subnet to scan. eg '192.168.1.'"}

subnet=$1
for addr in `seq 0 1 255 `; do
( ping -c 3 -t 5 $subnet$addr > /dev/null ) &
done
arp -a | grep -E --ignore-case 'b8:27:eb|dc:a6:32'
  • Copy & paste this into your text editor on RPi (nano), then save/write it as pingpong.sh in your home directory

  • make it executable:

chmod 755 pingpong.sh
  • execute it (use your network address here, not necessarily 192.168.1.):
./pingpong.sh 192.168.1.

Your output may look like this: This output reflects my network; it has 2 RPis connected, and one of them is a 3B+ with WiFi enabled and the other an oldie 1bp connected via Ethernet:

raspberrypi3b.local (192.168.1.144) at b8:27:eb:12:34:56 on en0 ifscope 
raspberrypi1bp.local (192.168.1.179) at b8:27:eb:78:9a:bc on en0 ifscope 

Notes:
1. All Pi's that have a MAC address whose first 24 bits are one of the two OUI numbers assigned to the Raspberry Pi Foundation. This may not include Pi's using 3rd party network hardware, or those whose MAC address has been 'spoofed'.

2
  • 1
    Very Clever. It didn't find all my Pi; my PiB+ is using a WiFi dongle but should work for others.
    – Milliways
    Commented Mar 20, 2021 at 23:12
  • @Milliways: Good point, I've revised to cover that.
    – Seamus
    Commented Mar 21, 2021 at 1:20
0

Add this to your .bash_aliases to get your ipv4, modify as needed to get the 6.

function ipv4() { ifconfig wlan0 | grep "inet " | awk '{ print $2 }' ; }

I'm running raspian as of this current date. -Steve

oops... edit: change wlan0 to your current device.

Another Edit: this can sometimes help:

function displ() { printf "`who | tail -1 | sed 's/[()]//g' | awk '{print $5}'`:0.0" ; }
0

I modified Seamus script to automatically determine subnet and to work on macOS or Linux.

#! /bin/sh
# find Raspberry Pi IP Address on macOS or Linux
# https://raspberrypi.stackexchange.com/a/122395/8697
# 2021-05-11

# Check if ip commend exists i.e. Linux
IPEXISTS=$(which ip)
ret=$?

if [ $ret -eq 0 ] ; then
    ISLINUX='Y'
fi

if [ $ISLINUX ]; then
    GATEWAY=$(ip route | grep default | head -n1 | awk '{print $3}')
else
    GATEWAY=$(route get default | grep gateway | awk '{print $2}')
fi

SUBNET=$(echo $GATEWAY | awk -F '.' '{print $1"."$2"."$3"."}')

for ADDR in `seq 0 1 255 `; do
( ping -c 1 -t 5 $SUBNET$ADDR > /dev/null 2>&1 ) &
done

arp -a | grep -E --ignore-case 'b8:27:eb|dc:a6:32'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.