Community discussions

MikroTik App
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Write to a .txt file

Sat Apr 23, 2022 2:11 pm

Hi guys, can you help me with a script that writes a message recorded in the routerboard log to a .txt file. I would like to record in the file. txt every time I change my IP address which is dynamic Thanks for your help





EL DONCITO.
 
msatter
Forum Guru
Forum Guru
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Write to a .txt file

Sat Apr 23, 2022 2:17 pm

Time to learn some scripting yourself. You requested that many scripts by now, that you could make a living from that.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Write to a .txt file

Sat Apr 23, 2022 2:20 pm

example of the message?

you have already obtained something:
viewtopic.php?t=184758#p923795
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3250
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Write to a .txt file

Sat Apr 23, 2022 6:54 pm

Hi guys, can you help me with a script that writes a message recorded in the routerboard log to a .txt file. I would like to record in the file. txt every time I change my IP address which is dynamic Thanks for your help
Depending on what your trying to do, you may not need a script. the System>Logging>Action lets you create a "custom log action" for "disk" where you can pick file name, length, etc. Then in the System>Logging>Rules you can add something for the "dhcp,info" topics. Like so:
/system logging action
add disk-file-count=1 disk-file-name=flash/ipchg name=ipchg target=disk
/system logging
add action=ipchg topics=dhcp,info

Otherwise, /ip/dhcp-client has the ability to add a script. How this works is your script has access to variables created by the dhcp-client, like if it got an address ($bound=1), etc You can use these variable however, including writing the ($"gateway-address") to a file.
See what you can get here: https://wiki.mikrotik.com/wiki/Manual:I ... pt_example
This parameter is available in v6.39rc33+ These are available variables that are accessible for the event script:
bound - 1 - lease is added/changed; 0 - lease is removed
server-address - server address
lease-address - lease address provided by server
interface - name of interface on which client is configured
gateway-address - gateway address provided by server
vendor-specific - stores value of option 43 received from DHCP server
lease-options - array of received options
 
kevinds
Long time Member
Long time Member
Posts: 638
Joined: Wed Jan 14, 2015 8:41 am

Re: Write to a .txt file

Sun May 15, 2022 11:18 pm

This is the 'checkIP' script that I use, scheduled to run every 5 minutes.

 
  :global actualIP;
 
  :local newIP [/ip address get [find interface="ether1"] address];
 
  :if ($newIP != $actualIP) do={
    :put "ip address $actualIP changed to $newIP";
    :set actualIP $newIP;
    /system script run THIS-RUNS-WHEN-THE-IP-CHANGES;
  }
Then make the second script do whatever you want, when the IP has changed..
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Re: Write to a .txt file

Mon May 16, 2022 12:07 am

This is the 'checkIP' script that I use, scheduled to run every 5 minutes.

 
  :global actualIP;
 
  :local newIP [/ip address get [find interface="ether1"] address];
 
  :if ($newIP != $actualIP) do={
    :put "ip address $actualIP changed to $newIP";
    :set actualIP $newIP;
    /system script run THIS-RUNS-WHEN-THE-IP-CHANGES;
  }
Then make the second script do whatever you want, when the IP has changed..



No friend, I use this:
:global filenameIP "iphistory.txt"
:global arrMonths {jan="01";feb="02";mar="03";apr="04";may="05";jun="06";jul="07";aug="08";sep="09";oct="10";nov="11";dec="12"}
{
             :local data [/system clock get date]
             :local itime  [/system clock get time]
             :local hours  [:pick $itime 0 2]
             :local minsec [:pick $itime 2 8]
             :local mhours [:tonum $hours]
             :local msymbl "AM"
             :if ($mhours > 11) do={:set msymbl "PM"}
             :set mhours ($mhours % 12)
             :if ($mhours = 0) do={:set mhours 12}
             :if ($mhours < 10) do={:set mhours "0$mhours"}
             :local mtime "$mhours$minsec $msymbl"
:global now "TIME:$mtime   HORA: $[:pick $data 4 6]-$($arrMonths->[:pick $data 0 3])-$[:pick $data 7 11]"
}
/file
:if ([:len [find where name=$filenameIP]] = 0) do={print file="$filenameIP"; :delay 5s; set $filenameIP contents=""}
:global currentIP
:local newIP [/ip address get [find interface="ETHERT 1"] address]
:set newIP [:pick $newIP 0 [:find $newIP "/" -1]]
:if ($newIP != $currentIP) do={
    /file
    :delay 5s
    :local filecontent [get $filenameIP contents]
    :local newfilecontent "********************************************************************\r\nNEW IP:     $newIP\t$now\r\n$filecontent"
    set $filenameIP contents=$newfilecontent
    :set currentIP $newIP
}
Try it to see


EL DONCITO.

Who is online

Users browsing this forum: marcelofares and 32 guests