I would like to know if there is any script in either python or C, by which I can save the IP address of my raspberry pi to a text file every time it boots up. I need to send this IP address to another raspberry pi who's IP address is static. I application that I am designing prevents me from assigning a static IP to the ther raspberry pi's. I plan to use socket program to send the IP address between the PIs.

I am also open to other methods of doing this if there are any! Thanks in advance

  • This is possibly technically off-topic but no different to dozens of others which ask how to find IP address of Pi - except it explains why he wants to do it and how he propses to use it.
    – Milliways
    Commented Mar 29, 2016 at 12:07

2 Answers 2


You can use a simple shell script to grab eth0's IP address.

ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}' > /home/pi/ip.txt

For an example, I will save this as ip.sh in my home directory: /home/pi/ip.sh

Make sure to mark it executable with:

chmod +x /home/pi/ip.sh

Then to run this script at start up, open up /etc/rc.local and make it look like this:

#!/bin/sh -e
# rc.local
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
# In order to enable or disable this script just change the execution
# bits.
# By default this script does nothing.
sh /home/pi/ip.sh &
exit 0
  • Seems a bit unnecessary to run grep, cut and awk when awk can do the lot on its own... ifconfig eth0 | awk '/inet /{print $2}' Commented Mar 30, 2016 at 11:44

You don't need to know the address for most protocols. See the "Connecting a Computer to the Pi" section in How do I set up networking/WiFi/Static IP

RemotePi=$(getent hosts hostname.local | awk '{ print $1 }') should give you the remote IP address. (The Pi should have unique hostname.)

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