I have a relatively niche use case where I need a single router to have two uses - one that it is a stand alone closed offline network where devices can connect directly and the other where this router can be linked via ethernet to an upstream router.
For this I have put together a script that runs on start up that identifies if there is an ethernet connected and if so puts the router into static protocol. If there isn't it enables a DHCP client so listen to the upstrea router.
The scripts functions but when the router has been set on the static protocol I cannot access the router via 192.168.1.1 as before. Could somebody shine some light as to why this is happening and what the fix would be? Script code below -
#!/bin/sh
ETH_INTERFACE="eth0"
NETWORK_CONFIG_FILE="/etc/config/network"
changes_made=0 # Set the flag to 0 initially (no changes made)
# Check the link status of the Ethernet interface
link_status=$(ethtool "${ETH_INTERFACE}" | grep "Link detected:" | awk '{print $3}')
if [ "${link_status}" = "yes" ]; then
echo "Ethernet port is connected."
dhcp_enabled=$(uci get network.lan.proto)
if [ "${dhcp_enabled}" != "dhcp" ]; then
echo "Set the network interface to be a DHCP client."
uci set network.lan.proto=dhcp
uci commit network
changes_made=1
else
echo "DHCP client is already enabled for LAN interface."
fi
else
echo "Ethernet port is not connected."
# Check if static is already disabled
static_enabled=$(uci get network.lan.proto)
if [ "${static_enabled}" != "static" ]; then
echo "Manually setting IP address for LAN interface..."
# Remove existing DHCP settings if present
uci delete dhcp.lan
# Set loopback interface configuration
uci set network.loopback=interface
uci set network.loopback.device='lo'
uci set network.loopback.proto='static'
uci set network.loopback.ipaddr='127.0.0.1'
uci set network.loopback.netmask='255.0.0.0'
# Set br-lan bridge configuration
uci set network.lan=interface
uci set network.lan.device='br-lan'
uci set network.lan.proto='static'
uci set network.lan.ipaddr='192.168.1.1'
uci set network.lan.netmask='255.255.255.0'
uci set network.lan.ip6assign='60'
# Unspecified IPv6 assignment length
uci set network.lan.gateway='192.168.0.1'
# Set switch configuration
uci set network.switch0=switch
uci set network.switch0.name='switch0'
uci set network.switch0.reset='1'
uci set network.switch0.enable_vlan='1'
# Set switch_vlan configuration
uci set network.switch_vlan0=switch_vlan
uci set network.switch_vlan0.device='switch0'
uci set network.switch_vlan0.vlan='1'
uci set network.switch_vlan0.ports='4 6t'
# Set global configuration
uci set network.globals=globals
uci set network.globals.ula_prefix='fdbb:584a:17dd::/48'
# Commit the changes
uci commit network
uci commit dhcp
echo "Configuration updated."
changes_made=1
else
echo "IP already on manual, no need to set."
fi
fi
# Reboot the device if changes were made
if [ $changes_made -eq 1 ]; then
echo "Rebooting the device..."
reboot
fi
I suspect you might need to consider an alternate test. If you're simply checking for a link status (Layer 1/Layer 2), you'll get a positive response if a cable is plugged in. Tests based on Layer 1/Layer 2 inherently have no logic to detect Layer 3 details.
Also, your script modifies only the LAN interface, yet your description refers to an "upstream" router; "upstream" implies WAN interface.
Not that you necessarily can't get it working entirely with the LAN interface, but you should consider which interface(s) you want to use in each scenario, and tailor your script accordingly.
I have indeed tried the reload instead, the complete reboot was a new addition to see if that helped in any way.
Should the lan gateway also be 192.168.1.1?
Sorry I wasn't clear there - the devices that are connecting are a range of android devices and PC's but I can't access 192.168.1.1 on any devices connected. I can when it's in the DHCP client case and if i set the static protocol from the web interface but not once set to static programatically.
No. The LAN interface is the gateway. You only need to define an explicit gateway if you need to send traffic from the LAN to another gateway on the same subnet (e.g. when using as a dumb AP or as an upstream router for another network).
The old adage "a picture paints a thousand words" may be relevant here. My profile has a link to an excellent tool for creating shoddy diagrams in a hurry. If you can "draw" what you're trying to achieve, that might help your audience better understand the challenges you're facing.
Obviously I can't have DHCP servers on both routers as this will end up in conflicts, but need some way to allocate IP address to devices connecting to the OpenWrt router when not connected to the upstream router with internet access.
If I take your diagram literally, then you might be able to achieve your desired goal very easily:
Reset OpenWRT to factory defaults
Enable the Wi-Fi AP on OpenWRT
(Optionally, configure a different SSID and/or encryption for the Wi-Fi AP)
Plug the WAN interface of OpenWRT in to the upstream router
Now, there is one caveat: the upstream router's subnet and OpenWRT's LAN subnet must not conflict. If both subnets are 192.168.1.0/24 then you must change one of them.
Yes, but the devices themselves might complain about "no Internet" or "no connectivity", if they can't carry out a basic "do I have Internet access?" test.