Bash script to get all IP addresses

I am trying to write a bash script to get all the IP addresses on the server. The script should work on all major distributions. Here is what I have:

 ifconfig | grep 'inet addr:' | awk {'print $2'} 

Result:

 addr:10.1.2.3 addr:50.1.2.3 addr:127.0.0.1 

How can I remove the addr: prefix first? Secondly, how can I exclude 127.0.0.1 ?

+7
linux bash shell
source share
12 answers

No grep needed. Here is one way: awk :

The list is only addr:

 ifconfig | awk -F "[: ]+" '/inet addr:/ { if ($4 != "127.0.0.1") print $4 }' 

List of devices and addr:

 ifconfig | awk -v RS="\n\n" '{ for (i=1; i<=NF; i++) if ($i == "inet" && $(i+1) ~ /^addr:/) address = substr($(i+1), 6); if (address != "127.0.0.1") printf "%s\t%s\n", $1, address }' 
+7
source share

ifconfig was outdated ip . It also has the -o flag, which easily writes output. Use ip -4 to display only IPV4 addresses. Pay attention to a simpler script, it already excludes the loopback address:

 ip -o addr | awk '!/^[0-9]*: ?lo|link\/ether/ {print $2" "$4}' 

Or if you do not want networks:

 ip -o addr | awk '!/^[0-9]*: ?lo|link\/ether/ {gsub("/", " "); print $2" "$4}' 
+12
source share

Just using hostname you can get a list of all your IP addresses using the -I flag.

i.e.

 $ hostname --all-ip-addresses || hostname -I 10.10.85.100 10.20.85.100 10.30.85.100 

then

 $ hostname --ip-address || hostname -i ::1%1 127.0.0.1 

Centos7 (k3.10.0)

+2
source share

Here is a similar script for what I tested to get the IP range of addresses, but this is slow - can someone give a hint how to speed this up? (ip-range here is an example in order to get all the lines that seem to be up - between Vancouver and Korea):

#! / Bin / bash

for ip in {209..210}. {125..206}. {5..231}. {65..72} # any ip between 209.126.230.71 and 210.205.6.66

do

printf '===% s === \ n' "$ ip"

whois "$ ip" β†’ /home/$user/test001.txt

to do

If this is too trivial or some kind of mistake here, just reply or comment.

This script will last until the end of about 5 to 8 hours.

+1
source share

It is simply a distillation of a few previous answers and comments. Sample output is included.

To list IP addresses:

Using ip :

(limited to IPv4 and global)

 $ /sbin/ip -4 -o addr show scope global | awk '{gsub(/\/.*/,"",$4); print $4}' 192.168.82.134 138.225.11.92 138.225.11.2 

Using ifconfig :

(Excluding 127.0.0.1)

 $ /sbin/ifconfig | awk -F "[: ]+" '/inet addr:/ { if ($4 != "127.0.0.1") print $4 }' 192.168.82.134 138.225.11.92 138.225.11.2 

To map IP addresses to host names, see this answer .

+1
source share

Use grep -v to ignore 127.0.0.1

 ifconfig | grep 'inet addr:' | awk {'print $2'} | grep -v '127.0.0.1' 

Use sed to edit "addr:"

 ifconfig | grep 'inet addr:' | awk {'print $2'} | grep -v '127.0.0.1' | sed -e 's/addr://' 
0
source share

ifconfig | grep 'inet addr:' | awk {'print $2'} | awk 'BEGIN{FS=":"}{print $2}' | grep -v '127.0.0.1'

0
source share

This is a very complicated solution, but it works:

ip a | awk ' !/[0-9]+\: lo/ && /^[0-9]\:/ || /inet / && !/127\.0\.0\.1/ {print $2}'

Output:

eth0: 192.168.0.1/24

Even better:

ip a | awk ' !/[0-9]+\: lo/ && /^[0-9]\:/ || /inet / && !/127\.0\.0\.1/ {print $2}' | perl -i -pe "s/:\n/: /g;" -pe "s/\/[\d]+$//"

Output:

eth0: 192.168.0.1

I don’t have a machine with several interfaces without a loop with which I can check, do not hesitate to publish your findings.

0
source share

if this is just the word "addr:" that you want to remove, I would use sed instead of awk , for example

 ifconfig | grep 'inet addr:' | awk {'print $2'}| grep -v 127.0.0.1 | sed -e 's/addr://' 
0
source share

I am always surprised that people use sed and awk instead of perl.

But first, using both grep and awk with an extra option, don't be shy about it:

 ifconfig | grep 'inet addr:' | awk {'print $2'} | awk -F: {'print $2'} | grep -v '127.0.0.1' 

replace awks with perl:

 ifconfig | grep 'inet addr:' | perl -F\\s\|: -ane 'print "$F[2]\n"' | grep -v '127.0.0.1' 

replacing greps within a single perl script:

 ifconfig | perl -F\\s\|: -ane 'next if !/^inet addr:/ or /127\.0\.0\.1/; print "$F[2]\n"' 

and finally just using the power of the perl regular expression:

 ifconfig | perl -ne 'next if !/inet addr:(?<ip>[0-9.]+)/ or $+{ip} == "127.0.0.1"; print "$+{ip}\n"' 
0
source share

I would introduce you to the Facebook OSQuery command-line tool , which will help you get system information by making SQL-like queries. For example, for your case you would enter;

 osquery> select * from interface_addresses; 

That would output something like;

 interface = wlan0 address = 192.168.0.101 mask = 255.255.255.0 broadcast = 192.168.0.255 point_to_point = 

Which I find a lot more neat and comfortable.

0
source share
 ifconfig | grep 'inet addr:' | awk {'print $2'} | cut -d ":" -f 2 
-one
source share

All Articles