I am trying to write a bash
script to get all IP addresses on a server. The script should work on all major distros. Here is what I have:
ifconf
I would to introduce you to a command-line tool called OSQuery by Facebook which helps you get system info by making SQL-like queries. For your case for instance, you would have enter;
osquery> select * from interface_addresses;
Which 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 convenient.
ifconfig | grep 'inet addr:' | awk {'print $2'} | cut -d ":" -f 2
if it is only the "addr:" word that you'd like to remove I would use sed
instead of awk
like
ifconfig | grep 'inet addr:' | awk {'print $2'}| grep -v 127.0.0.1 | sed -e 's/addr://'
ifconfig | grep 'inet addr:' | awk {'print $2'} | awk 'BEGIN{FS=":"}{print $2}' | grep -v '127.0.0.1'
There's no need for grep
. Here's one way using awk
:
List only addr:
ifconfig | awk -F "[: ]+" '/inet addr:/ { if ($4 != "127.0.0.1") print $4 }'
List device 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 }'
Here is a similiar script for what I have tested to get an ip-range of addresses, but it is slowly - somebody might give a hint, how to accelerate this ? (the ip-range here is an example for to get all lines, who seems 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
done
If this is too trivial or some mistake in it here, simply answer or comment.
This script would last until finish about 5 to 8 hours.