Downloads
16296.zip

In my server lab, I have a Cisco 5509 switch that handles the network server traffic. The switch has three 10/100Mbps Fast Ethernet switching modules. Each switching module has 24 ports, so I have 72 ports to properly configure and maintain. Occasionally, undocumented changes are made in the cables and devices attached to the ports. Because the cables often aren't properly marked, tracing the workstation, server, or other device attached to the ports is difficult. In the past, I manually queried each switch port during a Telnet session to determine each port's attached device and properties. To automate this tedious task, I wrote the script CiscoTelnet.pl.

CiscoTelnet.pl uses the Net::Telnet module, which lets you interact with Telnet and other ports. I used Net::Telnet to programmatically log on to the Ethernet switch and query all 72 ports for device and property information. Here's how the script works:

  1. The script connects to the switch. As the code
  2. $t->open("123.456.78.901") || die;

    shows, the script uses Net::Telnet's open method with the switch's IP address to connect to the Telnet session. To use CiscoTelnet.pl in your system, you need to replace 123.456.78.901 with your switch's IP address. The logical or (||) operator and the die function at the end of the line specify that if the preceding code (i.e., the open method) is unsuccessful, the script stops executing, or dies.

  3. The script uses Net::Telnet's waitfor method in the code
  4. $t->waitfor('/Enter password\:\s/i') || die;

    The waitfor method first waits for the password prompt (i.e., the message box that prompts users for their password), then programmatically presses Enter. To use this script, you need to replace Enter password with the text that appears in your password prompt.

  5. The script programmatically presses Enter to skip the logon and begin the query. The code
  6. $t->print("");

    is the equivalent of manually pressing Enter. If your system requires you to include a logon entry, you can place that logon entry between the set of double quotation marks ("").

  7. The script uses Net::Telnet's waitfor method to respond to a second logon prompt. In the code
  8. $t->waitfor('/MyCompany/i') || die;

    you need to replace MyCompany with the text that appears in your second logon prompt. If you don't have a second logon prompt, you can comment out this line by placing a number sign (#) in front of it.

  9. The script uses the sho port command to capture basic connection information for the port. This command is part of the command-line interface (CLI) for the Cisco 5509 switch. (For more information about CLI's commands, go to http://cio.cisco.com/univercd/cc/td/doc/ product/atm/l2020/2020r21x/netopx/comface.htm#xtocid256920.)


  10. If the port is connected, the script uses CLI's sho cam dyn command to capture the attached device's media access control (MAC) address.


  11. The script increments the port number by 1 and performs steps 5 and 6 again. This cycle repeats until the script captures the information for all 24 ports on the first switching module. If you have more or fewer ports on your switching modules, you can follow the instructions in step 8 to adapt the script.


  12. The script increments the switching module number by 1 and performs steps 5, 6, and 7 again. This cycle repeats until the script captures the information for all three switching modules. If you have a different number of switching modules or ports, you can adapt the script. Listing 1 shows the code you need to adapt and describes how to change it.


  13. The script outputs all the information to a text file for further examination. You need to enter the location of the output file. In the line
  14. $OPTFILE =
    "C:\\temp\\ciscotelnettest
    .csv";

    replace C:\\temp\\ciscotelnettest.csv with the path to your output file.

  15. The script uses CLI's quit command to close the Telnet session.
  16. You can find CiscoTelnet.pl in the Code Library on the Windows Scripting Solutions Web site (http://www.winscriptingsolutions.com). The script includes comments to help you understand the code. I wrote and tested CiscoTelnet.pl for use on machines running Windows NT 4.0 Service Pack 5 (SP5). To run the script, you need to install ActivePerl build 522 and the Net::Telnet module on the machine on which you'll execute the script. You can download ActivePerl and Net:: Telnet from ActiveState (http://www.activestate.com). Net::Telnet isn't part of the ActivePerl distribution but rather a separate download with ActivePerl's Perl Package Manager (PPM). Consult the ppm.html Help file in the \perl\html directory for information about how to install PPM and the telnet.html Help file that comes with the Net::Telnet module for configuration details.