From 251bc38996312059a8fe7612f0ba4fd537c31fe6 Mon Sep 17 00:00:00 2001 From: Anonymous <> Date: Wed, 15 Jan 2025 19:36:29 +0800 Subject: [PATCH] feat: use static ip without manually typing it --- debi.sh | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/debi.sh b/debi.sh index a629aec..58a3b5b 100755 --- a/debi.sh +++ b/debi.sh @@ -192,6 +192,24 @@ has_backports() { return 1 } +cidr_to_netmask() { + local cidr=$1 + local mask="" + + for ((i=0; i<4; i++)); do + if [ $cidr -ge 8 ]; then + mask+="255." + cidr=$((cidr - 8)) + else + mask+=$((256 - 2**(8 - cidr))). + cidr=0 + fi + done + + # Remove the trailing dot + echo "${mask%.}" +} + interface=auto ip= netmask= @@ -274,6 +292,14 @@ while [ $# -gt 0 ]; do mirror_host=mirrors.tuna.tsinghua.edu.cn ntp=time.amazonaws.cn ;; + --static-ip) + ip=$(ip r get 1.1.1.1 | awk '/src/ {print $7}') + gateway=$(ip r get 1.1.1.1 | awk '/via/ {print $3}') + _cidr=$(ip -o -f inet addr show | grep -w "$ip" | awk '{print $4}' | cut -d'/' -f2) + netmask=$(cidr_to_netmask "$_cidr") + hostname=$(hostname) + interface=$(ip r get 1.1.1.1 | awk '/dev/ {print $5}') + ;; --interface) interface=$2 shift