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.
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).
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.
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
Hostname -I
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With