Add get-lan-ip script

This commit is contained in:
Christopher
2023-09-19 13:04:26 -05:00
parent c6a180bf16
commit c9718a1ba1
2 changed files with 37 additions and 0 deletions

9
get-lan-ip/README.md Normal file
View File

@@ -0,0 +1,9 @@
## Why?
This is a handy script to get the server's primary IP address.
## How to use?
```bash
bash -c "$(wget -qLO - https://raw.githubusercontent.com/bigbeartechworld/big-bear-scripts/master/get-lan-ip/run.sh)"
```

28
get-lan-ip/run.sh Normal file
View File

@@ -0,0 +1,28 @@
#!/bin/bash
# This script attempts to find the server's primary IP address.
# It first tries using 'ifconfig' and then falls back to 'ip addr' if 'ifconfig' doesn't produce a result.
# Identify the primary network interface by checking the default route.
primary_interface=$(ip route | grep default | awk '{print $5}')
# Try to retrieve the server's primary IP address using ifconfig.
ifconfig_ip=$(ifconfig $primary_interface | grep -oE 'inet (10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|172\.(1[6-9]|2[0-9]|3[0-1])\.[0-9]{1,3}\.[0-9]{1,3}|192\.168\.[0-9]{1,3}\.[0-9]{1,3})' | awk '{print $2}')
# Check if the result from ifconfig is empty.
# If it is, try to use 'ip addr' to retrieve the IP.
if [ -z "$ifconfig_ip" ]; then
# Try to retrieve the server's primary IP address using 'ip addr'.
ip_ip=$(ip addr show $primary_interface | grep -oE 'inet (10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|172\.(1[6-9]|2[0-9]|3[0-1])\.[0-9]{1,3}\.[0-9]{1,3}|192\.168\.[0-9]{1,3}\.[0-9]{1,3})' | awk '{print $2}')
# Check if the result from 'ip addr' is non-empty.
# If it is, print the IP. Otherwise, print an error message.
if [ -n "$ip_ip" ]; then
echo "Server IP (via ip): $ip_ip"
else
echo "Unable to determine server IP"
fi
else
# If the result from 'ifconfig' was non-empty, print the IP.
echo "Server IP (via ifconfig): $ifconfig_ip"
fi