I am new to shell scripting and trying to create a shell script which will extract and print only ip address from ifconfig command and not all the information. I have two issues here -
- To know active interface like eth0, wl0, etc.
- I guess `route` command will give me interface name in last column.
So I wrote below code -
info=`route`
echo $info
route command gives output as -
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.0.1 0.0.0.0 UG 600 0 0 wlp3s0
link-local * 255.255.0.0 U 1000 0 0 wlp3s0
192.168.0.0 * 255.255.255.0 U 600 0 0 wlp3s0
Now I am not sure how to extract wlp3s0
from this output.
2. when I am able to get this wlp3s0 from above output, I will run -
ifconfig wlp3s0
which will give me below output -
wlp3s0 Link encap:Ethernet HWaddr 50:b7:c3:d9:b9:df
inet addr:192.168.0.9 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::a5d8:9c7b:c23f:116/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:58167 errors:0 dropped:0 overruns:0 frame:0
TX packets:44103 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:42208037 (42.2 MB) TX bytes:8352741 (8.3 MB)
Again I would want to extract 192.168.0.9 from this output and print it.
Any help would be greatly appreciated.