Community discussions

MikroTik App
 
richedav
Member Candidate
Member Candidate
Topic Author
Posts: 114
Joined: Mon Dec 13, 2010 12:13 am

Strip Linefeed/EOL from fetched file

Sat Oct 15, 2011 6:27 pm

Hi All

Just started to play with Scripting which is working well, except for the problem I have with a \n appearing on the end of the file I am pulling from a website

Its a very basic script, simply pulls a SSID down from the server , and sets the SSID on the WLAN1 interface if its different to that previously suppled ( to facilitate basic management of the hotspot service from a cloud based system)

If the server outputs 'test'

Mikrotk is reporting the said as 'test\n'

Theres no trim commands, so whats the best way to remove the EOL characters?

Many thanks
 
User avatar
TealFrog
just joined
Posts: 23
Joined: Sun Oct 02, 2011 11:56 am

Re: Strip Linefeed/EOL from fetched file

Sat Nov 05, 2011 9:07 am

It may be a bit late, but actually the script I posted a day before your post does something very similar to this...

http://forum.mikrotik.com/viewtopic.php?f=9&t=55738

If you want to trim the last character, then it is just a matter of doing something like:
:put [ :pick "test\n" 0 ( [ :len "test\n" ] -1 ) ];
As another example:
:local w "test\n";
:put [ :pick $w 0 ( [ :len $w ] -1 ) ];
To get characters up to the occurrence of the end of line (newline) character:
:local line "test\n";
:put [ :pick $line 0 [ :find $line "\n" ] ];
Regards.
 
richedav
Member Candidate
Member Candidate
Topic Author
Posts: 114
Joined: Mon Dec 13, 2010 12:13 am

Re: Strip Linefeed/EOL from fetched file

Tue Nov 15, 2011 4:52 pm

Thanks for the update - added to my cut.n.paste lists of code!
 
lanhampr
newbie
Posts: 34
Joined: Wed Aug 04, 2021 7:18 pm

Re: Strip Linefeed/EOL from fetched file

Tue Dec 12, 2023 5:48 pm

Here is a script I found, modified to send JSON payload to Webhook.
It sends each DHCP Server, total pool size, total IP's in use.
Probably some items here you can pick on but it works.

/ip dhcp-server {
:local ip;
:local servertotalips;
:local servertotalleased;
:local nextpool;
:local tmp;
:local poolname;
:local webhook "https://<your URL>";
:local systemname [/system identity get name];
:local json "";
:local dhcpservertotal 0;
:local debug true;
:local servername;
:local dhcpservers [find]

:log info "Start DHCP Inventory...";
# JSON Payload start...
:set $json "[{\"servername\":\"$systemname\" ,\"dhcp-servers\":{\"entities\":[";

# Log DCHP server total count.
:log info "Total DHCP Servers: $[:len $dhcpservers]"
:foreach item in=$dhcpservers do={
:set servername [get $item name];
:log info ("Get: " . $servername);
:if ($debug) do={:put ("DHCP Server: " . $servername)};

# Get total number of leases for DHCP Server
:set servertotalleased [/ip dhcp-server lease print value-list count-only where server=$servername];
:if ($debug) do={:put ("Total Leased: " . $servertotalleased)}

# Get DHCP Server pool name.
:set poolname [get $item address-pool];

:if ($debug) do={:put ("Pool Name: " . $poolname)}
:if ($poolname = "static-only") do={
:log info ($servername . " No Pool found.")
} else={
#:log info ("Pool Name: " . $poolname);
:set tmp $poolname;

# Get next pool (if exists)
:set nextpool [/ip pool get $tmp next-pool];
#:log info ("Next Pool: " . $nextpool);
# Get pool range
:set ip [:tostr [/ip pool get $poolname ranges]];
#:log info ("IP Range: " . $ip);

# Get total number of pool IP's
:local Start [:pick $ip 0 [:find $ip "-"]];
#:log info ("IP First: " . $Start);
:local Stop [:pick $ip ([:find $ip "-"] + 1) 31];
#:log info ("IP Last: " . $Stop);
:set servertotalips ($Stop - $Start);
#:log info ("IP Range Total: " . $servertotalips);

# Loop thru each DHCP Pool and get an IP Total
:while ([:len $nextpool] != 0) do={
:set ip [:tostr [/ip pool get $nextpool ranges]];

# Get total number of pool IP's
:local Start [:pick $ip 0 [:find $ip "-"]];
:local Stop [:pick $ip ([:find $ip "-"] + 1) 31];
:local pooltotal ($Stop - $Start);

:set servertotalips ($servertotalips + $pooltotal);

:set tmp $nextpool;
:set nextpool [/ip pool get $tmp next-pool];
};

:local available ($servertotalips - $servertotalleased);

:set json ($json . "{\"name\":\"" . $servername . "\",");
:set json ($json . "\"pool-size\":\"" . $servertotalips . "\",");
:set json ($json . "\"available\":\"" . $available . "\"}");
:log info ($servername . " Pool Size: " . $servertotalips . " Available: " . $available);

:set json ($json . ",");
}
}

# Trim last ","
:set json [:pick $json 0 ( [ :len $json ] -1 )];

# Finish JSON Payload
:set json ($json . "]}}]");
:log info $json;
:if ($debug) do={:put ($json)};
# Send webhook
/tool fetch http-method=post http-header-field="Content-Type: application/json" http-data=$json url="$webhook";
}

Who is online

Users browsing this forum: No registered users and 3 guests