Set an awk,sed output to a variable in Bash - linux

UPDATE:
Now I have a simpler file to handle, similar to this:
127.0.0.1 25 127.0.0.1
127.0.0.1 25 127.0.0.1
127.0.0.1 32828 127.0.0.1
127.0.0.1 32830 127.0.0.1
127.0.0.1 32906 127.0.0.1
127.0.0.1 32908 127.0.0.1
127.0.0.1 32984 127.0.0.1
(first column ip_local, second column port_local and last column ip_foreign)
All I have to do is to send the awk output to a variable.
The code I managed to write is this, but I still have problems to process the text file...
#!/bin/sh
for i in `cat DEV0_IPsListSep.txt`;
do
ip_local=$(awk '{print $1}' $i);
port_local=$(awk '{print $2}' $i);
ip_foreign=$(awk '{print $3}' $i);
if [$port_local < 3200] || [$ip_foreign != 127.0.0.1] || [$ip_foreign !=
192.168.1.1]
then
echo $ip_foreign >> IPFinalList.txt;
fi
done
I have a bash script that processes a file with two columns. The first column is the local ip addresses, the other is the one of the foreign addresses.
Example:
127.0.0.1:25 127.0.0.1:33862
127.0.0.1:25 127.0.0.1:36498
127.0.0.1:25 127.0.0.1:37338
127.0.0.1:25 127.0.0.1:37410
127.0.0.1:25 127.0.0.1:38320
127.0.0.1:25 127.0.0.1:39428
127.0.0.1:25 127.0.0.1:39514
127.0.0.1:25 127.0.0.1:39768
127.0.0.1:25 127.0.0.1:39846
127.0.0.1:25 127.0.0.1:40376
I would like the script to assign the outputs of sed and awk commands to my custom variables, to separate IPs and ports.
I tried to create a script but still receive syntax errors...
#!/bin/sh
for i in `IPslist.txt`;
ipLocal=$(awk '{print $1}' | sed -e 's/:.*//' $i)
portLocal=$(awk '{print $1}' | sed -e 's/.*://' $i)
ipForeign=$(awk '{print $2}' | sed -e 's/:.*//' $i)
portForeign=$(awk '{print $2}' | sed -e 's/.*://' $i)
if [ $portLocal < 3200 ] || [ $ipForeign != 127.0.0.1 ] || [
$ipForeign != 192.168.1.1 ]
then
echo $ipForeign >> IPFinalList.txt
fi

With single awk process (optimized solution):
awk -F':|[[:space:]]+' '$2<3200 || $3!= 127.0.0.1 || $3!= 192.168.1.1' IPslist.txt > IPFinalList.txt

Related

script for checking IP's hostname from file

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

Shell Script: Get eth name from Subnet mask

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 to place the output of an echo statement and an arp statement on one line?

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}')

find ip address of my system for a particular interface with shell script (bash)

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=" ")

Bash script to get all IP addresses

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

Resources