# Lua Utility Methods and Classes

# Class Scheduler

Provides a set of methods for running scripts on a schedule.

The instance is created automatically when the script is called and is available through the scheduler variable.

# Method curl

Takes a table of arguments (array of keys-values).

# Parameters of curl

Parameter Example Default value Description
method "POST" "GET" Allowable (according to 4.3 RFC 7231 (opens new window)) HTTP 1.1 method for making a request.
uri "http://my-userspace.acure.io" Absent Acceptable (according to RFC 2396 (opens new window)) URI of resourse.
body "[{ "key": "value" }]" Absent The body of the request. An arbitrary string.
mediaType "application/json" "text/plain" MIME-types -- data types that can be transmitted over the Internet using the MIME standard.
headers { ["accept-encoding"] = "gzip, deflate" } Absent A table of keys and their values (array of keys-values).
proxy { address = "http://1.1.1.1:8080", bypassList = { "http://url1", "http://url2" }, credentials = { username = "guest", password = "guest" } } Absent Request proxying settings.
address – Acceptable (according to RFC 2396 (opens new window)) URI of resource.
bypassList – an array of addresses or regular expressions describing addresses that will not use a proxy for requests to them.
credentials – credentials for authentication on the proxy server:
username – user name.
password – user password.
basicAuth { login = "guest", password = "guest" } Absent Basic authorization settings for the request.
login – user name.
password – user password.

# Curl response model

If an error occurs, then data = nil, err ~= nil. If the response is normal, then data ~= nil, err = nil.

{
    data = {
        statusCode = 200,
        headers = {
            ["Server"] = "nginx/1.10.3",
            ["Date"] = "Wed, 16 Oct 2019 07:00:09 GMT"
        },
        content = "content string",
        isSuccessStatusCode = true
    },
    err = {
        message = "Error"
    }
}

# Example of curl

Basic authorization through an open proxy:

local args =
{
  uri = "https://jigsaw.w3.org/HTTP/Basic/",
  proxy = {
    address = "http://212.220.216.70:8080"
  },
  basicAuth = {
    login = "guest",
    password = "guest"
  }
}
local response = curl(args)
if (response.err ~= nil) then
  print(response.err)
  return -- Exit the script early if an error occurs
end
  
if (response.data.isSuccessStatusCode) == true then
  print(response.data.content)
else
  print(":( status code " .. response.data.statusCode)
end

# Method ssh

Takes a table of arguments (array of keys-values).

# Parameters of ssh

Parameter Type Example Default value
host string "http://sample.ssh.com/" Absent
port number 23 22
username string "demo-user" Absent
password string "demo-password" Absent
privateKey string "0KHQtdC60YDQtdGC0L3QvtC1INC/0L7RgdC70LDQvdC40LUgO15Q" Absent
command string echo "I have a bad feeling about this." Absent

# Example of ssh

local args =
{
  host = "sample.ssh.com",
  command = "ls /tmp/doc"
}
local response = ssh(args)
if response.data.exitStatus == 0 then
  print(response.data.result)
else
  print(":( error " .. response.data.error)
end

# Method snmp

Sends messages to an email server.

# Parameters of snmp

Parameter Example Default value
ip "64.233.163.94" Absent
community "public" "public"
version "2c" "1"
level "authnopriv" "noauthnopriv"
method "walk" "get"
timeout 10000 1000
maxrepetitions 1 10
mode "subtree" "all"
data "s Shanghai" Absent
oid "1.3.6.1.2.1.1.6.0" Absent
user "privacy" ""
contextname "" ""
authentication "md5" ""
authphrase "authentication" ""
privphrase "privacyphrase" ""

# Example of snmp

Sophisticated snmpget of version 3:

local args =
{
  ip = "192.168.10.1",
  oid = "1.3.6.1.2.1.1.1.0",
  version = 3,
  level = "authPriv",
  authentication = "authentication",
  privphrace = "privacyphrase",
  user = "privacy"
}
local response = snmp(args)
print(response.data)

Simple snmpwalk of version 1:

