|
InterMapper's TCP Probes establish a connection to a remote system, exchange commands and receive responses, and then set the status of the device based on those responses.
This note describes how probe writers can use regular expressions and comparisons to parse out information from the responses.
The Overall Process
There is an annotated FTP probe in the Developer Guide. This gives an overview of the script language and shows how it connects and logs into a FTP server, how a script can respond to error conditions, and how to set the device's status based on those conditions.
The following commands are all documented in the TCP Probe reference section of the Developer Guide.
Regular Expressions
The TCP Script Language uses the MTCH command to compare a response string to expected values. It can also use a regular expression to match on a part of a string. For example:
MTCH "A([BCD]+)E"r else goto @NOMATCH
STOR "testval" "${1}"
If the incoming line contains ABDE, then the "testval" variable will contain "BD"
In the MTCH regular expression, enclosing something in parentheses turns
it into a capturing subgroup. The one or more Bs, Cs or Ds that it's
matching will be stored in the ${1} variable. If you have several
capturing groups, they get stored in ${2}, ${3}, etc.
Calculations in Scripts
To perform calculations within a TCP script, you should use the EVAL command. Its argument is an expression (in quotes) that will be evaluated. It usually contains an assignment (with the ":=" operator), that sets a variable to the result of the expression. For example:
EVAL $celsius := (($fahrenheit - 32) * 5 / 9)
will set the variable $celsius to the temperature that corresponds to the $fahrenheit variable. The value of the $celsius variable can be used in subsequent statements.
Comparisons in Scripts
You can use the EVAL statement to make comparisons between either strings or numeric (either integer or floating point) values. To do this, write an EVAL statement that compares the two values and set the result in a new variable. If the comparison was true, then the resulting variable will be set to 1, otherwise it will be zero. Here are examples of comparing numeric and string values
Comparing Numeric Values
EVAL $x := ($val > 50.5)
NBNE #$x #0 @greater
@less:
...
GOTO @ENDIF
@greater:
...
GOTO @ENDIF
@ENDIF:
|
Comparing String values
EVAL $x := ("dog" > "cat")
NBNE #$x #0 @dog
@cat:
...
GOTO @ENDIF
@dog:
...
GOTO @ENDIF
@ENDIF:
|
Simple Comparisons in Scripts
InterMapper TCP Scripts can compare two string or integer numeric values and branch based on the results. The commands below are no longer preferred as the EVAL statement described above is equally simple and more powerful.
The SBNE ("String Branch Not Equal") compares the two string values and branches if they are not equal. One or both of the arguments can be variables, expressed as ${variable-name}.
The NBNE ("Numeric Branch Not Equal") and NBGT ("Numeric Branch Greater Than") compares two numeric values, branching on the result. The arguments to these commands are strings and are expected to be within quotes. To convert a string to a numeric value, place a number sign (#) before the parameter. For example:
STOR "val1" "100"
STOR "val2" "50"
NBGT #${val1} #${val2} @exit
In this example, the string ${val1} will be converted to the numeric value 100, and ${val2} will be converted to the numeric value 50, and the branch will be taken, because 100 is greater than 50.
Note: The NBGT, NBNE and other TCP probe commands expect integer arguments only (with an optional + or -). A script parses up to the first non-digit character. Thus, the value of "50.5" is 50; the remaining digits are ignored. If you wish to compare against a fraction or floating point value, use the EVAL statement described above.
|