How to Create a Simple Telnet Script in Perl

Write a Perl (Practical Extraction and Report Language) script to establish a telnet connection to a remote server when you have repetitive command-line administration tasks (such as parsing log files) that must be performed regularly. You can leverage Perl modules located in the internationally mirrored CPAN (Comprehensive Perl Archive Network) site rather than writing all required methods from scratch. Use the Net::Telnet module to create a short script and establish a telnet connection.

Things You'll Need

  • Windows computer with Perl programming language software (such as ActivePerl) installed
  • Net:Telnet Perl module installed
Show More

Instructions

    • 1

      Click the “Start” button on the Windows taskbar and click Search. Type “cmd” into the Search box. Press the “Enter” key. Enter “notepad” without quotes on the command line and tap the “Enter” key.

    • 2

      Type the following into the Notepad text editor program that appears:

      #!/usr/bin/perl

      use Net::Telnet;
      $tlnt = new Net::Telnet ( Timeout=>10, Errmode=>'die');
      $tlnt->open('telnet_server_IP');
      $tlnt->waitfor('/login: $/i');
      $tlnt->print('user_name');
      $tlnt->waitfor('/password: $/i');
      $tlnt->print('account_password');
      $tlnt->waitfor('/\$ $/i');
      $tlnt->print('who');
      $output = $tlnt->waitfor('/\$ $/i');
      print $output;

      Replace user_name with the name of an account that has telnet access to the telnet server, replace account_password with the password for the account that has telnet access, and replace telnet_server_IP with the IP address of the telnet server.

      Note that the Net::Telnet module is called at the beginning of the telnet script and the methods used in the script are included in the module.

    • 3

      Click the File menu and select Save As in the menu that appears. Type “telnettest.pl” (including the quotes) in the File Name box. Click the Save as Type box and select All Files. Click the “Save” button.

    • 4

      Type “telnettest.pl” at the command line and press the “Enter” key. A message will appear confirming that the Perl script has established a telnet connection with the telnet server.

Related Searches:

References

Resources

Comments

Related Ads

Featured