If your program dies on an error when you want it to keep trucking, you may want to set the erromode of the connection. There are two options for Errmode: 'die' and 'return'. The default value is 'die', so, as you can probably guess, your app will die if it runs into an error. The value 'return' will let you keep trucking along on errors, but be careful -- you'll have to handle your own errors.

The first time I encountered this was when doing batch finds on routing entries on a router. When the router didn't have a route for the target, it would give me:

Last command and router error:
Router1>show ip route 1.1.1.1
% Network not in table at ./test.pl line 16

Of course, I want to know that it didn't have the route, but I didn't want the app to stop. So, after a few minutes of research, I found the Errmode option. Here's an example.

$conn = Net::Telnet::Cisco::IOS->new( Host => "router1", Errmode => "return");

With Errmode set to "return", the error will be ignored by NTCI and continue executing your code. Of course, there is a downside to skipping over errors. You'll have to develop some code to trap any errors or parse any unexpected values and react accordingly.

Let's use the example above -- trying to get the route for 1.1.1.1. Unless you're careless using 1.1.1.1 on your network, you will never find it in any routing table (it's a reserved space), so every time you look for it, the router should give something like

Router1>sh ip route 1.1.1.1
% Network not in table

If you use NTCI to get the route, the router will return the error I had at the top. However, since we set the Errmode, the code will keep executing, so the value of $conn->errmsg() will contain "Network not in table", and we will have to trap it. Here's one way to do it:

$net = "1.1.1.1";
%route = $conn->getIPRoute( $net );
if ( $conn->errmsg() =~ /Network not in table/ )  {
        print "Route to $net not found.\n";
}
else  {
       print "Net:  $net\tProto: $route{'protocol'}\tHop: $route{'nexthop'}\n";
}

This will check get the route then check for any error messages that may be returned from the router. If the error contains the term "Network not in table", our app will print "Route to 1.1.1.1 not found". If our app gets the route without generating the error, it will print something like

Net:  1.1.1.1	Proto: BGP 4242  Hop:  2.2.2.2
Route to 3.3.3.3 not found.
© 2005-2006 -- Aaron Conaway