Capture interface variable

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

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.

{
: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]]
}

Without PPPoE, I need to capture either manually done dynamically or via DHCP Client.
I will test with your script, thank you very much.

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

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

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<"
}

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.

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

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.

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

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
}

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:

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.

I’ll try, I’m not very good at scripting, but so far I really appreciate your help.

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"
    }
}
}

Thank you very much, it worked, I really appreciate your time in helping me.

:+1::+1::+1: