#!/bin/bash
# Input file containing IP ranges in CIDR notation
INPUT_FILE="ip_ranges.txt"
# Gateway to use in route commands
GATEWAY="192.168.0.1"
# Function to convert CIDR to range and generate route commands
generate_routes() {
local cidr=$1
local gateway=$2
# Extract the base IP and prefix from CIDR
local base_ip=$(echo "$cidr" | cut -d'/' -f1)
local prefix=$(echo "$cidr" | cut -d'/' -f2)
# Calculate the subnet mask
local mask=$(( 0xFFFFFFFF ^ ((1 << (32 - prefix)) - 1) ))
local netmask="$(( (mask >> 24) & 255 )).$(( (mask >> 16) & 255 )).$(( (mask >> 8) & 255 )).$(( mask & 255 ))"
echo "route add -net $base_ip netmask $netmask gw $gateway"
}
# Read input file and process each CIDR block
while read -r line; do
[[ -z "$line" || "$line" =~ ^# ]] && continue # Skip empty lines and comments
generate_routes "$line" "$GATEWAY"
done < "$INPUT_FILE"