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
Related
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
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
I am using bash to get the IP address of my machine with that script:
_MyGW="$( ip route get 8.8.8.8 | awk 'N=3 {print $N}' )"
And now I am trying to get the Subnet Mask in this type:
192.168.1.0/24
But I have no idea how can I do that.
there are couple of ways to achieve this:
first: to print the mask in format 255.255.255.0, you can use this:
/sbin/ifconfig wlan0 | awk '/Mask:/{ print $4;} '
second: we can use ip command to get the mask in format 192.168.1.1/24
ip -o -f inet addr show | awk '/scope global/ {print $4}'
A better approach will be:
ifconfig eth0 | awk '/netmask/{split($4,a,":"); print a[1]}'
You can substitute the eth0 with any other interface you want
A simple way of doing it for me, was:
IP=$(ifconfig eth0 | grep -w inet | cut -d" " -f10) # device IP, e.g. 11.1.1.43
IP_RANGE=$(echo $IP | cut -d"." -f1-3).0/24 # subnet 11.1.1.0/24
Replace of course eth0 with the right interface diplayed by ifconfig.
That's how I get the IP and subnet mask with bash/awk:
IFCONFIG=$(ifconfig eth0)
IPETH=$(echo "$IFCONFIG" | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
MASK=$(echo "$IFCONFIG" | awk '/Mask/{split($4,a,":"); split(a[2],m,"."); h=m[1]*16777216+m[2]*65536+m[3]*256+m[4]; s=0; for(i=0; i < 32; i++) { s+=and(h,1); h/=2 } print s; }')
echo ${IPETH}/${MASK}
Depending on your version of ifconfig you must use /Mask/ or /netmask/ to get the subnet mask. I need this bit fiddling because I don't have ip on my system.
This gives for me e.g.
172.29.11.12/24
INTERFACE=$(ip -o -f inet route |grep -e "^default" |awk '{print $5}')
echo $(ip -o -f inet addr show | grep "$INTERFACE" | awk '/scope global/ {print $4}')
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'm trying to separate the ifconfig and give me the output just the current IP, broadcast, and netmask. The problem I'm running into is that the output is in spaces and in colons. I must be missing something, because I can't seem to get around this part. It doesn't seem that my OFS statement is working.
ifconfig eth0 | grep "inet addr" | awk -F ':' 'BEGIN{OFS=" ";}{print $2}'
Outputs this:
127.0.0.1 Bcast
The above is the IP of the unit. I really just want it to output
127.0.0.1
Some easy solutions:
ifconfig eth0 | tr : \ | awk '/inet addr/{ print $3 }'
and
ifconfig eth0 | tr -s \ | awk '/inet addr/{print $4 }' FS=':| '
also:
ifconfig eth0 | awk '/inet addr/{print $13 }' FS=':| '
If you know that you will always have a static string, then maybe just a substitution will work for you?
ifconfig eth0 | awk '/inet addr/{ sub(/addr:/, "", $2); print $2; }'
You can also use sed:
ifconfig eth0 | sed -n '/inet addr/{s/[^:]*:\([^ ]*\).*/\1/p}'