From 4807410541ea806b29b9a73dfc8890193fa6022a Mon Sep 17 00:00:00 2001 From: cha0sCat <47235156+cha0sCat@users.noreply.github.com> Date: Sat, 22 Feb 2025 20:54:04 +0800 Subject: [PATCH] feat: add --static-ipv4 option, auto parse current network settings. (#131) * feat: use static ip without manually typing it * fix(cidr_to_netmask): rewrite to support sh * refactor: remove cidr_to_netmask(), set cidr directly https://lists.debian.org/debian-boot/2012/09/msg00239.html * refactor: rename option --static-ip to --static-ipv4 * Add --static-ipv4 doc --------- Co-authored-by: Anonymous <> Co-authored-by: Brent, Yang Bohan --- README.md | 1 + debi.sh | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/README.md b/README.md index a5c1cde..27fd4d9 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ Otherwise, you can run this command to revert all changes made by the script: * `--interface ` Manually select a network interface, e.g. eth1 * `--ethx` Disable *Consistent Network Device Naming* to get interface names like *ethX* back * `--ip ` Disable the auto network config (DHCP) and configure a static IP address, e.g. `10.0.0.2`, `1.2.3.4/24`, `2001:2345:6789:abcd::ef/48` + * `--static-ipv4` Disable the auto network config (DHCP) and configure with the current IPv4 address and gateway detected automatically * `--netmask ` e.g. `255.255.255.0`, `ffff:ffff:ffff:ffff::` * `--gateway ` e.g. `10.0.0.1`, `none` if no gateway * `--dns '8.8.8.8 8.8.4.4'` diff --git a/debi.sh b/debi.sh index a629aec..79ab351 100755 --- a/debi.sh +++ b/debi.sh @@ -274,6 +274,14 @@ while [ $# -gt 0 ]; do mirror_host=mirrors.tuna.tsinghua.edu.cn ntp=time.amazonaws.cn ;; + --static-ipv4) + 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) + ip="$ip/$_cidr" + hostname=$(hostname) + interface=$(ip r get 1.1.1.1 | awk '/dev/ {print $5}') + ;; --interface) interface=$2 shift