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).