Script to get interface WAN IP on PPPoE connections and DHCP connections

I need a script to get the WAN IP in order to get tx-byte rx-byte etc. and it needs to work when the connection is PPPoE or DHCP Client.

I already made this:

:local ipgw;
:foreach i in=[/ip route find where dst-address=“0.0.0.0/0”] do={
:set ipgw [/ip route get $i gateway];
:put $ipgw
:set [/ip address get [find where interface=$ipgw] value-name=address]
:put $ipgw
}
For PPP connection it works perfectly but, when is a DHCP client connection, the first value in the variable ipgw is the gateway IP, and in the line below it returns the error “no such item” because will not have any interface named with the gateway IP.

I need to make it works for both cases, can you help me please?

This part fails:

[/ip address get [find where interface=$ipgw] value-name=address]

For my routers, interface does not show IP default-gateway 92.xxx.xxx.1. So you can not use default-gateway to find what IP address you have on public interface.
Example:

/ip address print 
Flags: X - disabled, I - invalid, D - dynamic 
 #   ADDRESS            NETWORK         INTERFACE                                                                                                   
 2 D 92.xxx.xxx.91/21   92.xxx.xx.0    ether1

Simple solution, turn on Cloud service

/ip cloud
set ddns-enabled=yes

Then you should get public IP like this

:put [/ip cloud get public-address]

If you do not like this solution, you need to ask a http server and fetch the IP.

Thanks for answering.

So this command could be helpful, but I will also need to get rx-byte and tx-byte, and with the public IP i can’t get those infos.

Is any way to use the public IP to get the WAN interface rx-byte, tx-byte, rx-error and tx-error?

The actual script is:
#get wan interface
:local waninterface [/ip arp get number=[find where address=[/ip route get number=[find where dst-address=“0.0.0.0/0” && active=yes ] gateway]] interface];
:local wanrxbyte [/interface get $waninterface rx-byte];
:local wantxbyte [/interface get $waninterface tx-byte];
:local wantxerror [/interface get $waninterface tx-error];
:local wanrxerror [/interface get $waninterface rx-error];
:local wanrxrate;
:local wantxrate;

/interface monitor-traffic $waninterface once do={
:set wanrxrate $“rx-bits-per-second”
:set wantxrate $“tx-bits-per-second”
}

For PPPoE is not working