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

Hello Perlmonks. First time poster, first time script writer for Perl. I've got a sub that I've written here that I'm having some trouble getting output from a telnet connection.
sub telnet_connection{ my $targetHost = $_[0]; #my $uname = $_[1]; #my $pword = $_[2]; my @listOfCommands = @{$_[3]}; if($verbose){print "\t\tAttempting telnet connection to $targe +tHost\n"; } eval{ $telnet = Net::Telnet->new( Timeout=>5, Errmode=>'retu +rn', dump_log=>"dump_log.txt", input_log=>"input_log.txt"); $telnet->open($targetHost); $telnet->waitfor('/Username: $/i'); $telnet->print($username); $telnet->waitfor('/PASSCODE: $/i'); $telnet->print($password); $telnet->waitfor('/\>/'); #for(my $j=0; $j le scalar @listOfCommands; $j++) foreach my $command (@listOfCommands) { if($verbose) { print "\t\tClient: " . $targetHost +. " Command: $command\n"; } if($verbose) { print "\t\tExecuting command: $comm +and\n"; } my @output = $telnet->cmd($command); if($verbose) { print "OUTPUT:\n"; print @output; } logging($targetHost, \@output, "0"); } $telnet->close(); }; if($telnet->errmsg) { logging($targetHost,$telnet->errmsg,"1"); print $@; } }
This is a method I wrote that takes four arguments, the target of the telnet connection, username, password, and an array of commands. For right now, I'm just using the global usernames and passwords ($username $password respectively) because there was an issue with something else. $verbose is a flag that I pass, and in this case it's set to true. logging is another method I wrote to handle output accordingly, but with $verbose being true, I should see it on the console. The code is able to authenticate, and run the commands, but my output isn't present. When I check the dump_log and input_log, the raw (and formatted) text is present, and shows that I'm actually executing the commands and seeing the output, yet, when I try to check the output, it comes back with a "":
Telnet to <targetIPAddress> Attempting telnet connection to <targetIPAddress> Client: <targetIPAddress> Command: who Executing command: who OUTPUT:
And I'm just not sure where to troubleshoot from here. I've looked through several of the stack overflow and perlmonks posts already on Telnet in Perl, but not much help was found there. When I use warnings or use strict, there aren't any issues in this sub.

Replies are listed 'Best First'.
Re: Perl, Telnet, and No Output
by aitap (Curate) on Aug 13, 2014 at 18:57 UTC
    I don't see where you set the prompt attribute of Net::Telnet object. As the documentation says,
    The methods login() and cmd() use the prompt setting in the object to determine when a login or remote command is complete. Those methods will fail with a time-out if you don't set the prompt correctly.
    Net::Telnet just doesn't know where the command output starts and where it ends, so it can't return you anything. You can use $telnet->errmsg to get its error message:
    When mode is "return" then the method generating the error places an error message in the object and returns an undefined value in a scalar context and an empty list in list context. The error message may be obtained using errmsg().
    $telnet->errmsg probably gets cleared after you call ->disconnect, so you see no error message.
      This was it! Thank you! Yeah, once I set the prompt in the telnet connection settings, it's able to handle it accordingly. Now I just have to get the values of the array to pass to my logging sub so it doesn't separate out each line.
Re: Perl, Telnet, and No Output
by fishmonger (Chaplain) on Aug 13, 2014 at 16:23 UTC

    Do you get the expected output to the console if you do not enable input_log?

    I have not used that module for a long time, but the following statement taken from the module's documentation about that setting suggests that it's capturing/redirecting the output.

    Because most command interpreters echo back commands received, it's likely all your output will also be in this log.
      The output is in the debug log, which is why I'm having a hard time troubleshooting this. I tried to follow the module's standard setup, which is why the response is stored in an array. If the output weren't in input_log, I imagine a syntax error, transmission, or something. But everything is in the input_log. I just don't get a response back into the @output array.
      Sorry, I mis-interpreted what you said. No, even if I don't specify the input_log, it still doesn't appear.