2

I need to store the current IP adress of my Raspberry Pi into a file. Can someone tell me how to do this?

The goal is to create a file in /var/www/ipadress.txt with this content:

192.168.0.28

$ ifconfig
eth0      Link encap:Ethernet  Hardware Adresse b8:27:eb:7c:ce:18
          inet Adresse:192.168.0.28  Bcast:192.168.0.255  Maske:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metrik:1
          RX packets:1762 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1044 errors:0 dropped:0 overruns:0 carrier:0
          Kollisionen:0 Sendewarteschlangenlänge:1000
          RX bytes:267534 (261.2 KiB)  TX bytes:233261 (227.7 KiB)

I need the part: 192.168.0.28

syb0rg
  • 8,178
  • 4
  • 38
  • 51
s3bi
  • 160
  • 1
  • 2
  • 7

2 Answers2

2

I found that this command will give you the IP address only:

$ /sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'

To output it to a file:

$ /sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}' > "/var/www/ipadress.txt"
syb0rg
  • 8,178
  • 4
  • 38
  • 51
0

language independent version:

ifconfig | grep -o "\([0-9]\+\.\)\+[0-9]\+" | head -n 1

or, as it was noted in another question

hostname -I

might do the trick as well.

lenik
  • 11,533
  • 2
  • 32
  • 37