Skip to content

Instantly share code, notes, and snippets.

@eznix86
Last active June 12, 2025 09:38
Show Gist options
  • Select an option

  • Save eznix86/26f1547fcbfb7a0e599c08b72463fc62 to your computer and use it in GitHub Desktop.

Select an option

Save eznix86/26f1547fcbfb7a0e599c08b72463fc62 to your computer and use it in GitHub Desktop.
# #############################
# 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"
@eznix86
Copy link
Author

eznix86 commented Jun 11, 2025

Automation scripts

# same thing as above but shorter without time sync
bash <(curl -sL http://tiny.cc/milkv-duo-activate-intern) 

# with time sync
bash <(curl -sL https://gist.github.com/eznix86/26f1547fcbfb7a0e599c08b72463fc62/raw/1add999d282f0a2779a4ea8f06896763e9cd9485/milkv-duo-rdnis-internet.sh) 

or do the inverse (from milkv-duo activate internet) or checkout on how to make it automated:

bash -c "ssh [email protected] 'bash -s'" < <(curl -sL https://gist.github.com/eznix86/4b562aeafd99a0ad4479bba13e6dee31/raw/459785c42dd8a6514093573c5d3d81cf25a9eea2/milkv-duo-internet.sh)

When the hosts reboots

sudo su # login as root

ssh-keygen # without password

ssh-copy-id [email protected]

on Raspberry Pi

sudo vi /etc/systemd/system/milkv-duo-network.service
[Unit]
Description=Milkv Duo Service
After=network.target
Requires=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/milkv-network-setup.sh

[Install]
WantedBy=multi-user.target

Inside of /usr/local/bin/milkv-network-setup.sh

#!/bin/bash

# Check if usb0 interface exists
if ip link show usb0 > /dev/null 2>&1; then
    echo "usb0 interface found."

    # Check if we have an internet connection (e.g., ping Google DNS)
    if ping -q -c 1 -W 2 8.8.8.8 > /dev/null 2>&1; then
        echo "Internet connection detected. Running script..."
        bash <(curl -sL https://gist.github.com/eznix86/26f1547fcbfb7a0e599c08b72463fc62/raw/1add999d282f0a2779a4ea8f06896763e9cd9485/milkv-duo-rdnis-internet.sh) 
    else
        echo "No internet connection. Script not run."
    fi
else
    echo "usb0 interface not found. Script not run."
fi

Then

chmod +x /usr/local/bin/milkv-network-setup.sh
sudo systemctl enable milkv-duo-network
sudo systemctl start milkv-duo-network

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment