Skip to content

Instantly share code, notes, and snippets.

@rrbutani
Last active January 15, 2020 02:59
Show Gist options
  • Save rrbutani/fa1fac4dbe22492b9cdf2c3ea4d1bb0d to your computer and use it in GitHub Desktop.
Save rrbutani/fa1fac4dbe22492b9cdf2c3ea4d1bb0d to your computer and use it in GitHub Desktop.
/usr/share/openmediavault/mkconf `ifconfig` patch
#!/usr/bin/env bash
IFACE=__
# Existing:
d_get_ipv4_definition()
{
ifconfig "$1" | grep "inet addr:"
}
d_get_ip()
{
d_get_ipv4_definition "$1" | cut -d: -f2 | cut -d" " -f1
}
d_get_mask()
{
d_get_ipv4_definition "$1" | cut -d: -f4 | cut -d" " -f1
}
d_get_subnet()
{
local ip=$(d_get_ip "$1")
local mask=$(d_get_mask "$1")
local IFS='.' subnet i
local -a oct msk
read -ra oct <<< "$ip"
read -ra msk <<< "$mask"
for i in ${!oct[@]}; do
subnet+=( "$(( oct[i] & msk[i] ))" )
done
echo "${subnet[*]}"
}
d_get_ipv4_definition "${IFACE}"
d_get_ip "${IFACE}"
d_get_mask "${IFACE}"
d_get_subnet "${IFACE}"
# Modified:
get_ipv4_definition()
{
ifconfig "$1" | grep "inet " | xargs
}
get_ip()
{
get_ipv4_definition "$1" | cut -d' ' -f2
}
get_mask()
{
get_ipv4_definition "$1" | cut -d' ' -f4
}
get_subnet()
{
local ip=$(get_ip "$1")
local mask=$(get_mask "$1")
local IFS='.' subnet i
local -a oct msk
read -ra oct <<< "$ip"
read -ra msk <<< "$mask"
for i in ${!oct[@]}; do
subnet+=( "$(( oct[i] & msk[i] ))" )
done
echo "${subnet[*]}"
}
echo
get_ipv4_definition "${IFACE}"
get_ip "${IFACE}"
get_mask "${IFACE}"
get_subnet "${IFACE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment