howtouselinux

Bash Shell Script to Find IP address

Table of Contents

A network interface is a point of interaction between a computer and a network. It can be a physical device, such as an Ethernet card or wireless adapter, or a virtual interface created by software.

An IP address is a numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication. IP addresses serve two main functions: identifying the host or network interface, and providing the location of the host in the network.

Together, network interfaces and IP addresses allow devices to communicate and transfer data over a network.

It is worth noting that a device can have multiple network interfaces and each interface can have different IP address, but they are all unique within the network they are connected to.

Find IP address using shell script

Here is a simple shell script that uses the ip command to retrieve the IP address of the host on interface eth0:

#!/bin/bash
# Store the IP address in a variable
IP_ADDRESS=$(ip addr show eth0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)
# Print the IP address
echo "The IP address is: $IP_ADDRESS"

You can run the script by making it executable and then executing it:

chmod +x get_ip_address.sh
./get_ip_address.sh

This script uses the ip addr command to retrieve information about the network interfaces and then filters that information to show only the IP address of the interface eth0. The IP address is then stored in a variable and printed to the screen.

You can also use ifconfig command instead of ip command, ifconfig can also be used to get the IP address of the host.

#!/bin/bash
# Store the IP address in a variable
IP_ADDRESS=$(ifconfig eth0 | grep "inet" | awk '{print $2}' | cut -d: -f2)
# Print the IP address
echo "The IP address is: $IP_ADDRESS"

Please note that this script is just an example and may not work on all systems, you may have to modify it to work on your specific system.

Find IP addresses on all network interfaces using shell script

Here is a shell script that uses the ip command to retrieve all network interfaces and their associated IP addresses:

#!/bin/bash
# Get a list of all network interfaces
INTERFACES=$(ip addr | grep ^[0-9] |awk '{print $2}'|cut -d: -f1)
# Loop through each interface and get its IP address
for INTERFACE in $INTERFACES
do
    IP_ADDRESS=$(ip addr show $INTERFACE | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)
    echo "Interface: $INTERFACE, IP address: $IP_ADDRESS"
done

You can run the script by making it executable and then executing it:

chmod +x get_interfaces_ip.sh
./get_interfaces_ip.sh

This script uses the ip addr command to retrieve information about all the network interfaces, and then filters that information to show only the interface name and the IP address.
The script then loops through each interface and prints the interface name and the IP address.

Welcome to howtouselinux.com!

Our website is dedicated to providing comprehensive information on using Linux.

We hope you find our site helpful and informative, and we welcome your feedback and suggestions for future content.

Learn More

Facebook
Twitter
LinkedIn