Jump to content

Port forward automation using Event Juggler

From Teltonika Networks Wiki

Introdutcion

This configuration example demonstrates how to automate port forwarding using the Event Juggler function on Teltonika devices. By leveraging this feature, users can automatically create a port forwarding rule for the most recent DHCP lease IP address. This is especially useful in dynamic network environments, as it eliminates the need for manual updates and significantly reduces setup time during on-site deployments.

Port_forward script

The script used for this setup uses uci commands to create a new firewall rule:


#!/bin/sh                                                               
                                               
LAN_INTERFACE="br-lan"  # Adjust if needed                               
FORWARD_PORT=8080       # External port to forward                       
DEST_PORT=443           # Internal port on the target device           
PROTO="tcp"             # Protocol (tcp, udp, both)
RULE_NAME="DHCP_Forward"                                                                          
                    
LATEST_LEASE=$(head -n 1 /tmp/dhcp.leases | awk '{print $3}')      
LEASE_IP=$(head -n 1 /tmp/dhcp.leases | awk '{print $3}')                          
if [ -z "$LEASE_IP" ]; then
   echo "No DHCP leases found. Exiting."                                 
   exit 1
fi  

#Remove existing port forward rule if it exists                         
uci delete firewall.$RULE_NAME 2>/dev/null 
                                          
uci set firewall.$RULE_NAME="redirect"                                   
uci set firewall.$RULE_NAME.name="$RULE_NAME"                             
uci set firewall.$RULE_NAME.src="wan"                                     
uci set firewall.$RULE_NAME.src_dport="$FORWARD_PORT"                     
uci set firewall.$RULE_NAME.dest="lan"                                   
uci set firewall.$RULE_NAME.dest_ip="$LEASE_IP"                           
uci set firewall.$RULE_NAME.dest_port="$DEST_PORT"                       
uci set firewall.$RULE_NAME.proto="$PROTO"
uci commit firewall          
/etc/init.d/firewall restart 


Event juggler configuration

Event data configuration

Action data configuration

Conditions

Condition UP

Condition DOWN

Testing