Skip to content

Instantly share code, notes, and snippets.

@Geofferey
Last active February 22, 2026 23:52
Show Gist options
  • Select an option

  • Save Geofferey/0b4f91c1357d3f71dd39670b7bc5b4e8 to your computer and use it in GitHub Desktop.

Select an option

Save Geofferey/0b4f91c1357d3f71dd39670b7bc5b4e8 to your computer and use it in GitHub Desktop.
A Pineapple Pager Payload for Toggling the USB-C port between WAN/LAN
#!/bin/bash
# Title: USB-C WAN/LAN Toggler
# Author: Geofferey
# Description: Removes eth0 from bridge so it obtains an IP from connected device
# Version: 1.0
PATH=/mmc/bin:/mmc/sbin:/mmc/usr/bin:/mmc/usr/sbin:/usr/sbin:/usr/bin:/sbin:/bin
enable () {
LOG yellow "Removing eth0 from br-lan..."
brctl delif br-lan eth0
if ! ps | grep -q "[u]dhcpc -i eth0 -b"; then
LOG ""
LOG yellow "Starting udhcpc on eth0..."
udhcpc -i eth0 -b -n -q
fi
LOG ""
LOG yellow "Re-add wlan0mgmt to br-lan if up and not a member"
if [[ $(ip link show wlan0mgmt |grep "state UP") ]] && [[ ! $(brctl show br-lan |grep wlan0mgmt) ]]; then
brctl addif br-lan wlan0mgmt
fi
if ! nft list chain inet fw4 srcnat | grep -q 'comment "eth0_wan_masquerade"'; then
LOG ""
LOG yellow "Enabling NAT masquerading on eth0"
nft insert rule inet fw4 accept_to_wan oifname "eth0" counter accept comment "eth0_wan_fwd"
nft insert rule inet fw4 srcnat oifname "eth0" jump srcnat_wan comment "eth0_wan_masquerade"
fi
sleep 1
ETH0_IP=$(ifconfig eth0 |grep -w "inet addr" | cut -d ':' -f2 | cut -d ' ' -f1)
ETH0_BCAST=$(ifconfig eth0 |grep -w "inet addr" | cut -d ':' -f3 | cut -d ' ' -f1)
ETH0_MASK=$(ifconfig eth0 |grep -w "inet addr" | cut -d ':' -f4 | cut -d ' ' -f1)
ETH0_GW=$(ip ro |grep default |grep eth0 | cut -d ' ' -f3)
LOG ""
LOG green "IP: ${ETH0_IP}"
LOG yellow "BCAST: ${ETH0_BCAST}"
LOG red "NETMASK: ${ETH0_MASK}"
LOG cyan "GATEWAY: ${ETH0_GW}"
}
disable () {
ETH0_IP=$(ifconfig eth0 |grep -w "inet addr" | cut -d ':' -f2 | cut -d ' ' -f1)
if nft list chain inet fw4 srcnat | grep -q 'comment "eth0_wan_masquerade"'; then
LOG cyan "Remove NAT masquerading on eth0"
ACCPT_HANDLE=$(nft -a list chain inet fw4 accept_to_wan |grep "eth0_wan_fwd" |cut -d '#' -f2 |cut -d ' ' -f3)
SRCNAT_HANDLE=$(nft -a list chain inet fw4 srcnat |grep "eth0_wan_masquerade" |grep handle |cut -d '#' -f2 | cut -d ' ' -f3)
nft delete rule inet fw4 accept_to_wan handle $ACCPT_HANDLE
nft delete rule inet fw4 srcnat handle $SRCNAT_HANDLE
fi
LOG ""
LOG cyan "Removing current IP from eth0..."
ip addr del ${ETH0_IP}
LOG ""
LOG cyan "Re-adding eth0 to br-lan"
if [[ ! $(brctl show br-lan |grep eth0) ]]; then
brctl addif br-lan eth0
fi
LOG ""
LOG cyan "Re-add wlan0mgmt to br-lan if up and not a member"
if [[ $(ip link show wlan0mgmt |grep "state UP") ]] && [[ ! $(brctl show br-lan |grep wlan0mgmt) ]]; then
brctl addif br-lan wlan0mgmt
fi
}
info () {
ETH0_IP=$(ifconfig eth0 |grep -w "inet addr" | cut -d ':' -f2 | cut -d ' ' -f1)
ETH0_BCAST=$(ifconfig eth0 |grep -w "inet addr" | cut -d ':' -f3 | cut -d ' ' -f1)
ETH0_MASK=$(ifconfig eth0 |grep -w "inet addr" | cut -d ':' -f4 | cut -d ' ' -f1)
ETH0_GW=$(ip ro |grep default |grep eth0 | cut -d ' ' -f3)
LOG ""
LOG green "IP: ${ETH0_IP}"
LOG green "BCAST: ${ETH0_BCAST}"
LOG green "NETMASK: ${ETH0_MASK}"
LOG green "GATEWAY: ${ETH0_GW}"
}
LOG ""
LOG yellow "Press [â–²] for WAN mode"
LOG cyan "Press [â–¼] for LAN mode"
LOG green "Press [A] for INFO"
LOG red "Press [B] to CANCEL"
COUNT=1
resp=$(WAIT_FOR_INPUT)
until [[ "${resp}" == "UP" ]] || [[ "${resp}" == "DOWN" ]]; do
if [[ "${resp}" == "B" ]]; then
exit 0
fi
if [[ "${resp}" == "A" ]]; then
info
break
fi
if [[ ${COUNT} -eq "1" ]]; then
LOG ""
fi
LOG red "INVALID: Please press [â–²]/[â–¼]"
resp=$(WAIT_FOR_INPUT)
((COUNT++))
if [[ ${COUNT} -gt "2" ]]; then
LOG ""
LOG red "No valid response received"
break
fi
done
LOG ""
if [[ "${resp}" == "UP" ]]; then
enable
fi
if [[ "${resp}" == "DOWN" ]]; then
disable
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment