0

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

Jacobm001
  • 11,904
  • 7
  • 47
  • 58

2 Answers2

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
Greenonline
  • 2,969
  • 5
  • 27
  • 38
jrcichra
  • 168
  • 1
  • 7
0

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.)

Milliways
  • 62,573
  • 32
  • 113
  • 225