Community discussions

MikroTik App
 
elico
Member Candidate
Member Candidate
Topic Author
Posts: 143
Joined: Mon Nov 07, 2016 3:23 am

Trying to create a multiline file using Ruby API client

Tue Mar 23, 2021 1:15 am

I am writing a keep-alive script from a proxy server to RB.
I have an array of proxies which I am managing from a master node.
The master keeps track on the proxies and updates the MT if one or more proxies are down and changes the mangle rules accordingly.
Instead of re-creating the whole rules or looking up for rules from the master what I want to is to write a static script on the RB.
To make this script work I need some files to be created on the RB flash drive.
I am able to create a file with content but no new lines in it.
The script translates the content as a part of the command.

I want to be able to create a file with couple lines.
Currently I am fetching a file from github or a local git but I am yet to know how to do that from a script.
I was thinking about using the SMB share which might be more convenient however yet to try it.
Any recommendations and help are welcome.
#!/usr/bin/env ruby

require 'rubygems'
require 'mtik'

# Be verbose in output
MTik::verbose = true

admin_user = "admin"
host = "10.200.191.3"
password = "superduperpasswordl1"

flag_file = "test.1"

arr1 = ["/file/print","=file=#{flag_file}.txt" ]
arr2 = ["/file/set", "=.id=#{flag_file}.txt", "=contents=1"]

connection = MTik::command(
  :host    => host,
  :user    => admin_user,
  :pass    => flag_file,
  :unencrypted_plaintext => true,
  :command => arr1,
  :limit => 10  ## Auto-cancel after 10 replies
)
connection = MTik::command(
  :host    => host,
  :user    => admin_user,
  :pass    => flag_file,
  :unencrypted_plaintext => true,
  :command => arr2,
  :limit => 10  ## Auto-cancel after 10 replies
)
The output is:
SENDING PLAINTEXT PASSWORD OVER UNENCRYPTED CONNECTION
<<< '/login' (6)
<<< '=name=admin' (11)
<<< '=password=superduperpasswordl1' (19)
<<< '.tag=0' (6)
<<< END-OF-SENTENCE

>>> '!done' (5)
>>> '.tag=0' (6)
>>> END-OF SENTENCE

<<< '/system/resource/getall' (23)
<<< '.tag=1' (6)
<<< END-OF-SENTENCE

>>> '!re' (3)
>>> '.tag=1' (6)
>>> 'uptime=1w2d4h46m17s' (19)
>>> 'version=6.48.1 (stable)' (23)
>>> 'build-time=Feb/03/2021 10:54:22' (31)
>>> 'factory-software=6.7' (20)
>>> 'free-memory=101588992' (21)
>>> 'total-memory=134217728' (22)
>>> 'cpu=MIPS 74Kc V4.12' (19)
>>> 'cpu-count=1' (11)
>>> 'cpu-frequency=600' (17)
>>> 'cpu-load=6' (10)
>>> 'free-hdd-space=113270784' (24)
>>> 'total-hdd-space=134217728' (25)
>>> 'write-sect-since-reboot=1606' (28)
>>> 'write-sect-total=12106' (22)
>>> 'bad-blocks=0' (12)
>>> 'architecture-name=mipsbe' (24)
>>> 'board-name=RB2011UiAS' (21)
>>> 'platform=MikroTik' (17)
>>> END-OF SENTENCE

>>> '!done' (5)
>>> '.tag=1' (6)
>>> END-OF SENTENCE

<<< '/file/print' (11)
<<< '=file=test.1.txt' (16)
<<< '.tag=2' (6)
<<< END-OF-SENTENCE

>>> '!re' (3)
>>> '.tag=2' (6)
>>> '.id=*1F010108' (13)
>>> 'name=skins' (10)
>>> 'type=directory' (14)
>>> 'creation-time=jan/01/1970 02:00:02' (34)
>>> END-OF SENTENCE

>>> '!re' (3)
>>> '.tag=2' (6)
>>> '.id=*1F010115' (13)
>>> 'name=auto-before-reset.backup' (29)
>>> 'type=.backup file' (17)
>>> 'size=1' (6)
>>> 'creation-time=mar/23/2021 00:21:49' (34)
>>> 'contents=2' (10)
>>> END-OF SENTENCE

