Last active
June 12, 2025 09:38
-
-
Save eznix86/26f1547fcbfb7a0e599c08b72463fc62 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ############################# | |
| # To run as root, also don't forget to do ssh-copy-id to the milkv | |
| # ############################### | |
| #!/bin/bash | |
| if [[ $EUID -ne 0 ]]; then | |
| echo "[ERROR] This script must be run as root (use sudo)." >&2 | |
| exit 1 | |
| fi | |
| OUT_IF=$(ip route get 8.8.8.8 2>/dev/null | awk '{print $5; exit}') | |
| if [[ -z "$OUT_IF" ]]; then | |
| echo "[ERROR] Could not detect outgoing interface." >&2 | |
| exit 1 | |
| fi | |
| echo "[INFO] Outgoing interface detected: $OUT_IF" | |
| echo "[INFO] Enabling IP forwarding..." | |
| sysctl -w net.ipv4.ip_forward=1 >/dev/null | |
| if [[ "$(sysctl -n net.ipv4.ip_forward)" -ne 1 ]]; then | |
| echo "[ERROR] Failed to enable IP forwarding." >&2 | |
| exit 1 | |
| fi | |
| echo "[INFO] Setting iptables rules..." | |
| iptables -P FORWARD ACCEPT | |
| iptables -t nat -C POSTROUTING -s 192.168.42.0/24 -o "$OUT_IF" -j MASQUERADE 2>/dev/null \ | |
| || iptables -t nat -A POSTROUTING -s 192.168.42.0/24 -o "$OUT_IF" -j MASQUERADE | |
| if ! iptables -t nat -L POSTROUTING -n | grep -q MASQUERADE; then | |
| echo "[ERROR] iptables MASQUERADE rule was not set correctly." >&2 | |
| exit 1 | |
| fi | |
| IP_OF_HOST=$(ip addr show | grep '192.168.42' | awk '{print $2}' | cut -d/ -f1) | |
| if [[ -z "$IP_OF_HOST" ]]; then | |
| echo "[ERROR] Could not detect IP in 192.168.42.x range. Are you connected to the board?" >&2 | |
| exit 1 | |
| fi | |
| echo "[INFO] Host IP on 192.168.42.x network: $IP_OF_HOST" | |
| echo "[INFO] Configuring the board via SSH..." | |
| ssh [email protected] <<EOF | |
| ip route replace default via $IP_OF_HOST | |
| echo "nameserver 8.8.8.8" >> /etc/resolv.conf | |
| exit | |
| EOF | |
| echo "[INFO] Verifying connectivity from board to the internet..." | |
| ssh [email protected] "ping -c 2 -W 2 8.8.8.8" >/dev/null | |
| if [[ $? -eq 0 ]]; then | |
| echo "[SUCCESS] Board can reach 8.8.8.8" | |
| echo "[INFO] Verifying DNS resolution (ping google.com)..." | |
| ssh [email protected] "ping -c 2 -W 2 google.com" >/dev/null | |
| if [[ $? -eq 0 ]]; then | |
| echo "[SUCCESS] DNS resolution works on the board (google.com reachable)" | |
| else | |
| echo "[WARNING] Board can reach IPs, but DNS (e.g. google.com) failed." | |
| fi | |
| else | |
| echo "[ERROR] Board still cannot reach the internet." >&2 | |
| exit 1 | |
| fi | |
| echo "Syncing time with NTP..." | |
| ssh [email protected] "ntpd -d -q -n -p pool.ntp.org" | |
| echo "Time sync completed" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Automation scripts
or do the inverse (from milkv-duo activate internet) or checkout on how to make it automated:
When the hosts reboots
on Raspberry Pi
Inside of
/usr/local/bin/milkv-network-setup.shThen
chmod +x /usr/local/bin/milkv-network-setup.sh sudo systemctl enable milkv-duo-network sudo systemctl start milkv-duo-network