Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find ip address of my system for a particular interface with shell script (bash)

I am trying to find ip-address of my own system through a shell script and write into a text thats my script content

#!/bin/bash

wifiip=$(ip addr | grep inet | grep wlan0 | awk -F" " '{print $2}'| sed -e 's/\/.*$//')

eth0ip=$(ip addr | grep inet | grep eth0 | awk -F" " '{print $2}' | sed -e 's/\/.*$//')

if [ "$eth0ip" == "0" ]; then

    echo "$eth0ip" | grep [0-9]$ > /home/pi/att/ip.txt

else 

    echo "$wifiip" | grep [0-9]$ > /home/pi/att/ip.txt

fi

and trying to do something like if one interface is not up print another ip in ip.txt

but it's giving

ip.sh: 14: [: unexpected operator 
like image 389
Ayush joshi Avatar asked Oct 07 '13 12:10

Ayush joshi


People also ask

How do I find the IP address of my interface?

To get a detailed listing of all the IP-related characteristics of an interface, use the show ip interface command. A common use for this command is to view any secondary addresses that have been assigned to an interface (they do not show up in the standard show interface output).

How do I find the IP address of a bash script?

To find out the IP address of Linux/UNIX/*BSD/macOS and Unixish system, you need to use the command called ifconfig on Unix and the ip command or hostname command on Linux. These commands used to configure the kernel-resident network interfaces and display IP address such as 10.8. 0.1 or 192.168. 2.254.

Which Linux command is used to find your systems IP address?

Using the ifconfig Command The system will display all network connections – including connected, disconnected, and virtual. Look for the one labeled UP, BROADCAST, RUNNING, MULTICAST to find your IP address. This lists both IPv4 and IPv6 addresses.

How do I find my eth1 IP address in Linux?

You can use the ifconfig command or ip command with grep command and other filters to find out an IP address assigned to eth0 and display it on screen.


2 Answers

Let's clean up your code first. You don't need chains of a dozen different commands and pipes when you're already using awk. This:

wifiip=$(ip addr | grep inet | grep wlan0 | awk -F" " '{print $2}'| sed -e 's/\/.*$//')

can be written simply as this:

wifiip=$(ip addr | awk '/inet/ && /wlan0/{sub(/\/.*$/,"",$2); print $2}')

but your whole script can be written as just one awk command.

I need you to update your question with some sample output of the ip addr command, the output you want from the awk command given that input, and explain more clearly what you're trying to do in order to show you the correct way to write that but it might be something like this:

ip addr | awk '
/inet/ { ip[$NF] = $2; sub(/\/.*$/,"",ip[$NF]) }
END { print ( "eth0" in ip ? ip["eth0"] : ip["wlan0"] ) }
' > /home/pi/att/ip.txt
like image 194
Ed Morton Avatar answered Sep 25 '22 09:09

Ed Morton


Here is a nice way to get your IP address. This gives you the address used to reach the internet at the test, so it will give you correct IP even if you change from Wifi to eth or to any other IF type.

See more detailed post here: Linux bash script to extract IP address

my_ip=$(ip route get 8.8.8.8 | awk '/8.8.8.8/ {print $NF}')

To get interface name:

my_if=$(ip route get 8.8.8.8 | awk '/dev/ {f=NR} f&&NR-1==f' RS=" ")
like image 40
Jotne Avatar answered Sep 25 '22 09:09

Jotne