Check the device IP address and node name

This section of the SNMP GET access example Perl script checks for the device's IP address and node name. Use this part of the example script as a guide to writing code that checks for a device's IP address and node name in client/server Perl scripts that retrieve SNMP information.

The SNMP GET access example Perl script checks for a device's IP address and node name as follows:

 my $nodeIP = $node;  1 

 if ($node !~ /^\d+\.\d+\.\d+\.\d+$/) {  2 

 $nodeIP = gethostbyname($node);  3 

 $nodeIP = inet_ntoa($nodeIP) if (defined $nodeIP) or

 die "Can't find IP address for '$node'";  4 

 }

The following list explains specific numbered items in the previously listed section of the SNMP GET access Perl script example:

  1. Assigns the value stored in the my $node variable to the my $nodeIP variable. The my $node variable was set after the call to the RIV::Param constructor.

    See RIV::Param Constructor for more information.

  2. Determines if an IP address or node (host) name was specified.
  3. Gets the IP address from the node name by calling the gethostbyname and inet_ntoa functions.
  4. If the defined function verifies that the value in $nodeIP is undef, consider this a fatal error and call the die function. The die function prints out an appropriate message (in this case, that the IP address for this device cannot be found) to the standard error stream.