I need to know the IP address of my raspberry pi. I need to do this with a Character LCD display. So here's what I have done so far.

I can display text on a HD44780 based 16x2 LCD connected to my Raspberry-Pi as shown here.

Also using putty if i do hostname -I I get my local IP as

Now I think I need to fire/execute this command 'hostname -I' using python, grab the result text( which will be the IP) string, and pass it to my lcd. MoreOver I need to do this once after every DHCP address assignment( I don't know the exact/correct name of this thing). Now the problem is, my linux(raspian) knowledge is limited. I read on internet about CRON JOB but I'm unsure as to how to do this..

Kindly Help me out !!

  • Other than the printing to the LCD part of this question, this would probably fit better on Unix & Linux, as that is a broader, *nix-focused site, as opposed to this site, which is focused specifically on Raspberry Pi-specific issues. – RPiAwesomeness May 14 '14 at 16:46
  • @RPiAwesomeness I will raise a migration flag then. Thanks. – vvy May 14 '14 at 16:48
  • No problem! Please don't feel we don't want you and your questions here, it's just that RPi.SE is a very focused community, for issues that only occur on the Raspberry Pi (GPIO, RPi Cam, stuff that works on a normal system - but not on the RPi, etc.) – RPiAwesomeness May 14 '14 at 16:50
  • @RPiAwesomeness I see the rationale of pi-exchange more clearly now, its very focused on Pi related issue. Even as I am searching for python command execute and linux dhcp I am getting results from stackoverflow and Unix and Linux. So I've raised a migration flag. In the first place I came to this exchange.site thinking that someone might already have done such a thing. What better place then Pi-exchange site. I think I will get back with results to here again for reference purpose. – vvy May 14 '14 at 17:01

cron job is easy, just type crontab -e and you'll get a chance to edit your cron job table, if you want your script to run every 10 minutes, you just add:

*/10 * * * * /home/pi/whatever.py

where /home/pi/whatever.py is your script that executes hostname -I and shows the result on your LCD panel.

| improve this answer | |
  • (+1) It works! I reduced the time period further to 2 minutes. And I am still searching for the DHCP based solution/approach. – vvy May 14 '14 at 18:13

here's explanation regarding DHCP part.

there are two directories in your system:


that contain scripts to be run when dhcp configuration changes. here's a sample script (most likely you have a similar one under the name debug in these directories):

# The purpose of this script is just to show the variables that are
# available to all the scripts in this directory. All these scripts
# are called from /etc/dhcp3/dhclient-script, which exports all the
# variables shown before. If you want to debug a problem with your DHCP
# setup you can enable this script and take a look at
# /tmp/dhclient-script.debug.

# To enable this script set the following variable to "yes"

if [ "$RUN" = "yes" ]; then
    echo $(date): entering ${0%/*}, dumping variables. \
        >> /tmp/dhclient-script.debug

    # loop over the 4 possible prefixes: (empty), cur_, new_, old_
    for prefix in '' 'cur_' 'new_' 'old_'; do
        # loop over the DHCP variables passed to dhclient-script
        for basevar in reason interface medium alias_ip_address \
                   ip_address host_name network_number subnet_mask \
                   broadcast_address routers static_routes \
                   rfc3442_classless_static_routes \
                   domain_name domain_search domain_name_servers \
                   netbios_name_servers netbios_scope \
                   ntp_servers \
                   ip6_address ip6_prefix ip6_prefixlen \
                   dhcp6_domain_search dhcp6_name_servers ; do

            # show only variables with values set
            if [ -n "${!var}" ]; then
                echo "$var='${!var}'" >> /tmp/dhclient-script.debug

    echo '--------------------------' >> /tmp/dhclient-script.debug

you may copy this script under the name you like, enable it by setting RUN="yes" and have fun with the variables available (run it once to see the output in /tmp/dhclient-script.debug).

or maybe just ignore the variables and use hostname -I approach as before.

anyway, if you need a manual regarding these scripts, check this.

| improve this answer | |
  • AWESOME ! I think this is the ideal approach. A question for now. When I added the cron job( to execute my python script) in previous approach, I had to use the sudo prefix( sudo python -u ip_disp_clcd.py). However in this approach, as you told, I just need to copy my python script to the directory( supposedly dhclient-exit-hooks.d) and it will work. Won't it require the sudo force as the previous one did ?? – vvy May 17 '14 at 10:17
  • @vvy why don't you try and see? – lenik May 17 '14 at 12:56
  • That's the first thing on my TODO list, I'll test it as soon as I receive my new R-Pi. I have submitted my previous R-Pi to the college checkout this snap. – vvy May 19 '14 at 5:10

I had a similar issue. Even though you already have the answer for those looking for something that does the trick here's what I wrote using python and a PifaceCAD LCD

#!/usr/bin/env python
Get's the local IPv4 address and displays to the user
    * ifconfig
    * grep
    * awk
import subprocess

import pifacecad

_VERSION = '0.1'

def get_ipv4():
    ip = subprocess.check_output('ifconfig | grep broadcast | awk \'{print $2}\'', shell=True).decode('utf-8')
    print('IP is: {}'.format(ip))
    return ip

def main(cad=None):
    if not cad:
        cad = pifacecad.PiFaceCad()
    cad.lcd.write('IPv4 Address:')
    ip = get_ipv4()
    cad.lcd.set_cursor(0, 1)
    cad.lcd.write(' ' * pifacecad.lcd.LCD_WIDTH)
    cad.lcd.set_cursor(0, 1)

    return 0

if __name__ == '__main__':

As you can see I didn't use the 'hostname -I' because I had limited success with that, for some reason even though it seems more complex the ifconfig/grep/awk solution worked better for me.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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