Community discussions

MikroTik App
 
antiqued4
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 75
Joined: Mon Jan 13, 2020 1:50 pm

Capture interface variable

Mon Aug 29, 2022 11:26 pm

Hello everyone, I wanted to know if there is a way to create a script like this:
The default route is an IP gateway to capture the responsible interface and put it in a variable.
As in the image, if the script detects that the gateway is an ip, it captures the physical interface ether1 and puts the value ether1 in a variable

Image
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Capture interface variable

Tue Aug 30, 2022 1:59 am

This is dynamic rule generated from dhcp client? pppoe client? another?
Simply go to read on what interface is the dhcp client, pppoe client, etc.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Capture interface variable

Tue Aug 30, 2022 2:14 am

{
:local test [:tostr [/ip route get [find where dst-address=0.0.0.0/0 and ([:typeof [:toip (($gateway)->0)]] = "ip")] gateway-status]]
:put [:pick $test ([:find $test "via  "] + 5) [:len $test]]
}
 
antiqued4
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 75
Joined: Mon Jan 13, 2020 1:50 pm

Re: Capture interface variable

Tue Aug 30, 2022 1:59 pm

This is dynamic rule generated from dhcp client? pppoe client? another?
Simply go to read on what interface is the dhcp client, pppoe client, etc.
Without PPPoE, I need to capture either manually done dynamically or via DHCP Client.
I will test with your script, thank you very much.
 
antiqued4
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 75
Joined: Mon Jan 13, 2020 1:50 pm

Re: Capture interface variable

Tue Aug 30, 2022 2:09 pm

It worked my friend, but I need to put this value of the variable on a command line and tested here all return error.
I'll give an example:
{
:local test [:tostr [/ip route get [find where dst-address=0.0.0.0/0 and ([:typeof [:toip (($gateway)->0)]] = "ip")] gateway-status]]
:put [:pick $test ([:find $test "via  "] + 5) [:len $test]]
/ip firewall mangle add chain=postrouting connection-state=new out-interface=$test action=mark-connection new-connection-mark=test
}
The result is this in the terminal:
ether1
input does not match any value of interface
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Capture interface variable

Tue Aug 30, 2022 2:17 pm

I do not understand why write the interface then give error.
If the interface exist, on next line can't do error.
RouterOS version used?
 
antiqued4
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 75
Joined: Mon Jan 13, 2020 1:50 pm

Re: Capture interface variable

Tue Aug 30, 2022 2:52 pm

I do not understand why write the interface then give error.
If the interface exist, on next line can't do error.
RouterOS version used?
This error:
"input does not match any value of interface"
It only appears with the add mangle line....
Without it, just your script, the error does not come.
Latest version Long-term v6
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Capture interface variable

Tue Aug 30, 2022 5:05 pm

Use this and post on forum the exact string you give, do not omit >< or other characters:
{
:local test [:tostr [/ip route get [find where dst-address=0.0.0.0/0 and ([:typeof [:toip (($gateway)->0)]] = "ip")] gateway-status]]
:put ">$test<"
}
 
antiqued4
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 75
Joined: Mon Jan 13, 2020 1:50 pm

Re: Capture interface variable

Tue Aug 30, 2022 5:30 pm

Use this and post on forum the exact string you give, do not omit >< or other characters:
{
:local test [:tostr [/ip route get [find where dst-address=0.0.0.0/0 and ([:typeof [:toip (($gateway)->0)]] = "ip")] gateway-status]]
:put ">$test<"
}
Image

The idea is the same as you did in the first code, remove everything and leave only "ether1" in the variable's value, then use this variable in the mangle rule, as an example.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Capture interface variable

Tue Aug 30, 2022 5:40 pm

ok, is exactly "ether1"
but why this give you an error???
I test again only this line on 6.48.6 and work as expected
/ip firewall mangle add chain=postrouting connection-state=new out-interface=ether1 action=mark-connection new-connection-mark=test
 
antiqued4
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 75
Joined: Mon Jan 13, 2020 1:50 pm

Re: Capture interface variable

Tue Aug 30, 2022 5:46 pm

Here in this part:
connection-state=new out-interface=ether1
I use the variable, getting
connection-state=new out-interface=$test

And then comes the error.

Image

my route table, I'm testing with more than one default wheel.
As using PPPoE I use another script that does this already, creates the mangle rules using the variable and added the pppoe interface

Image
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Capture interface variable

Tue Aug 30, 2022 5:49 pm

Ah, now I understand...
The line with add on mangle is added by you.

This is the corrected version:
{
:local test [:tostr [/ip route get [find where dst-address=0.0.0.0/0 and ([:typeof [:toip (($gateway)->0)]] = "ip")] gateway-status]]
:local gint [:pick $test ([:find $test "via  "] + 5) [:len $test]]
:put $gint
/ip firewall mangle add chain=postrouting connection-state=new out-interface=$gint action=mark-connection new-connection-mark=test
}
 
antiqued4
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 75
Joined: Mon Jan 13, 2020 1:50 pm

Re: Capture interface variable

Tue Aug 30, 2022 5:56 pm

I just did the test with your script and it worked fine. But in case you have two routes, in the same way using IP for different interfaces, it returns this error:
invalid internal item number

I think it's because it has two interfaces, I believe it would be a totally different script, correct? Example:

Image
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Capture interface variable

Tue Aug 30, 2022 6:00 pm

The script is just for obtain a result to project script as needed.
Obviously if result are not one single value, but one array, the script must be changed accordingly.
 
antiqued4
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 75
Joined: Mon Jan 13, 2020 1:50 pm

Re: Capture interface variable

Tue Aug 30, 2022 6:06 pm

I'll try, I'm not very good at scripting, but so far I really appreciate your help.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Capture interface variable  [SOLVED]

Tue Aug 30, 2022 6:21 pm

This avoid inserting duplicate time the interface on mangle, and support more interfaces.
{
/ip route
:foreach item in=[find where dst-address=0.0.0.0/0 and ([:typeof [:toip (($gateway)->0)] ] = "ip")] do={
    :local test [:tostr [get $item gateway-status]]
    :local gint [:pick $test ([:find $test "via  "] + 5) [:len $test]]
    /ip firewall mangle
    :if ([:len [find where chain="postrouting" and connection-state="new" and out-interface=$gint and action="mark-connection" \
            and new-connection-mark="test"]] = 0) do={
        add chain="postrouting" connection-state="new" out-interface=$gint action="mark-connection" new-connection-mark="test"
    }
}
}
 
antiqued4
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 75
Joined: Mon Jan 13, 2020 1:50 pm

Re: Capture interface variable

Tue Aug 30, 2022 7:24 pm

Thank you very much, it worked, I really appreciate your time in helping me.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Capture interface variable

Tue Aug 30, 2022 7:26 pm

👍👍👍

Who is online

Users browsing this forum: No registered users and 19 guests