This function will open a connection to a SNMP device.
Input:
Host : STRING
The address of the device to connect to.
Port : DINT [1..65535] (default 161)
The port of the device to connect to.
TCP : BOOL (default FALSE)
Set to TRUE if a TCP connection is required, otherwise an UDP connection is used.
The protocol uses UDP connections as default.
Version : INT (default _SNMP_VER_2C)
The SNMP version to use.
_SNMP_VER_1
|
SNMP version 1. Using community names for security.
|
_SNMP_VER_2C
|
SNMP version 2. Using community names for security.
|
_SNMP_VER_3
|
SNMP version 3. More advanced security (USM/TLS), including certificates.
|
Community : STRING (Max 63 characters)
The community string. Used in version 1 and 2c.
Security : SINT (default _SNMP_SEC_USM)
The type of security to use. Used in version 3.
_SNMP_SEC_USM
|
- The USM security model. (username and password)
|
_SNMP_SEC_TLS
|
- The TSM security model. (certificates)
|
Level : SINT (default _SNMP_SEC_ENC)
The security level used in communication (TSM security only, see overview).
_SNMP_SEC_NONE
|
- No authentication or encryption
|
_SNMP_SEC_AUTH
|
- Authentication and no encryption
|
_SNMP_SEC_ENC
|
- Authentication and encryption
|
USMUser : PTR
Pointer to a snmpUser structure. (USM security only)
EngineID : STRING (10..64 characters or empty)
The engine ID of the peer agent (TSM) or the USM users engineID when sending traps to the manager.
Certlocal : STRING
The name of the certificate to identify the local device. (TSM security only, see overview)
Certclient : STRING
The name of the certificate to identify the peer agent. (TSM security only, see overview)
Context : STRING (Max 63 characters)
The security context name to use if needed.
Output:
Handle : SYSHANDLE
The handle to the connection.
Returns: INT
1
|
- Success.
|
0
|
- This function is not supported.
|
-2
|
- Invalid parameter.
|
-3
|
- Too many client sessions. See limitations on overview page.
|
-12
|
- General error.
|
Declaration
FUNCTION snmpConnect : INT;
VAR_INPUT
Host : MANDATORY STRING;
Port : DINT := 161;
TCP : BOOL := FALSE;
Version : INT := _SNMP_VER_2C;
Community : STRING;
Security : SINT := _SNMP_SEC_USM;
Level : SINT := _SNMP_SEC_ENC;
UsmUser : PTR;
EngineID : STRING;
Certlocal : STRING;
Certclient : STRING;
Context : STRING;
Handle : ACCESS SYSHANDLE;
END_VAR;
Example:
INCLUDE rtcu.inc
VAR
iface : SINT := 2;
END_VAR;
PROGRAM test;
VAR
rc : INT;
var_str : STRING;
snmplabs : SYSHANDLE;
END_VAR;
rc := netOpen(iface := iface);
DebugFmt(Message := "netOpen (rc=\1)", v1 := rc);
WHILE NOT netConnected(iface := iface) DO
Sleep(Delay := 2000);
END_WHILE;
rc := snmpConnect(
handle := snmplabs,
community := "public",
host := "demo.snmplabs.com",
version := _SNMP_VER_2C
);
IFrc = 1THEN
rc := snmpGetString(handle := snmplabs, oid := "1.3.6.1.2.1.1.5", v := var_str);
IF rc = 1 THEN
DebugFmt(message := "snmpGetString (rc=\1): returned : '" + var_str + "'", v1 := rc);
ELSE
DebugFmt(message := "snmpGetString error. (rc=\1)", v1 := rc);
END_IF;
ELSE
DebugFmt(message := "snmpConnect failed (rc=\1)", v1 := rc);
END_IF;
...
BEGIN
...
END;
END_PROGRAM;
|