[root tip] [Utility Script] Get lan IP address

This is a utility script from my notepad to display your local network address.

Intended use case is anywhere you need an unambigious reference to your current LAN IPv4 whether this is another script or just to make sure.

Add a file to your ~/.local/bin folder, name it check-network, make it executable

mkdir -p  ~/.local/bin
touch ~/.local/bin/check-network
chmod +x ~/.local/bin/check-network

Edit the file and paste below content - be aware that the ttf_icons() does not display correct.

The function looks like this in Sublime Text. The font used is the package ttf-font-icons available in the repo.

20220129-153010_527x178_screenshot

#! /bin/bash
#
# Script for displaying local network address in polybar, tint2, conky etc.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
# @linux-aarhus - root.nix.dk
#
# for icons install the package ttf-font-icons from the repo
#

ttf_icons(){
    # set icons if font is available
    if [ -f '/usr/share/fonts/TTF/icons.ttf' ]; then
      wlan=''
      lan=''
      offline=''
    fi
}

lanip() {
    # parse output from ip a show $nic
    echo $(ip a show $1 | grep 'inet ' | head -n 4 | awk '{print $2}' | cut -d'/' -f1)
}

ttf_icons
# find active interface
nic=$(ip a | grep ' state UP' | cut -d' ' -f2 | cut -d':' -f1)
if [[ ${nic} != "" ]]; then
    # print IP address
    if [[ ${nic} == e* ]]; then
        echo ${lan} $(lanip ${nic})
    else
        echo ${wlan} $(lanip ${nic})
    fi
else
    # print offline
    echo ${offline} n/a
fi

6 Likes

Not as pretty, but the ip -o address command gives a nice summary of active addresses, including localhost, wifi, and ipv6.

You are right - in fact it is the parsed output from that command which provides the info.

What font am I missing that I get your script not properly displayed?
Screenshot_20220129_150451

sudo pacman -S ttf-font-icons

ttf-font-icons installed, rebooted - still the same… (using Firefox)

I used font-manager to display the font and I simply copied the character.

I don’t know why you cannot see the icons on the forum. I cannot either.

In an editor (Sublime Text) it looks fine.

20220129-153010_527x178_screenshot

If I copy it to kate - no improvement, in LibreOffice Writer it improves to:
Screenshot_20220129_152414

No big deal, anyway.

font weirdness :grin: and from what see now a double byte issue when pasting from the