get_ip.sh
This is a script for quickly obtaining local IP addresses in termux. Tested on termux. Current version v0.1.
Features
- Get IPv4 addresses of all network interfaces
- Display network interface names and corresponding IP addresses in a clear format
- Automatically check if ifconfig command is available
Usage
CNB:
bash
wget https://cnb.cool/SDCOM/shit/-/git/raw/main/script/get_ip.sh && sudo chmod +x ./get_ip.sh && sudo ./get_ip.shGithub:
bash
wget https://github.com/SDCOM-0415/shit/raw/refs/heads/main/script/get_ip.sh && sudo chmod +x ./get_ip.sh && sudo ./get_ip.shAfter running, the script will display all network interfaces with IPv4 addresses in the format: interface_name: IP_address
Script Content
bash
#!/bin/bash
echo "Get IP Version: v0.1"
echo "Author: SDCOM"
echo "CNB Project URL: https://cnb.cool/SDCOM/shit/-/blob/main/script/get_ip.sh"
echo "GitHub Project URL: https://github.com/SDCOM-0415/shit/blob/main/script/get_ip.sh"
# Script function: Get IP addresses of all network interfaces and output in specified format
# Output format: interface_name: IP_address
# Check if ifconfig command is available
if ! command -v ifconfig &> /dev/null; then
echo "Error: ifconfig command is not available, please install net-tools package"
exit 1
fi
# Get all network interface names
interfaces=$(ifconfig | grep -E '^[a-zA-Z0-9]+:' | awk -F: '{print $1}')
# Iterate through each interface and get its IP address
for interface in $interfaces; do
# Get IPv4 address
ip_addr=$(ifconfig $interface | grep -oP 'inet \K\d+\.\d+\.\d+\.\d+')
# If the interface has an IP address, output it
if [ ! -z "$ip_addr" ]; then
echo "$interface: $ip_addr"
fi
done
exit 0Working Principle
- Check if
ifconfigcommand is installed on the system - Use
ifconfigto get all network interface names - Iterate through each interface and extract its IPv4 address
- Output interface names and IP addresses in the specified format
Dependencies
net-toolspackage (providesifconfigcommand)
Notes
- If
ifconfigcommand is not installed, the script will prompt to install thenet-toolspackage - The script only displays interfaces with IPv4 addresses; interfaces without IP addresses will not be shown
- This script is mainly designed for termux environment but can also be used on other Linux systems
Project Repository
CNB: https://cnb.cool/SDCOM/shit/-/blob/main/script/get_ip.sh
Github: https://github.com/SDCOM-0415/shit/blob/main/script/get_ip.sh
© Author
SDCOM