Hello there. I have been trying to logon to switches using an array to collect switch IPs and then log onto the switches. I got 2 different ways that I have to use to log on to the switches depending on the configuration of the switches. Some of the switches require a username and password and some only require a password, here is my code for both ways:

#login 1 foreach $host ( @hosts ) { my $conn = Net::Telnet::Cisco::IOS->new( +HOST => $host); # connect to the host $conn->login( Name => $username, Password => $password); $conn->close; }; #Login 2 foreach $host ( @hosts ) { my $conn = Net::Telnet::Cisco::IOS->new( +HOST => $host); # connect to the host $conn->login( Passcode => $passw); $conn->close; };

Both these login ways is tested and works on switches with the different configs. What I want to do is to have both of these in one script so that I can run through the whole range of switches. I was thinking of using an IF statement, but being a noob I dont really know where to start. This is what I was thinking about doing:

foreach $host ( @hosts ) { my $conn = Net::Telnet::Cisco::IOS->new +(HOST => $host); # connect to the host if ( $conn->login( Name => $username, Password => $password)); else( $conn->login( Passcode => $passw));

I know my coding layout is wrong and I am not looking to be spoon fead, but would like someone to point me in the right direction. Thanks


In reply to Net telnet Cisco Login by sout11

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.