>>> '!re' (3)
>>> '.tag=2' (6)
>>> '.id=*1F01013D' (13)
>>> 'name=supout.rif' (15)
>>> 'type=.rif file' (14)
>>> 'size=1' (6)
>>> 'creation-time=mar/23/2021 00:20:06' (34)
>>> 'contents=1' (10)
>>> END-OF SENTENCE

>>> '!re' (3)
>>> '.tag=2' (6)
>>> '.id=*1F010151' (13)
>>> 'name=email.backup' (17)
>>> 'type=backup' (11)
>>> 'size=340152' (11)
>>> 'creation-time=mar/04/2020 03:22:46' (34)
>>> END-OF SENTENCE

>>> '!re' (3)
>>> '.tag=2' (6)
>>> '.id=*1F0101AD' (13)
>>> 'name=backup.rsc' (15)
>>> 'type=script' (11)
>>> 'size=149977' (11)
>>> 'creation-time=mar/04/2020 03:22:49' (34)
>>> END-OF SENTENCE

>>> '!re' (3)
>>> '.tag=2' (6)
>>> '.id=*1F01020C' (13)
>>> 'name=newFile.txt' (16)
>>> 'type=.txt file' (14)
>>> 'size=1430' (9)
>>> 'creation-time=mar/23/2021 00:24:42' (34)
>>> 'contents=# mar/23/2021  0:24:42 by RouterOS 6.48.1' (50)
>>> END-OF SENTENCE

>>> '!re' (3)
>>> '.tag=2' (6)
>>> '.id=*1F01020E' (13)
>>> 'name=autosupout.rif' (19)
>>> 'type=.rif file' (14)
>>> 'size=594372' (11)
>>> 'creation-time=jun/21/2020 16:00:41' (34)
>>> END-OF SENTENCE

>>> '!re' (3)
>>> '.tag=2' (6)
>>> '.id=*1F01022F' (13)
>>> 'name=ngtech-05202.rsc' (21)
>>> 'type=script' (11)
>>> 'size=24729' (10)
>>> 'creation-time=may/27/2020 07:38:07' (34)
>>> END-OF SENTENCE

>>> '!re' (3)
>>> '.tag=2' (6)
>>> '.id=*1F010232' (13)
>>> 'name=pub' (8)
>>> 'type=directory' (14)
>>> 'creation-time=aug/14/2019 18:48:36' (34)
>>> END-OF SENTENCE

>>> '!re' (3)
>>> '.tag=2' (6)
>>> '.id=*1F010259' (13)
>>> 'name=ngtech-052020-hide.rsc' (27)
>>> 'type=script' (11)
>>> 'size=24615' (10)
>>> 'creation-time=may/27/2020 07:43:32' (34)
>>> END-OF SENTENCE

<<< '/cancel' (7)
<<< '=tag=2' (6)
<<< '.tag=3' (6)
<<< END-OF-SENTENCE

>>> '!re' (3)
>>> '.tag=2' (6)
>>> '.id=*1F01030D' (13)
>>> 'name=ip-firewall.rsc' (20)
>>> 'type=script' (11)
>>> 'size=12411' (10)
>>> 'creation-time=sep/24/2020 21:54:40' (34)
>>> END-OF SENTENCE

>>> '!re' (3)
>>> '.tag=2' (6)
>>> '.id=*1F010312' (13)
>>> 'name=test.1.txt' (15)
>>> 'type=.txt file' (14)
>>> 'size=1' (6)
>>> 'creation-time=mar/23/2021 01:08:44' (34)
>>> 'contents=' (9)
>>> END-OF SENTENCE

>>> '!re' (3)
>>> '.tag=2' (6)
>>> '.id=*1F010370' (13)
>>> 'name=MT-LB-RB2011UiAS-dhcp.rsc' (30)
>>> 'type=script' (11)
>>> 'size=23722' (10)
>>> 'creation-time=jan/01/2002 03:04:55' (34)
>>> END-OF SENTENCE

>>> '!re' (3)
>>> '.tag=2' (6)
>>> '.id=*1F0103A7' (13)
>>> 'name=MT-LB-RB2011UiAS-firewall.rsc' (34)
>>> 'type=script' (11)
>>> 'size=6309' (9)
>>> 'creation-time=jan/01/2002 03:05:03' (34)
>>> END-OF SENTENCE

