title: Errors while logging into routers via telnet
body: <p>I'm new to Perl and i need to write a code to log into a list of routers via Telnet, execute a series of commands, and save the output to a text file. 
  All works well, except for when a router fails to log in. I want to be able to check for a successful login, and if unsuccessful, output an error message to the text file and move on to the next router.
  Here is the part of the code that reads IP addresses, executes the commands, and saves the output to a text file:</p>
  
  <pre><code>    # foreach loop to continue reading lines from the IP address file until EOF
      foreach $ip (&lt;INPUTIP&gt;)
      {
          # output the IP address read from the file
          chomp $ip;
  
          # open the file to read commands from
          open INPUTCMD, $readCmdsFrom;   
  
          # open the file to write output to and write IP to it
          open OUTPUT, "&gt;&gt;$writeOutputTo";
          print OUTPUT "For the router with IP address $ip: \n";
  
          # telnet to router
          $session = Net::Telnet::Cisco-&gt;new(Host =&gt; "$ip");
          $session-&gt;login("$username", "$password");
  
          # foreach loop to continue reading and executing lines from the commands file until EOF
          foreach $command (&lt;INPUTCMD&gt;)
          {
              # output the command read from the file
              chomp $command;
  
              # execute the line and save output to array
              @output = $session-&gt;cmd("$command");
  
              # write the output of the command to the output file
              print OUTPUT "@output\n";
          }
  
          # close the router's session
          $session-&gt;close;
  
          # close files
          close OUTPUT;
          close INPUTIP;
          close INPUTCMD;
      }
  
      # output success message
      print "Commands successfully executed!";
  </code></pre>