Hello,
I am reaching out to the pro's here. In one of my network bash scripts, I need to be able to print only the first three octets of on IP address. Here is what I have so far:
Code:
IP_3OCT=`ifconfig $1 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}' | sed /127.0.0.1/d | cut -c 1-11`
This would trim off the last octet for 192.168.10.100, giving me:
192.168.10.
but I need this to be universal enough to handle any class of IP address...say a class A IP of 10.100.15.10? This line would give me:
10.100.15.1
I still need that last "1" trimmed. I am sure there is a better way to do this, so I don't have to modify the script itself each and every time I need to do this task.
btw...if anyone was wondering why I am removing the loopback, or why it is even coming up at all...this is the only way I could figure out how to get just the static IP from the ifconfig. If there a better way for that, then I am all ears (ps...I tried using hostname -i, but when this script runs, the hosts file isn't set up yet and this defaults to the loopback).
Any help is most appreciated!