network-scripts/wireguard/genconfig_router

147 lines
3.3 KiB
Text
Raw Permalink Normal View History

2025-08-12 23:01:13 -04:00
#!/bin/bash
#
debug=0
ScriptName=$(basename "$0")
RouterName="RB5009-CTG"
RouterAddrPublic="heh08h84mnt.sn.mynetname.net"
RouterPort="14322"
RouterAddrPrivate="172.16.254.2"
RouterInterface="WG-Devices"
BOLD=$( tput bold)
NORMAL=$( tput sgr0)
RESET=$( tput sgr0)
NC=$( tput sgr0) # No color
BOLD=$( tput bold)
BLACK=$( tput setaf 0)
RED=$( tput setaf 1)
GREEN=$( tput setaf 2)
YELLOW=$( tput setaf 3)
BLUE=$( tput setaf 4)
MAGENTA=$( tput setaf 5)
CYAN=$( tput setaf 6)
WHITE=$( tput setaf 7)
DEFAULT=$( tput setaf 9)
#---------- function Message --------------------------------------------------
#
# Send to STDOUT
#
function Message()
{
printf "\n${GREEN}[i] ${BLUE}%s${NC}\n" "$*"
}
function CreateRouter()
{
local debug=0
local DeviceNum="$1" # voir plus bas avec printf
local DeviceName="$2"
local DeviceInterface="$3"
DeviceAllowedAddress="172.16.40.2"
DevicePrivKey=$(wg genkey)
DevicePubKey=$(echo "${DevicePrivKey}" | wg pubkey)
DevicePSK=$(wg genpsk)
DeviceNumPad=$(printf "%03d" $1) # 3 digit pad of $1
DeviceFilesPrefix="R-${DeviceNumPad}"
RouterFileCfg="${DeviceFilesPrefix}_RouterCfg.rsc"
DeviceFileCfg="${DeviceFilesPrefix}_DeviceCfg.rsc"
((debug)) && echo -e "
DeviceNum = $DeviceNum
DevicePrivKey = $DevicePrivKey
DevicePubKey = $DevicePubKey
DevicePSK = $DevicePSK
DeviceFileCfg = $DeviceFileCfg
RouterFileCfg = $RouterFileCfg
" | column -t && exit
Message "Generated output files:"
echo -e "${GREEN}---------------------------------------------------------${NC}
${RouterFileCfg}
${DeviceFileCfg}
"
Message "Router Config:"
echo -e "${GREEN}---------------------------------------------------------${NC}"
echo -e "S'assurer que sur router: /interface wireguard add listen-port=${RouterPort} mtu=1420 name=${RouterInterface}\n"
echo -e "/interface wireguard peers add allowed-address=172.16.254.${DeviceNum}/32 client-keepalive=10 disabled=no comment=\"${DeviceName}\" \\
interface=WG-Devices preshared-key=\"${DevicePSK}\" public-key=\"${DevicePubKey}\""
#| tee "${RouterFileCfg}"
Message "${DeviceName} device Config:"
echo -e "${GREEN}---------------------------------------------------------${NC}"
echo -e "/interface wireguard add listen-port=13239 mtu=1420 name=${DeviceInterface} private-key=\"${DevicePrivKey}\"
/interface wireguard peers add allowed-address=${RouterAddrPrivate} client-keepalive=15 disabled=no comment=\"${RouterName}\" \\
interface=${DeviceInterface} preshared-key=\"${DevicePSK}\" public-key=\"${DevicePubKey}\" endpoint-address=${RouterAddrPublic} endpoint-port=${RouterPort}
/ip route add dst-address=0.0.0.0 gateway=${RouterAddrPrivate}"
#| tee "${DeviceFileCfg}"
}
Help() {
cat << EOF
usage: $(basename "$0") [OPTIONS]
-d Device Number
-n Device Name
-i Device Interface
-h This help
EOF
}
((!$#)) && Help
while getopts d:n:i:h option
do
case "${option}" in
d) DeviceNumber=${OPTARG};;
n) DeviceName=${OPTARG};;
i) DeviceInterface=${OPTARG};;
h) Help
exit 0;;
*) echo -e "Usage (bad argument: ${OPTARG}) \n"
exit 1;;
esac
done
((debug)) && echo -e "
DeviceNumber = $DeviceNumber
DeviceName = $DeviceName
DeviceInterface = $DeviceInterface
" | column -t && exit
CreateRouter ${DeviceNumber} ${DeviceName} ${DeviceInterface}