gistfile1.txt
· 1.3 KiB · Text
Surowy
#!/usr/bin/env bash
# ============================================
# IP Assignment
# ============================================
function ip::assigned() {
grep -h "^Address" "$(ctx::clients)"/*.conf 2>/dev/null \
| awk '{print $3}' \
| cut -d'/' -f1
}
function ip::is_assigned() {
local candidate="$1"
ip::assigned | grep -q "^${candidate}$"
}
function ip::next_for_type() {
local type="$1"
local subnet
subnet=$(config::subnet_for "$type")
if [[ -z "$subnet" ]]; then
log::error "Unknown device type: ${type}"
return 1
fi
for i in $(seq 1 254); do
local candidate="${subnet}.${i}"
if ! ip::is_assigned "$candidate"; then
echo "$candidate"
return 0
fi
done
log::error "No available IPs in subnet ${subnet}.0/24"
return 1
}
# ============================================
# Validation
# ============================================
function ip::is_valid() {
local ip="$1"
[[ "$ip" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}(/([0-9]|[1-2][0-9]|3[0-2]))?$ ]]
}
function ip::is_cidr() {
[[ "$1" == *"/"* ]]
}
function ip::validate() {
local ip="$1"
if ! ip::is_valid "$ip"; then
log::error "Invalid IP or CIDR: ${ip}"
return 1
fi
return 0
}
function ip::require_valid() {
local ip="$1"
ip::validate "$ip" || exit 1
}
| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # ============================================ |
| 4 | # IP Assignment |
| 5 | # ============================================ |
| 6 | |
| 7 | function ip::assigned() { |
| 8 | grep -h "^Address" "$(ctx::clients)"/*.conf 2>/dev/null \ |
| 9 | | awk '{print $3}' \ |
| 10 | | cut -d'/' -f1 |
| 11 | } |
| 12 | |
| 13 | function ip::is_assigned() { |
| 14 | local candidate="$1" |
| 15 | ip::assigned | grep -q "^${candidate}$" |
| 16 | } |
| 17 | |
| 18 | function ip::next_for_type() { |
| 19 | local type="$1" |
| 20 | local subnet |
| 21 | subnet=$(config::subnet_for "$type") |
| 22 | |
| 23 | if [[ -z "$subnet" ]]; then |
| 24 | log::error "Unknown device type: ${type}" |
| 25 | return 1 |
| 26 | fi |
| 27 | |
| 28 | for i in $(seq 1 254); do |
| 29 | local candidate="${subnet}.${i}" |
| 30 | if ! ip::is_assigned "$candidate"; then |
| 31 | echo "$candidate" |
| 32 | return 0 |
| 33 | fi |
| 34 | done |
| 35 | |
| 36 | log::error "No available IPs in subnet ${subnet}.0/24" |
| 37 | return 1 |
| 38 | } |
| 39 | |
| 40 | # ============================================ |
| 41 | # Validation |
| 42 | # ============================================ |
| 43 | |
| 44 | function ip::is_valid() { |
| 45 | local ip="$1" |
| 46 | [[ "$ip" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}(/([0-9]|[1-2][0-9]|3[0-2]))?$ ]] |
| 47 | } |
| 48 | |
| 49 | function ip::is_cidr() { |
| 50 | [[ "$1" == *"/"* ]] |
| 51 | } |
| 52 | |
| 53 | function ip::validate() { |
| 54 | local ip="$1" |
| 55 | if ! ip::is_valid "$ip"; then |
| 56 | log::error "Invalid IP or CIDR: ${ip}" |
| 57 | return 1 |
| 58 | fi |
| 59 | return 0 |
| 60 | } |
| 61 | |
| 62 | function ip::require_valid() { |
| 63 | local ip="$1" |
| 64 | ip::validate "$ip" || exit 1 |
| 65 | } |
| 66 |