local args =
{
  ip = "192.168.10.1",
  community = "public",
  oid = "1.3.6.1.2.1.1.1.0",
  method = "walk",
  mode = "subtree",
  maxrepetitions = 5
}
local response = snmp(args)
print(response.data)

# Method smtp

# Parameters of smtp

Parameter Example Description
host The address of the sending server (IP address or DNS name)
port 25 Port number on the server
secureMode "None" / "Auto" / "SslOnConnect" / "StartTls" / "StartTlsWhenAvailable" Forces the encryption mode.
username Username of the sender (optional, if available, authentication is performed on the server).
password Sender user password (optional).
subject Letter subject
body Text of the letter
mailType Message display type (plain, html)
from { title = "Notifications department", address = "sender@server.net" } Sender address (must match the format of the email address, if it doesn't match, the script will end with an error)
recipients { "overwatchers@gmail.com", "overwatchers@yahoo.com", "tester@gmail.com" } List of recipient addresses (addresses that do not match the email address format will be excluded from the mailing list)

# smtp response model

On success, data = true. If an error occurs, data = false,err - an exception object with an error message error.Message.

# Example of smtp

Sending a message to one recipient:

local = smtp({
  host = "server.mail.com",
  port = 25,
  useSsl = true,
  username = "user",
  password = "pass",
  subject = "Event notification",
  body = "Dear participants, ...",
  mailType = "plain",
  from = "sender@server.net",
  recipients = "overwatchers@server.com"
})
if (response.data) == false then print(response.err) end

# Method regexIsMatch(string input, string pattern)

Checks if the specified input string matches the specified regular expression.

# Parameters of regexIsMatch

  • input – input string.
  • pattern – regular expression (PCRE dialect).

# regexIsMatch response model

  • true – if it matches.
  • false – if does not match.

# Example of regexIsMatch

regexIsMatch("user@mailbox.net", "(\\w+@[a-zA-Z_]+?\\.[a-zA-Z]{2,6})")

# Method regexMatch(string input, string pattern)

Searches for the first occurrence of the specified regular expression in the specified input string .

# Parameters of regexMatch

  • input – input string.
  • pattern – regular expression (PCRE dialect).

# Example of regexMatch

Prints all email addresses contained in a string to the console:

local result = regexMatch([[test1@test.com test2 test3@test.com test4@test4com]], [[(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})]])
while result.Success == true do
  print (result.Value)
  result=result:NextMatch()
end

# Method regexReplace(string input, string pattern, string replacement)

Replaces all substrings in the input string that match the specified regular expression with the specified value.

# Parameters of regexReplace

  • input – input string.
  • pattern – regular expression (PCRE dialect).
  • replacement – replacement string.

# regexReplace response model

  • string – string with the replaced substrings.

# Example of regexReplace

Replaces all found email addresses with the string email:

local result = regexReplace([[test1@test.com test2 test3@test.com test4@test4com]], [[(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})]], [[email]])
print (result)

# Method regexSplit(string input, string pattern)

Splits the input string into an array of substrings at positions specified by a regular expression pattern.

# Parameters of regexSplit

  • input – input string.
  • pattern – regular expression (PCRE dialect).

# regexSplit response model

  • string[] – array of substrings.

# Example of regexSplit

Splits the input string into an array of email addresses:

local result = regexSplit([[test1@test.com test2 test3@test.com test4@test4com]], [[(\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})]])
for i = 0, result.Length-1 do
   print (result:GetValue(i))
end

# Method scheduleAfter

Schedule a script launch after <seconds> seconds, starting from the moment the method was called.

# Parameters of scheduleAfter

  • seconds – the number of seconds after which the script will be launched, starting from the moment the method was called.

# scheduleAfter response model

  • LuaResult<boolean> – result of the method execution.
  • True – the script is scheduled
  • False – there were problems with submitting the script to the scheduler.
  • result.err – error details.

# Example of scheduleAfter

GET request to get a JSON resource at a given path:

if (isScheduled == true) then
  print("The script was launched by the scheduler.")
else
  scheduler:executeAfter(60) -- Schedule script execution 60 seconds after calling this method 
  print("The script has been scheduled.")
end