JoeJaz has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I am using Net::Telnet to read in input from different commands. What is happening, for example, is that I want to be able to telnet into an account, 'cat' a file, and use that file in my perl script. To be more specific, I want to read in data from the ~/.vacation.msg file and then (eventually) display it to the screen. So far, I have gotten this to work for the most part. However, when the .vacation.msg file contains the text $SUBJECT, the perl script fails to store whatever line in which that variable occurs. The below working excerpt, taken from my full program, I believe demonstrates the problem. Forgive me for not including 'use strict' and for other likely sloppy syntax; this program is only for testing purposes:
#!/usr/bin/perl my $username="user1"; my $password="password"; my $host="localhost"; my $CAT = "/usr/bin/cat"; my $LAST = "/usr/bin/last"; &start_login; print "@vacation_str\n"; sub start_login { my $telnet = new Net::Telnet; telnet_login($username,$password,$host,\$telnet); @vacation_str=$telnet->cmd("$CAT /home/$username/.vacation.msg"); telnet_close(\$telnet); } sub telnet_login { use Net::Telnet; my ($username, $password, $host, $telnet) = @_; my $error_msg = "Incorrect username or password, please try again" +; my $error_type = "ERROR"; $$telnet = new Net::Telnet ('Timeout'=>'7', 'Errmode'=> sub { &report_error($error +_type,$error_msg); }, 'Prompt'=> '/.*([\$#\%>~]|\\\[\\e\[0m\ +\\] \[0m)\s?/' ); $$telnet->open(Host=>$host); $$telnet->login($username,$password); } sub telnet_close { my $telnet=shift; $$telnet->close; }
I believe that perl thinks that $SUBJECT is a variable and is trying to store it into @vacation_str, but failing to do so. I am looking for a way to escape the $ before it is stored into @vacation_str so it won't see $SUBJECT as a variable. I also had my eye on the Net::Telnet functions 'get' and 'getlines' to try take the input a different way. However, I don't know if this is a dead end. I am greatful for any advice that anyone has on this issue. Thanks for reading this. Joe

Replies are listed 'Best First'.
Re: Can't get $ from Net::Telnet
by phydeauxarff (Priest) on Jun 25, 2003 at 22:54 UTC
    I was attempting something similar to read back configuration files from Netscreen firewalls a few years ago and I seem to recall as outlined in the docs....@vacation_str=$telnet->cmd; will output your command and then read the characters sent back up to and including the matching prompt. Which is not what I think you are trying to accomplish.

    You can probably verify what is going on by using dump_log().

    I would also check out attempting something like

    $telnet->cmd("$CAT /home/$username/.vacation.msg"); @vacation_str=$telnet->getlines();
    which should read back all the lines of data until eof is read.
      Thank you for your advice. I will look into recieving input in that fashion. This is one of my last few stumbling blocks that I have to make it through to finish my project :-)
      Thanks, Joe
Re: Can't get $ from Net::Telnet
by Zaxo (Archbishop) on Jun 26, 2003 at 12:36 UTC

    Lose the quotes in line 11, print @vacation_string, $/; They are making perl try to interpolate $SUBJECT as a variable, as well as inserting $" (default, a space) between each array element.

    After Compline,
    Zaxo

      Thanks for the info regarding perl's behavior. I have made the appropriate changes to my code.
      Joe