I used for to file one contain the IP with hostname like the /ec/hosts
and the other contain the IP and counter try to print the ip if hostname not found and if is found print the hostname.
Script:
for i in `cat ip | awk '{print $2}'` ;do
var=`grep "$i" Server_ip` |
awk ' {if($var == "") print $i else print $1}';
done
File 1
localhost 127.0.0.1
test 10.0.0.1
test1 10.0.0.2
File 2
3 127.0.0.1
2 10.0.0.1
1 10.0.0.2
4 10.0.0.3
5 10.0.0.4
Desired output
localhost
test
test1
10.0.0.3
10.0.0.4
You can try this;
#!/bin/bash
for i in `cat ip | awk '{print $2}'` ;do
var=`awk -v ip=${i} '$2 == ip {print $1}' Server_ip`
if [ -z "$var" ]; then
echo $i
else
echo "$var"
fi
done
Eg;
user#host:/tmp/test$ cat Server_ip
localhost 127.0.0.1
test 10.0.0.1
test1 10.0.0.2
user#host:/tmp/test$ cat ip
3 127.0.0.1
2 10.0.0.1
1 10.0.0.2
4 10.0.0.3
5 10.0.0.4
user#host:/tmp/test$ ./test.sh
localhost
test
test1
10.0.0.3
10.0.0.4
My machine has multiple ethernet device such as eth0, eth1, etc. One of those IP will have an IP in the range 192.168.x.x. How can I fetch the device name using shell script? (Preferably using ip commands rather than ifconfig)
Like
eth0 192.168.2.3
or
eth3 192.168.5.6
you can try this;
ip -o -4 a | awk '$2 ~ "eth" { gsub(/\/.*/, "", $4); print $2" "$4}'
to all interfaces;
ip -o -4 a | awk ' { gsub(/\/.*/, "", $4); print $2" "$4}'
eg:
user#host:/tmp/$ ip -o -4 a | awk ' { gsub(/\/.*/, "", $4); print $2" "$4}'
lo 127.0.0.1
eth0 x.x.x.x
docker0 x.x.x.x
user#host:/tmp/$ ip -o -4 a | awk '$2 ~ "eth" { gsub(/\/.*/, "", $4); print $2" "$4}'
eth0 x.x.x.x
man ip said :
-o, -oneline
output each record on a single line, replacing line feeds with the '\' character. This is convenient when you want to
count records with wc(1) or to grep(1) the output.
-4 a : Only show TCP/IP IPv4
How can I get the output "echo" and "macaddress" on one line?
This is what I've got:
ipRange="192.168.0."
macaddress= arp | grep -w "$ipRange$1" | awk '{print $3,$1}'
ping -c1 "$ipRange$1" > /dev/null
if [ $? -eq 0 ]; then
echo "deze host met mac address en ip address is up $macaddress"
else
echo "het is down"
fi
This is the output:
VirtualBox ~ $ bash test2.sh 149
e0:b9:a5:f8:24:c3 192.168.0.149
deze host met mac address en ip address is up
this should do output in single line
ipRange="192.168.0."
macaddress=$(arp | grep -w "$ipRange$1" | awk '{print $3,$1}')
ping -c1 "$ipRange$1" > /dev/null
if [ $? -eq 0 ]; then
echo "deze host met mac address en ip address is up $macaddress"
else
echo "het is down"
fi
Just replace macaddress= arp | grep -w "$ipRange$1" | awk '{print $3,$1}'
with macaddress=$(arp | grep -w "$ipRange$1" | awk '{print $3,$1}')
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
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
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=" ")
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:
ifconfig | grep 'inet addr:' | awk {'print $2'}
Resulting in:
addr:10.1.2.3
addr:50.1.2.3
addr:127.0.0.1
How can I first remove the addr: prefix? Second, how I can exclude 127.0.0.1?
ifconfig was obsoleted by ip. It also has the flag -o that write outputs easy to parse. Use ip -4 to show only IPV4 addresses. Note the simpler script, it already exclude the loopback address:
ip -o addr | awk '!/^[0-9]*: ?lo|link\/ether/ {print $2" "$4}'
Or if you don't want the networks:
ip -o addr | awk '!/^[0-9]*: ?lo|link\/ether/ {gsub("/", " "); print $2" "$4}'
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 }'
Simply 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
whereas
$ hostname --ip-address || hostname -i
::1%1 127.0.0.1
Centos7 (k3.10.0)
This is merely a distillation of several prior answers and comments. Sample output is included.
To list IPs:
Using ip:
(Restricted 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 IPs to hostnames, see this answer.
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.
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 out the 'addr:'
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'
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://'
It's a very tricky 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
Better yet:
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 non loopback interfaces I can check it with, feel free to post your findings.
I'm always surprised to see people using sed and awk instead of perl.
But first, using both grep and awk with an extra option, feel free to just:
ifconfig | grep 'inet addr:' | awk {'print $2'} | awk -F: {'print $2'} | grep -v '127.0.0.1'
replacing the awks with perl:
ifconfig | grep 'inet addr:' | perl -F\\s\|: -ane 'print "$F[2]\n"' | grep -v '127.0.0.1'
replacing the greps within the same perl script:
ifconfig | perl -F\\s\|: -ane 'next if !/^inet addr:/ or /127\.0\.0\.1/; print "$F[2]\n"'
and lastly just using the power of perl's regex:
ifconfig | perl -ne 'next if !/inet addr:(?<ip>[0-9.]+)/ or $+{ip} == "127.0.0.1"; print "$+{ip}\n"'
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