matchport
Filters input records using a port group. Records where the port or protocol value is null or the type does not match are discarded.
Command properties
| Property | Description |
|---|---|
| Command type | Transforming |
| Required permission | None |
| License usage | N/A |
| Parallel execution | Supported |
| Distributed execution | Runs on Data Node (mapper) |
Syntax
In the syntax above, name is available starting with 4.0.2609.0.
Options
guid=STR- GUID of the port group to use for matching. In the 5.0 series (through 5.0.2605.2), this is the only way to specify the target. In the 4.0 series (4.0.2609.0 and later), this option cannot be used together with
name, and you must specify eitherguidorname. name=STR- Name of the port group to use for matching (cannot be used together with
guid). You must specify either theguidornameoption. This option is available starting with 4.0.2609.0. port=STR- Name of the field containing the port number. (Default:
port) protocol=STR- Name of the field containing the protocol. Recognizes
tcp,udp, andicmpvalues. (Default:protocol) Any other string is treated the same as omitting the protocol, matching against both TCP and UDP. invert=BOOL- When set to
t, discards records that match the port group and outputs only non-matching records. (Default:f) verify=BOOL- When set to
f, skips port group validation at the parse stage. (Default:t)
Input fields
| Field | Type | Required | Description |
|---|---|---|---|
port | integer | Required | Port number. You can specify a different field name using the port option. Records where the value is not a numeric type are discarded. |
protocol | string | Required | Protocol name. You can specify a different field name using the protocol option. Records where the value is not a string are discarded. |
Error codes
Parse errors
| Error code | Message | Description |
|---|---|---|
| 300010 | cannot use both field and port option of matchport | Both the field and port options were specified at the same time. |
| 300011 | cannot use both field and protocol option of matchport | Both the field and protocol options were specified at the same time. |
| 300012 | Specify 'guid' option in the matchport command. | The guid option is not specified. The error code differs by series — the 5.0 series (through 5.0.2605.2) reports 300012; the 4.0 series (4.0.2609.0 and later) reports 300056 instead. |
| 300013 | Port group not found. | No port group exists for the specified GUID (validated only when verify=t). |
| 300014 | Cannot load port group. | An error occurred while loading port group data. |
| 300056 | Specify 'guid' or 'name' option in the matchport command. | Neither the guid nor the name option is specified. Available starting with 4.0.2609.0, which has the name option. |
| 300057 | Port group not found by name. | No port group exists with the name specified in the name option. Available starting with 4.0.2609.0, which has the name option. |
| 300058 | Duplicate port group name. | More than one port group exists with the name specified in the name option. Available starting with 4.0.2609.0, which has the name option. |
| 300059 | Specify only one of 'guid' or 'name' option in the matchport command. | Both the guid and name options are specified. Available starting with 4.0.2609.0, which has the name option. |
| 300060 | The GUID in the matchport command is invalid format. | The guid option value is not a valid GUID. Available starting with 4.0.2609.0. |
Runtime errors
None
Description
The matchport command checks whether the port number and protocol value of input records are included in the specified port group, and outputs only matching records. When invert=t is specified, only non-matching records are output.
Use the guid option to specify the port group to use for matching. In the 4.0 series (4.0.2609.0 and later), you can also use the name option; the command looks up the group by that name to resolve its GUID, and specifying both guid and name, or omitting both, causes an error, as does specifying a name that does not exist or that matches more than one group.
If the port field value is not a numeric type or the protocol field value is not a string, the record is discarded regardless of the invert option.
In a distributed environment, filtering runs on the Data Node.
Examples
-
Filter using a port group
json "[{'port': 80, 'protocol': 'tcp'}, {'port': 53, 'protocol': 'udp'}, {'port': 8080, 'protocol': 'tcp'}]" | matchport guid=550e8400-e29b-41d4-a716-446655440000Outputs only records included in the specified port group.
-
Extract records not matching a port group
json "[{'port': 80, 'protocol': 'tcp'}, {'port': 53, 'protocol': 'udp'}, {'port': 8080, 'protocol': 'tcp'}]" | matchport guid=550e8400-e29b-41d4-a716-446655440000 invert=tUses the
invert=toption to output only records not included in the port group. -
Specify custom field names
json "[{'dst_port': 443, 'proto': 'tcp'}, {'dst_port': 53, 'proto': 'udp'}]" | matchport guid=550e8400-e29b-41d4-a716-446655440000 port=dst_port protocol=protoSpecifies the port number field and protocol field names using the
portandprotocoloptions. -
Specifying a port group by name (4.0 series, 4.0.2609.0 and later)
json "[{'port': 80, 'protocol': 'tcp'}, {'port': 53, 'protocol': 'udp'}]" | matchport name=web_portsMatches using a port group specified by name instead of GUID.
Change history
| Target | Introduced in |
|---|---|
The matchport command | Before Sonar 4.0 |
The name option | 4.0.2609.0 |