Let's look at some examples, shall we? I would never, EVER deploy these in a production environment, but they do show what NTCI can do. If you want to use these as a starting point for your own scripts, there needs to be some error and privilege level checking added, as well as a better file-naming system. If you have any questions, open a Support Request.

Configuration Backup
Below is a simple (very simple) script to log into a list of IOS devices and write the configuration to file. Like I sai, I would never deploy this script to do production work, but it does demonstrate how NTCI can be used in a custom backup application.
1   use  Net::Telnet::Cisco::IOS;  # import NTCI
2   $username = "username";        # the username to use when logging in
3   $password = "password";        # the matching password
4   @hosts = qw(    switch1        # an array of hosts to log into
                    router1
                    switch2
                    router2
                    );
5   foreach $host ( @hosts )  {    # go through the array one element at a time
6         my $conn = Net::Telnet::Cisco::IOS->new(HOST => $host);  # connect to the host
7         $conn->login(   Name => $username,        #  Log into the device
                          Password => $password);
8         @output = $conn->getConfig();  # Put the config in an array
9         $outfile = ">" . $host . "-confg";  # create a filename called ">host-confg"
10        open ( OUTFILE, $outfile );         # open a file for writing
11        print "Writing $host to file\n";    # write a status message
12        print ( OUTFILE @output );          # print the config to file
13        close OUTFILE;                      # close the file
14        $conn->close;                       # close the connection to the device
15  }

Let's go through them one at a time.

Line 1: Import the NTCI library. This is basic Perl stuff that everyone should know.
Line 2: Declare a variable to be used as the username for your IOS device.
Line 3: Declare a variable to be used as the password.
Line 4: Create an array called "hosts" that contains a list of the hosts you want to backup.
Line 5: Go through the @hosts array one host at a time and do some stuff.
Line 6: Create a new connection to the host using the element of the array.
Line 7: Login to the device with the username and password declared in lines 2 and 3.
Line 8: Get the configuration and put it in an array called "output".
Line 9: Declare a variable called "outfile" to be used as the file to write the configuration to. The name of the file will be ">host-confg" where "host" is the hostname we're backing up right now.
Line 10: Open the file we just named.
Line 11: Tell the world that we're writing the configuration to file.
Line 12: Write the output to the file we opened.
Line 13: Close the file.
Line 14: Close the telnet connection to the device.


Low Port Speed/Duplex Report
This script looks through every Ethernet interface of every device in the list to find ports that are either running at 10M or half-duplex. If it finds one, it prints a comma-separated summary line listing the hostname, interface, speed, duplex, and all the MAC addresses on that the device thinks is one it.
Again, there's no way I'd use this in production, blah, blah, blah.
1   use  Net::Telnet::Cisco::IOS;
2   $username = "username";
3   $password = "password";
4   @hosts = qw(    switch1
                   switch2
                   routerwithethernets1
                   switch3
                   );
5   foreach $host ( @hosts )  {
6           my $conn = Net::Telnet::Cisco::IOS->new(HOST => $host);
7           $conn->login(   Name => $username,
                           Password => $password );
8           @ints = $conn->listInts();
9           foreach $int ( @ints )  {
10                 $int =~ s/\s+$//g;
11                 if ( $int =~ /Ethernet/ )  {
12                         %state = $conn->getIntState( $int );
13                         $speed = $conn->getEthSpeed( $int );
14                         $duplex = $conn->getEthDuplex( $int );
15                         if ( $state{'lineprotocol'} =~ /up/ )  {
16                                 if ( $speed eq "10" || $duplex eq "half" )  {
17                                         @mac = $conn->getIntCAM( $int );
18                                         print "$host,$int,$speed,$duplex,@mac\n";
19                                 }
20                         }
21                 }
22         }
23         $conn->close;
24  }

Here we go again:

Line 1: Import the NTCI library. This is basic Perl stuff that everyone should know.
Line 2: Declare a variable to be used as the username for your IOS device.
Line 3: Declare a variable to be used as the password.
Line 4: Create an array called "hosts" that contains a list of the hosts to analyze.
Line 5: Go through the @hosts array one host at a time and do some stuff.
Line 6: Create a new connection to the host using the element of the array.
Line 7: Login to the device with the username and password declared in lines 2 and 3.
Line 8: Get a list of all the interfaces and put them in an array called "ints".
Line 9: Go through all the interfaces one at a time.
Line 10: This line takes out all the whitespace in the interface name. This should probably be done in the listInts() method, so check future versions of NTCI for it.
Line 11: Look at the interface name; if it contains the term "Ethernet", then we'll do some more stuff to it.
Line 12: Put the state in a hash called "state". See the docs on the getIntState() for more info on it.
Line 13: Put the speed in a variable called "speed".
Line 14: Put the duplex in a variable called "duplex".
Line 15: If the interface is acutally up, do some more stuff.
Line 16: If the speed is "10" or the duplex is "half", then do some more stuff.
Line 17: Get all the MACs on that interface and put them in an array called "mac". Hopefully, you'll only have one, but you may have more, so we'll have to use an array instead of a scalar.
Line 18: Print the summary line.
Line 23: Close the telnet connection.
© 2005-2006 -- Aaron Conaway