| 1 | #!/bin/bash |
| 2 | # Authors: Steven Shiau <steven _at_ clonezilla org>, Ceasar Sun <ceasar _at_ nchc org tw> |
| 3 | # License: GPL |
| 4 | # Description: Get the netmask of network card interface |
| 5 | |
| 6 | # Load DRBL setting and functions |
| 7 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 8 | |
| 9 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 10 | |
| 11 | |
| 12 | ethx=$1 |
| 13 | export LC_ALL=C |
| 14 | |
| 15 | Usage() { |
| 16 | echo "Get the netmask of network card interface" |
| 17 | echo "Usage:" |
| 18 | echo "$(basename $0) INTERFACE" |
| 19 | echo "Ex: $(basename $0) eth0" |
| 20 | } |
| 21 | |
| 22 | [ -z "$ethx" ] && Usage && exit 1 |
| 23 | if ! LC_ALL=C ip link show dev $ethx >/dev/null 2>&1; then |
| 24 | exit 1 |
| 25 | fi |
| 26 | _cidr="$(LC_ALL=C ip addr show dev $ethx 2>/dev/null | awk '/inet / {print $2}' | cut -d/ -f2 | head -n1)" |
| 27 | if [ -n "$_cidr" ]; then |
| 28 | _val=$(( 0xffffffff ^ ((1 << (32 - _cidr)) - 1) )) |
| 29 | echo "$(( (_val >> 24) & 255 )).$(( (_val >> 16) & 255 )).$(( (_val >> 8) & 255 )).$(( _val & 255 ))" |
| 30 | else |
| 31 | exit 1 |
| 32 | fi |
| 33 | exit 0 |