Linux Panel Widget to Show Your IP Address(es)

Linux Panel Widget to Show Your IP Address(es)

Knowing your internal and external IP addresses can be useful for a variety of reasons, especially in development and penetration testing. Instead of checking your IP address constantly, you can add a nifty widget, as seen in the photo above, to your panel in many Linux distributions. We start by generating some quick bash code. Mine includes some additional code for the instances when I'm using a VPN, and I'm also working under the assumption that when I'm connected to a VPN, it's automatically going to default to the tun0 interface. If you don't expect to be using one, you can cut out those lines, or if your setup is different, you can adjust accordingly.

Find a spot where you can create a bash script that won't accidentally get deleted, and copy and paste the following code inside:

#!/bin/bash

# Generic Monitor Script
# Gets WAN, LAN, and VPN IP addresses
# Presents IP addresses to panel via colored output
# Provides colored Tooltips for each IP type

# Get internal IP address
hostIP="$(hostname -I)";
lanIP="$(echo "${hostIP}" | awk '{print $1}')";

# Get external IP address
wanIP="$(curl ifconfig.me 2>/dev/null)";

# Create text output
textOuts="<txt><span fgcolor='Green'>${lanIP}</span> | <span fgcolor='Cyan'>${wanIP}</span>"
# Create tooltip output
toolTips="<tool><span fgcolor='Green'>LAN</span> | <span fgcolor='Cyan'>WAN</span>"

# Get TUN device info
tunDev=$(ip a show tun0 2>&1);
# Test if device exists
if [[ ${tunDev} != *"not exist"* ]]
then
	# Get TUN IP address
	tunIP="$(echo "${hostIP}" | awk '{print $2}')";

	# Append text output
	textOuts+=" | <span fgcolor='Yellow'>${tunIP}</span>"	
	# Append tooltip output
	toolTips+=" | <span fgcolor='Yellow'>VPN</span>"
fi

# Close text output
textOuts+="</txt>"
# Close tooltip output
toolTips+="</tool>"


# Print output
echo "${textOuts} ${toolTips}"

Make sure you chmod +x widget.sh (or whatever you named it) to give it the appropriate execution permissions, then right-click your panel and select Panel > Add New Items... Your experience may vary depending on exactly which distribution you're working with. Within new items, you're looking for Generic Monitor.

Panel > Add New Items > Generic Monitor

Once you've added it, right-click and "Move," then slide that bad boy over to where you'd like it to be, then right-click and select Properties. Within properties, you want to click the ... and find the file location for your script, then uncheck the label box, and if so desired, change the Period (s) to something like 5s, so it updates a little faster. It's not too taxing on the system, so running it fairly often shouldn't affect anything. The last thing to do is just save it and enjoy your new IP Address Widget!