Community discussions

MikroTik App
 
rfrancocantero
just joined
Topic Author
Posts: 7
Joined: Mon Mar 25, 2019 5:05 pm

Write IP to log

Wed Apr 03, 2019 12:36 pm

Hi all,

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")
 
User avatar
BlackVS
Member Candidate
Member Candidate
Posts: 174
Joined: Mon Feb 04, 2013 7:00 pm
Contact:

Re: Write IP to log

Wed Apr 03, 2019 2:27 pm

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")
 
rfrancocantero
just joined
Topic Author
Posts: 7
Joined: Mon Mar 25, 2019 5:05 pm

Re: Write IP to log

Wed Apr 03, 2019 3:19 pm

Hi,

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")
 
nostromog
Member Candidate
Member Candidate
Posts: 226
Joined: Wed Jul 18, 2018 3:39 pm

Re: Write IP to log

Wed Apr 03, 2019 4:35 pm

Hi,

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

data=NNN.NNN.NNN.NNN;downloaded=0;duration=00:00:01;status=finished;total=0

So you need to access ($myvar->"data")
 
rfrancocantero
just joined
Topic Author
Posts: 7
Joined: Mon Mar 25, 2019 5:05 pm

Re: Write IP to log

Wed Apr 03, 2019 6:16 pm

Hi,

I modified the code like you listed:
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"
      }
  /log warning message=($myvar->"data")
Unfortunately it still doesn't do anything. I'm starting to think it's a firewall issue or something....

Who is online

Users browsing this forum: almdandi, netmas and 16 guests