Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux bash script to extract IP address

Tags:

linux

bash

sed

awk

I want to make big script on my Debian 7.3 ( something like translated and much more new user friendly enviroment ). I have a problem. I want to use only some of the informations that commands give me. For example my ifconfig looks like:

eth0      Link encap:Ethernet  HWaddr 08:00:27:a3:e3:b0             inet addr:192.168.1.103  Bcast:192.168.1.255  Mask:255.255.255.0           inet6 addr: fe80::a00:27ff:fea3:e3b0/64 Scope:Link           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1           RX packets:1904 errors:0 dropped:0 overruns:0 frame:0           TX packets:2002 errors:0 dropped:0 overruns:0 carrier:0           collisions:0 txqueuelen:1000            RX bytes:1309425 (1.2 MiB)  T 

I want to display only the IP address in line: echo "Your IP address is: (IP_ADDRESS )". Is there any command that allow me to do such a thing, to search in stream for informations I want to get?. I know about grep and sed but I am not really good with them.

Edit: Firstly to say thank you for helping me with this problem, now I know much more. Secondly to say project is in progress. If anyone would be interested in it just pm me.

like image 931
user3232381 Avatar asked Jan 24 '14 15:01

user3232381


People also ask

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

Instead, you could just use this: hostname --all-ip-addresses or hostname -I , which does the same thing (gives you ALL IP addresses of the host).

How do I use grep to find an IP address?

so you can use grep -oE "\b([0-9]{1,3}\.){ 3}[0-9]{1,3}\b" to grep the ip addresses from your output. Thanks. This works.


2 Answers

If the goal is to find the IP address connected in direction of internet, then this should be a good solution.

Edit 2021: Added "sed" and "grep" versions and new "awk" versions (some are gnu)


To find what IP adders is used connected to internet, we can use the ip route command. With newer version of Linux you get more information with a typical output some like this:

ip route get 8.8.8.8 8.8.8.8 via 10.36.15.1 dev ens160 src 10.36.15.150 uid 1002     cache 

so to get IP you need to find the IP after src, using awk, sed or grep

ip route get 8.8.8.8 | awk -F"src " 'NR==1{split($2,a," ");print a[1]}' ip route get 8.8.8.8 | awk 'match($0,/src (\S*)/,a)&&$0=a[1]' ip route get 8.8.8.8 | awk '{for(i=1;i<=NF;i++)if($i~/src/)$0=$(i+1)}NR==1'  ip route get 8.8.8.8 | sed -E 's/.*src (\S+) .*/\1/;t;d' ip route get 8.8.8.8 | sed 's/.*src \([^ ]*\).*/\1/;t;d' ip route get 8.8.8.8 | sed  -nE '1{s/.*?src (\S+) .*/\1/;p}'  ip route get 8.8.8.8 | grep -oP 'src \K[^ ]+' 10.36.15.150 

and if you like the interface name using awk, sed or grep

ip route get 8.8.8.8 | awk -F"dev " 'NR==1{split($2,a," ");print a[1]}' ip route get 8.8.8.8 | awk 'match($0,/dev (\S*)/,a)&&$0=a[1]' ip route get 8.8.8.8 | awk '{for(i=1;i<=NF;i++)if($i~/dev/)$0=$(i+1)}NR==1'  ip route get 8.8.8.8 | sed -E 's/.*?dev (\S+) .*/\1/;t;d' ip route get 8.8.8.8 | sed 's/.*dev \([^ ]*\).*/\1/;t;d' ip route get 8.8.8.8 | sed  -nE '1{s/.*?dev (\S+) .*/\1/;p}'  ip route get 8.8.8.8 | grep -oP 'dev \K[^ ]+' ens192 

ip route does not open any connection out, it just shows the route needed to get to 8.8.8.8. 8.8.8.8 is Google's DNS.

If you like to store this into a variable, do:

my_ip=$(ip route get 8.8.8.8 | awk -F"src " 'NR==1{split($2,a," ");print a[1]}')  my_interface=$(ip route get 8.8.8.8 | awk -F"dev " 'NR==1{split($2,a," ");print a[1]}') 

Why other solution may fail:

ifconfig eth0

  • If the interface you have has another name (eno1, wifi, venet0 etc)
  • If you have more than one interface
  • IP connecting direction is not the first in a list of more than one IF

Hostname -I

  • May get only the 127.0.1.1
  • Does not work on all systems.
  • Give all IP not only the one connected to internet. -I, --all-ip-addresses all addresses for the host
like image 102
Jotne Avatar answered Oct 04 '22 13:10

Jotne


To just get your IP address:

echo `ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'` 

This will give you the IP address of eth0.

Edit: Due to name changes of interfaces in recent versions of Ubuntu, this doesn't work anymore. Instead, you could just use this:

hostname --all-ip-addresses or hostname -I, which does the same thing (gives you ALL IP addresses of the host).

like image 35
Marco Hegenberg Avatar answered Oct 04 '22 13:10

Marco Hegenberg