Real-World Scripting: Querying Telnet Ports for Device Information

Instead of manually querying switch ports during a Telnet session, you can use a script to automate this tedious task. The script programmatically logs on to an Ethernet switch and queries all ports for device and property information.

Dick Lewis

January 2, 2001

4 Min Read
ITPro Today logo in a gray background | ITPro Today


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

    $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.

  2. The script uses Net::Telnet's waitfor method in the code

    $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.

  3. The script programmatically presses Enter to skip the logon and begin the query. The code

    $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 ("").

  4. The script uses Net::Telnet's waitfor method to respond to a second logon prompt. In the code

    $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.

  5. 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.)

  6. 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.

  7. 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.

  8. 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.

  9. 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

    $OPTFILE ="C:\temp\ciscotelnettest.csv";

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

  10. The script uses CLI's quit command to close the Telnet session.

    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 perlhtml directory for information about how to install PPM and the telnet.html Help file that comes with the Net::Telnet module for configuration details.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like