Basically what I'm trying to do is pull the first three octets from the ipaddr out of ifconfig
ifconfig eth0 |awk -F" " '{ print $2 }'|grep addr: |awk -F":" '{ print $2 }'| awk -F"." '{print $1"."$2"."$3}'
keeps giving me
192.168.0
..
and I can not for the life of me get rid of the bottom half (the ..'s)
Scott
July 6, 2013, 10:33pm
2
You could probably add one more pipeline:
... | head -1
But there may be a shorter way depending on your OS.
I.e. in Redhat:
$ hostname -I
Which OS are you using?
root@bt:~# ifconfig eth0 |grep "inet addr" |awk -F":" '{print$2}'|awk -F"." '{print $1"."$2"."$3}'
192.168.0
root@bt:~# inet=$?
root@bt:~# echo "$inet"
0
my head is going to explode
---------- Post updated at 09:40 PM ---------- Previous update was at 09:39 PM ----------
Backtrack 5r3
Ubuntu
Scott
July 6, 2013, 10:41pm
4
You're testing the result of the command with $?, not the output. If you want to store the output:
inet=$(...)
i.e.
$ inet=$(ip addr show eth0 | awk -F"[ /]" '/inet /{print $6}')
$ echo $inet
I'm trying to trim off that last octet so I can input the variable inet.* and run a nmap on specific ports.
---------- Post updated at 09:48 PM ---------- Previous update was at 09:46 PM ----------
Wait a tick, I see what you did. Thank you!
Scott
July 6, 2013, 10:48pm
6
# ip addr show eth0 | awk -F"[ /.]" '/inet /{print $6"."$7"."$8}'
10.10.44
root@bt:~# inet=$(ifconfig eth0 |grep "inet addr" |awk -F":" '{print$2}'|awk -F"." '{print $1"."$2"."$3}')
root@bt:~# echo $inet
192.168.0
I've just started using awk. At the moment I only know how to grab fields. So the fancy things you're doing, I have no idea. This works though, thank's a bunch!
RudiC
July 7, 2013, 4:41pm
8
polishing your code snippet a bit:
inet=$(ifconfig eth0 | awk '/inet addr/ {gsub (/^.*:|\.[^.]*$/,"",$2); print $2}')
echo $inet
10.1.1
... as awk
can replace grep
in certain instances...