I want to get my netmask on Linux. It is outputted with
ifconfig but I want to extract the string.
I should clarify that the code here works for Linux, (note comments and post about other Unices). OP asked for a Linux solution, but it would be good to change the question to "how to get netmask" in general, and have the answer combine the best way for more Unix flavors.
About ifconfig in Linux
Along with other traditional networking commands like netstat, arp, rarp and route, ifconfig is part of the net-tools package. Net-tools hasn't been actively developed from a really long time and there are efforts to deprecate it in favor of the newer iproute2 package. For the sake of brevity, if you want more details on this fundamental transition, here are some relevant links:
if you're using an OS that outputs the mask in hex, you can do something like:
I wonder if you really only want the mask. Maybe you really want the network (or IP) with mask, to use in config files, with
In that case, on Linux, you can also parse the output of the
To list all interfaces with their address and mask in CIDR notation:
Or to restrict it to the default interface:
which gives me "192.168.1.61/24" on my machine.