>>> '!re' (3)
>>> '.tag=2' (6)
>>> '.id=*1F0103BB' (13)
>>> 'name=MT-LB-RB2011UiAS-rest.rsc' (30)
>>> 'type=script' (11)
>>> 'size=11930' (10)
>>> 'creation-time=jan/01/2002 03:05:03' (34)
>>> END-OF SENTENCE

>>> '!re' (3)
>>> '.tag=2' (6)
>>> '.id=*1F0103CF' (13)
>>> 'name=MT-LB-RB2011UiAS-dns.rsc' (29)
>>> 'type=script' (11)
>>> 'size=4016' (9)
>>> 'creation-time=jan/01/2002 03:05:03' (34)
' (10)ontents=
>>> END-OF SENTENCE

>>> '!re' (3)
>>> '.tag=2' (6)
>>> '.id=*1F01040F' (13)
>>> 'name=MT-LB-RB2011UiAS-interfaces.rsc' (36)
>>> 'type=script' (11)
>>> 'size=3000' (9)
>>> 'creation-time=jan/01/2002 03:02:20' (34)
' (51)ontents=# aug/14/2019 11:57:12 by RouterOS 6.44.5
>>> END-OF SENTENCE

>>> '!re' (3)
>>> '.tag=2' (6)
>>> '.id=*1F01044F' (13)
>>> 'name=mt-RB-RB2011-email.backup' (30)
>>> 'type=backup' (11)
>>> 'size=189404' (11)
>>> 'creation-time=jan/01/2002 03:12:55' (34)
>>> END-OF SENTENCE

>>> '!done' (5)
>>> '.tag=2' (6)
>>> END-OF SENTENCE

>>> '!done' (5)
>>> '.tag=3' (6)
>>> END-OF SENTENCE

<<< '/quit' (5)
<<< '.tag=4' (6)
<<< END-OF-SENTENCE

>>> '!fatal' (6)
>>> 'session terminated on request' (29)
>>> END-OF SENTENCE

SENDING PLAINTEXT PASSWORD OVER UNENCRYPTED CONNECTION
<<< '/login' (6)
<<< '=name=admin' (11)
<<< '=password=superduperpasswordl1' (19)
<<< '.tag=5' (6)
<<< END-OF-SENTENCE

>>> '!done' (5)
>>> '.tag=5' (6)
>>> END-OF SENTENCE

<<< '/system/resource/getall' (23)
<<< '.tag=6' (6)
<<< END-OF-SENTENCE

>>> '!re' (3)
>>> '.tag=6' (6)
>>> 'uptime=1w2d4h46m17s' (19)
>>> 'version=6.48.1 (stable)' (23)
>>> 'build-time=Feb/03/2021 10:54:22' (31)
>>> 'factory-software=6.7' (20)
>>> 'free-memory=101588992' (21)
>>> 'total-memory=134217728' (22)
>>> 'cpu=MIPS 74Kc V4.12' (19)
>>> 'cpu-count=1' (11)
>>> 'cpu-frequency=600' (17)
>>> 'cpu-load=6' (10)
>>> 'free-hdd-space=113270784' (24)
>>> 'total-hdd-space=134217728' (25)
>>> 'write-sect-since-reboot=1608' (28)
>>> 'write-sect-total=12108' (22)
>>> 'bad-blocks=0' (12)
>>> 'architecture-name=mipsbe' (24)
>>> 'board-name=RB2011UiAS' (21)
>>> 'platform=MikroTik' (17)
>>> END-OF SENTENCE

>>> '!done' (5)
>>> '.tag=6' (6)
>>> END-OF SENTENCE

<<< '/file/set' (9)
<<< '=.id=test.1.txt' (15)
<<< '=contents=1' (11)
<<< '.tag=7' (6)
<<< END-OF-SENTENCE

>>> '!done' (5)
>>> '.tag=7' (6)
>>> END-OF SENTENCE

<<< '/quit' (5)
<<< '.tag=8' (6)
<<< END-OF-SENTENCE

>>> '!fatal' (6)
>>> 'session terminated on request' (29)
>>> END-OF SENTENCE

Who is online

Users browsing this forum: No registered users and 31 guests