tony1812
Posts: 354
Joined: Sat Jul 15, 2017 12:40 pm
Location: Boston MA.

Show ip at boot

Thu Dec 20, 2018 10:27 pm

Hello, I have a script like this:

Code: Select all

import socket
import fcntl
import struct

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])

print get_ip_address('lo')
print get_ip_address('wlan0')
I run it in the terminal, all is fine.

Now I want to make it so that it shows the ip of the pi, so in ~/.config/lxsession/LXDE-pi/autostart
I added these lines
@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@[email protected] s noblank
@xset s off
@xset -dpms
@sudo python /home/pi/Desktop/my_ip.py

when I reboot, it doesn't show anything, what gives?

User avatar
DougieLawson
Posts: 35347
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Show ip at boot

Thu Dec 20, 2018 10:32 pm

Try this program which gets both your LAN and public IPv4 addresses.

Code: Select all

#!/usr/bin/python3

from urllib.request import urlopen
import json
import socket
from tkinter import *

rootWindow = Tk()
rootWindow.title('IP Addresses')

localIP = Label(rootWindow, font = ('fixed', 20),)
localIP.grid(sticky = N, row = 2, column = 1, padx = 5, pady = (20,20))
publicIP = Label(rootWindow, font = ('fixed', 20),)
publicIP.grid(sticky = N, row = 3, column = 1, padx = 5, pady = (20,20))
hostName = Label(rootWindow, font = ('fixed', 20),)
hostName.grid(sticky = N, row = 1, column = 1, padx = 5, pady = (20,20))

ip = urlopen('http://httpbin.org/ip').read()
ip = ip.decode('utf-8')
ip = json.loads(ip)
testIP = "8.8.8.8"
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((testIP, 0))
ipaddr = s.getsockname()[0]
host = socket.gethostname()
localIP.config(text=ipaddr)
publicIP.config(text=ip['origin'])
hostName.config(text=host)
print(ip['origin'], "IP:", ipaddr, " Host:", host)

rootWindow.mainloop()
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

klricks
Posts: 6433
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Show ip at boot

Thu Dec 20, 2018 10:43 pm

Try this:

Code: Select all

@lxterminal -e python /home/pi/Desktop/my_ip.py
Note: If your program exits to the command line for any reason then the terminal will immediately close and you won't see anything.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

tony1812
Posts: 354
Joined: Sat Jul 15, 2017 12:40 pm
Location: Boston MA.

Re: Show ip at boot

Fri Dec 21, 2018 9:45 pm

Thank everyone.

I like DougieLawson's solution. It shows up in a nice window. Except for the damn SSH warning blocks it :(

User avatar
DougieLawson
Posts: 35347
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Show ip at boot

Sat Dec 22, 2018 11:39 am

tony1812 wrote:
Fri Dec 21, 2018 9:45 pm
Except for the damn SSH warning blocks it :(
Change the password for the pi userid to any string other than "raspberry" and that nagware pop-up will go silently into hibernation (it'll pop-up again if you change the password back to "raspberry").
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “Beginners”