I have created a script which works on a Hex Poe but doesn’t work on a RB4011 nor a CCR1009. I just can’t figure out why it doesn’t.
Can someone please take a look why it doesn’t work?
#Get Ip And Save it To "RFC_WAN_IP.txt" File In Mikrotik
/tool fetch url="http://myip.dnsomatic.com/RFC_WAN_IP.txt" mode=http
#Save Ip From "RFC_WAN_IP.txt" File To "MyVar" Variable
:local myvar [file get RFC_WAN_IP.txt contents ]
#Write Ip From "MyVar" Variable To Mikrotik Log
/log warning message=($myvar . " - NAME OF CLIENT")
Reason - ccr1009 and rb4011 are much faster comparing to Hex %))
Tool fetch call in not synchronous i.e. returns immedeatly and url connected in background.
I.e. you should wait after “fetch” until file downloaded.
How long… who knows %)
In my scripts I do in more complicated way - redirecting output of tool fetch to file and wait until file really downloaded or failed (check here).
But for you simplest way - just add delay :
#Get Ip And Save it To "RFC_WAN_IP.txt" File In Mikrotik
/tool fetch url="http://myip.dnsomatic.com/RFC_WAN_IP.txt" mode=http
delay 5s
#Save Ip From "RFC_WAN_IP.txt" File To "MyVar" Variable
:local myvar [file get RFC_WAN_IP.txt contents ]
#Write Ip From "MyVar" Variable To Mikrotik Log
/log warning message=($myvar . " - NAME OF CLIENT")
Thanks a lot but unfortunately it isn’t working.
I even put a delay of 45s but no luck
#Get Ip And Save it To "RFC_WAN_IP.txt" File In Mikrotik
/tool fetch url="http://myip.dnsomatic.com/RFC_WAN_IP.txt" mode=http
delay 45s
#Save Ip From "RFC_WAN_IP.txt" File To "MyVar" Variable
:local myvar [file get RFC_WAN_IP.txt contents ]
#Write Ip From "MyVar" Variable To Mikrotik Log
/log warning message=($myvar . " - NAME OF CLIENT")
The URL you posted is rate-limited, and returns often “429 Too many requests”. In console you can see it, but probably not in script. You are probably hitting this.
I’m not sure which RouterOS version are you using, but I prefer using
do {
:local myvar [/tool fetch url="http://myip.dnsomatic.com/RFC_WAN_IP.txt" output=user as-value ];
:put (($myvar->"data") . " - NAME OF CLIENT")
} on-error={
:put "got error trying to fetch"
}
This should work in long term, stable and testing.
Note that “as-value” returns a data structure like