1

I was updating my shell dot file, and I thought that it would be nice to have an alias that show just my ip. I usually connect with wifi, which is on wlan0 and it's the last "block" of devices.

So I've something like this:

wlan0     Link encap:Ethernet  HWaddr 6c:71:d9:d3:28:9d  
          inet addr:192.168.1.16  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::6e71:d9ff:fed3:289d/64 Scope:Link

And I want my Bcast Address.

With this command i can get it:

alias ip="ifconfig | grep Bcast | tail -n1 | cut -d' ' -f14 | cut -d':' -f2"

But I don't like this. Is there a more clean way to do it?

3
  • 2
    If you are certain that wlan0 is the interface you are using, the following does the trick: ifconfig wlan0|sed -n 's/^.*Bcast:\(.*\) .*$/\1/p'. Is this neater?
    – AFH
    Commented Jan 24, 2016 at 16:22
  • Yes, much better then using 4 commands. But could you please explain it? Thanks Commented Jan 24, 2016 at 16:30
  • I'll need to do it as an answer.
    – AFH
    Commented Jan 24, 2016 at 16:35

1 Answer 1

2

If you are certain that wlan0 is the interface, then you can return the information for just this interface with:

ifconfig wlan0

Now that you have only one line with Bcast: in it (eliminating tail) you can use sed to combine the grep and cut functions:

ifconfig wlan0 | sed -n 's/^.*Bcast:\(.*\) .*$/\1/p'

In this case, sed -n eliminates the routine printing of lines from input, and the search string matches the whole line, while marking what lies between Bcast: and the next space: the substitution is this marked field only, and /p overrides the -n and forces printing. Note that to assign this string to an alias you will need to use double quotes and escape some of the characters with back-slash, or use single quotes, replacing those in the above command by '\'' (though you can omit the resultant final '').

Finally, if you want to make sure that wlan0 is the interface you want, and there are no others configured for internet access, then you can find out which interface is being used (you may need to install networkctl):

networkctl status | sed -n 's/^.*Gateway: .* on \(.*$\)/\1/p'

This uses sed on the networkctl output similarly to before. Now you can combine them:

ifconfig $(networkctl status |\
  sed -n 's/^.*Gateway: .* on \(.*$\)/\1/p')|sed -n 's/^.*Bcast:\(.*\) .*$/\1/p'

This is hardly neat, but it covers most of the bases - in particular, if your wireless isn't working and you use Ethernet instead, it should still work. If you have both interfaces working together, it becomes more complicated still: I'll leave you to work it out (hint: you'll need to use /Gateway/,$ as the line range for the s command, and a way to exclude the IPv6 entries).

2
  • The output of ip -4 -o dev wlan0 may be easier to parse
    – Teun Vink
    Commented Jan 24, 2016 at 17:46
  • @TeunVink - My version of ip (Ubuntu 15.04) doesn't have dev as an object. The information is in ip -4 -o addr, but I don't think this is any easier to parse.
    – AFH
    Commented Jan 24, 2016 at 17:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .