The following script:
IP=`ifconfig en0 inet | grep inet | sed 's/.*inet *//; s/ .*//'`
isolates the IP address from ipconfig
command and puts it into the variable $IP
.
How can I now isolate the last octet from the said IP address and put it in a second variable - $CN
for instance:
$IP = 129.66.128.72 $CN = 72 or $IP = 129.66.128.133 $CN = 133...
Use "cut" command with . delimiter:
IP=129.66.128.72
CN=`echo $IP | cut -d . -f 4`
CN now contains the last octet.
In BASH you can use:
ip='129.66.128.72'
cn="${ip##*.}"
echo $cn
72
Or using sed for non BASH:
cn=`echo "$ip" | sed 's/^.*\.\([^.]*\)$/\1/'`
echo $cn
72
Using awk
cn=`echo "$ip" | awk -F '\\.' '{print $NF}'`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With