Jump to content

Port forward automation using Event Juggler: Difference between revisions

From Teltonika Networks Wiki
Created page with "= 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 scri..."
 
 
(13 intermediate revisions by the same user not shown)
Line 5: Line 5:
== Port_forward script ==
== Port_forward script ==


The script used for this setup
The script used in this setup utilizes UCI commands to create a new firewall port forwarding rule, which is applied to the most recent IP address lease from the router's DHCP server. For instructions on creating a runnable script file, refer to our configuration example article - [[User Scripts examples]].
 
 
#!/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 juggler configuration ==
To begin with open router's ''WebUI'', navigate to '''Services → Event juggler''' and creat a new instance by entering the name and clicking on "'''Add'''" button.
[[File:Event juggler new.png|border|class=tlt-border]]
=== Event data configuration ===
=== Event data configuration ===
After creating a new event juggler instance, you will be redirected to the event data configuration window. In this window select the following options:
# Enable - '''On'''
# Event type - '''Log'''
# Events logs type - '''Port state'''
# Events log subtype - '''LAN1''' (''depending on the specific port you want to monitor'')
# Click on '''Next: action configuration'''
[[File:Event data configurationv2.png|border|class=tlt-border]]
=== Action data configuration ===
=== Action data configuration ===
----
In this setup, we will use multiple actions triggered by changes in the LAN1 port status. The first action will run a script, which creates a custom port forwarding rule for the latest IP lease. The second action will trigger when the LAN1 port goes DOWN, sending an SMS notification to a specific recipient to let them know the device has been disconnected or is no longer detected on the LAN1 port.
===== Script action =====
----
Make the following changes:
# Action name - '''Enter your preferred action name'''
# Delay - '''5''' (Specifies a delay time in seconds before the action is performed after the event is triggered)
# Action tpye - '''Script'''
# Script file type - '''Upload'''
# Script file - '''Upload your created script file'''
[[File:Demo_action1v2_.png|border|class=tlt-border]]
After the changes are done, click on[[File:Add new action.png|frameless]]
===== Send SMS action =====
----
Make the following changes:
# Action name - '''Enter your preferred action name'''
# Action type - '''Send SMS'''
# Text message - '''Enter your preferred text''' (''In this configuration we will send routers name and the timestamp'')
# Recipients - '''Single'''
# Phone number - '''Enter recipients phone number'''
# '''Save & Apply'''
[[File:Demo action2SMS.png|border|class=tlt-border]]
=== Conditions ===
=== Conditions ===
----
In the Event Juggler feature of the Teltonika devices, conditions are optional filters you can use to control when an action should happen.
When an event is triggered (for example, when the state of LAN1 changes), the router checks if any conditions are set. If the conditions are met, the action will be executed. If not, the action is skipped.
This allows you to narrow down broad triggers (like any change on a port) to more specific ones — for example, only reacting when the port goes UP or DOWN.
In this example, we will use two filter conditions to monitor the LAN1 port. One condition will detect when the port goes UP and trigger the first action. The other will detect when the port goes DOWN and trigger the second action.
To create a new '''Condition''' click the "'''Add'''" button under the conditions tab.
[[File:Event juggler condition1.png|border|class=tlt-border]]
==== Condition UP ====
==== Condition UP ====
----
Make the following changes:
# Condition name - '''Enter your preferred name'''
# Condition type - '''Filter'''
# Field name - '''Event text'''
# Value - '''UP'''
# Operator - '''In (a set of values)'''
# '''Save & Apply'''
[[File:Condition UP.png|border|class=tlt-border]]
==== Condition DOWN ====
==== Condition DOWN ====
----
Make the following changes:
# Condition name - '''Enter your preferred name'''
# Condition type - '''Filter'''
# Field name - '''Event text'''
# Value - '''DOWN'''
# Operator - '''In (a set of values)'''
# '''Save & Apply'''
[[File:Condition down.png|border|class=tlt-border]]
==== Assigning the conditions ====
----
Once the conditions are created, they can assigned to the specific action under the '''''Active conditions''''' section:
#Condition compatibility - '''And'''
#Active conditions - '''Select created condition accordingly to the setup.'''
#'''Save & Apply'''
[[File:Assign action.png|border|class=tlt-border]]
=== Finish the configuration ===
----
Once the configuration is done, click on "'''Finish'''".
[[File:Finish_configuration.png|border|class=tlt-border]]
== See also ==


== Testing ==
#[[UCI command usage]]
#[[User Scripts examples]]

Latest revision as of 13:00, 24 April 2025

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 in this setup utilizes UCI commands to create a new firewall port forwarding rule, which is applied to the most recent IP address lease from the router's DHCP server. For instructions on creating a runnable script file, refer to our configuration example article - User Scripts examples.


#!/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

To begin with open router's WebUI, navigate to Services → Event juggler and creat a new instance by entering the name and clicking on "Add" button.

Event data configuration

After creating a new event juggler instance, you will be redirected to the event data configuration window. In this window select the following options:

  1. Enable - On
  2. Event type - Log
  3. Events logs type - Port state
  4. Events log subtype - LAN1 (depending on the specific port you want to monitor)
  5. Click on Next: action configuration

Action data configuration


In this setup, we will use multiple actions triggered by changes in the LAN1 port status. The first action will run a script, which creates a custom port forwarding rule for the latest IP lease. The second action will trigger when the LAN1 port goes DOWN, sending an SMS notification to a specific recipient to let them know the device has been disconnected or is no longer detected on the LAN1 port.

Script action

Make the following changes:

  1. Action name - Enter your preferred action name
  2. Delay - 5 (Specifies a delay time in seconds before the action is performed after the event is triggered)
  3. Action tpye - Script
  4. Script file type - Upload
  5. Script file - Upload your created script file

After the changes are done, click on

Send SMS action

Make the following changes:

  1. Action name - Enter your preferred action name
  2. Action type - Send SMS
  3. Text message - Enter your preferred text (In this configuration we will send routers name and the timestamp)
  4. Recipients - Single
  5. Phone number - Enter recipients phone number
  6. Save & Apply

Conditions


In the Event Juggler feature of the Teltonika devices, conditions are optional filters you can use to control when an action should happen.

When an event is triggered (for example, when the state of LAN1 changes), the router checks if any conditions are set. If the conditions are met, the action will be executed. If not, the action is skipped. This allows you to narrow down broad triggers (like any change on a port) to more specific ones — for example, only reacting when the port goes UP or DOWN.

In this example, we will use two filter conditions to monitor the LAN1 port. One condition will detect when the port goes UP and trigger the first action. The other will detect when the port goes DOWN and trigger the second action.

To create a new Condition click the "Add" button under the conditions tab.

Condition UP


Make the following changes:

  1. Condition name - Enter your preferred name
  2. Condition type - Filter
  3. Field name - Event text
  4. Value - UP
  5. Operator - In (a set of values)
  6. Save & Apply

Condition DOWN


Make the following changes:

  1. Condition name - Enter your preferred name
  2. Condition type - Filter
  3. Field name - Event text
  4. Value - DOWN
  5. Operator - In (a set of values)
  6. Save & Apply

Assigning the conditions


Once the conditions are created, they can assigned to the specific action under the Active conditions section:

  1. Condition compatibility - And
  2. Active conditions - Select created condition accordingly to the setup.
  3. Save & Apply

Finish the configuration


Once the configuration is done, click on "Finish".

See also

  1. UCI command usage
  2. User Scripts examples