DEV Community

Kevin John Mulligan
Kevin John Mulligan

Posted on

Default setup - Display IP on startup 🌐

Display IP on startup

Show IP address on startup (for connection via SSH)

  1. Create "display_ip.py"

  2. Run this command to install the modules

$pip install urlopen socket json

  1. Run this command to open the cron jobs file

$ sudo crontab -e

Add this to the cron jobs file. (Sleep for 1 minute to allow the internet to connect)

@reboot /bin/sleep 60; python3 /home/pi/display_ip.py
Enter fullscreen mode Exit fullscreen mode

Addendum

File: display_ip.py

#!/usr/bin/python3

from urllib.request import urlopen
import json
import socket

from tkinter import *
import sys
import os

if os.environ.get('DISPLAY','') == '':
    print('no display found. Using :0.0')
    os.environ.__setitem__('DISPLAY', ':0.0')


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

hostName = Label(rootWindow, font = ('TkFixedFont', 20),)
hostName.grid(sticky = W, row = 1, column = 0, padx = 10, pady = (20,10))
localIP = Label(rootWindow, font = ('TkFixedFont', 20))
localIP.grid(sticky = W, row = 2, column = 0, padx = 10, pady = (20,10))
publicIP = Label(rootWindow, font = ('TkFixedFont', 20),)
publicIP.grid(sticky = W, row = 3, column = 0, padx = 10, pady = (20,10))

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= "LAN:  " + ipaddr)
publicIP.config(text="WAN:  " + ip['origin'])
hostName.config(text="Host: " + host)

rootWindow.mainloop()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)