Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Telnet Client or CRT Help?

Expand Messages
  • Gerrit P. Haase
    Message 1 of 3 , Nov 12, 2000
    • 0 Attachment
      <7 Nov 2000, 18:57 Uhr wars, als EleqTrizi'T folgendes schrub:>
      < [PBML] Telnet Client or CRT Help? >

      SEE ALSO
      RFC 854
      TELNET Protocol Specification


      RFC 1143
      Q Method of Implementing TELNET Option Negotiation


      TELNET Option Assignments

      This example gets the current weather forecast for Brainerd, Minnesota.

      my ($forecast, $t);

      use Net::Telnet ();
      $t = new Net::Telnet;

      ## Wait for first prompt and "hit return".

      ## Wait for second prompt and respond with city code.
      $t->waitfor('/city code.*$/');

      ## Read and print the first page of forecast.
      ($forecast) = $t->waitfor('/[ \t]+press return to continue/i');
      print $forecast;


      This example checks a POP server to see if you have mail.

      my ($hostname, $line, $passwd, $pop, $username);

      $hostname = "your_destination_host_here";
      $username = "your_username_here";
      $passwd = "your_password_here";

      use Net::Telnet ();
      $pop = new Net::Telnet (Telnetmode => 0);
      $pop->open(Host => $hostname,
      Port => 110);

      ## Read connection message.
      $line = $pop->getline;
      die $line unless $line =~ /^\+OK/;

      ## Send user name.
      $pop->print("user $username");
      $line = $pop->getline;
      die $line unless $line =~ /^\+OK/;

      ## Send password.
      $pop->print("pass $passwd");
      $line = $pop->getline;
      die $line unless $line =~ /^\+OK/;

      ## Request status of messages.
      $line = $pop->getline;
      print $line;


      Here's an example you can use to down load a file of any type. The file
      is read from the remote host's standard output using cat. To prevent any
      output processing, the remote host's standard output is put in raw mode
      using the Bourne shell. The Bourne shell is used because some shells,
      notably tcsh, prevent changing tty modes. Upon completion, FTP style
      statistics are printed to stderr.

      my ($block, $filename, $host, $hostname, $k_per_sec, $line,
      $num_read, $passwd, $prevblock, $prompt, $size, $size_bsd,
      $size_sysv, $start_time, $total_time, $username);

      $hostname = "your_destination_host_here";
      $username = "your_username_here";
      $passwd = "your_password_here";
      $filename = "your_download_file_here";

      ## Connect and login.
      use Net::Telnet ();
      $host = new Net::Telnet (Timeout => 30,
      Prompt => '/[%#>] $/');
      $host->login($username, $passwd);

      ## Make sure prompt won't match anything in send data.
      $prompt = '_funkyPrompt_';
      $host->cmd("set prompt = '$prompt'");

      ## Get size of file.
      ($line) = $host->cmd("/bin/ls -l $filename");
      ($size_bsd, $size_sysv) = (split ' ', $line)[3,4];
      if ($size_sysv =~ /^\d+$/) {
      $size = $size_sysv;
      elsif ($size_bsd =~ /^\d+$/) {
      $size = $size_bsd;
      else {
      die "$filename: no such file on $hostname";

      ## Start sending the file.
      binmode STDOUT;
      $host->print("/bin/sh -c 'stty raw; cat $filename'");
      $host->getline; # discard echoed back line

      ## Read file a block at a time.
      $num_read = 0;
      $prevblock = '';
      $start_time = time;
      while (($block = $host->get) and ($block !~ /$prompt$/o)) {
      if (length $block >= length $prompt) {
      print $prevblock;
      $num_read += length $prevblock;
      $prevblock = $block;
      else {
      $prevblock .= $block;


      ## Print last block without trailing prompt.
      $prevblock .= $block;
      $prevblock =~ s/$prompt$//;
      print $prevblock;
      $num_read += length $prevblock;
      die "error: expected size $size, received size $num_read\n"
      unless $num_read == $size;

      ## Print totals.
      $total_time = (time - $start_time) || 1;
      $k_per_sec = ($size / 1024) / $total_time;
      $k_per_sec = sprintf "%3.1f", $k_per_sec;
      warn("$num_read bytes received in $total_time seconds ",
      "($k_per_sec Kbytes/s)\n");


      Jay Rogers <jay@...>

      Copyright 1997, 2000 by Jay Rogers. All rights reserved. This program is
      free software; you can redistribute it and/or modify it under the same
      terms as Perl itself.

      - gph -

      Gerrit Peter Haase
    Your message has been successfully submitted and would be delivered to recipients shortly.