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

I'm seeking knowledge to guide me through this dense mist...

I discovered the telnet server on which I connect (Windows Server (probably 2013)), any command sent must have at maximum 1024 characters, anything more than this, the server asks me for "More? ", and then I should input the remaining characters and press enter. So far, so good.

I have a program written in Perl, that executes a series of commands in this server, but recently some commands have been stuck until it times out, and I discovered that are the commands with more than 1024 characters.

So, I decided to rewrite the execution function to support this behavior, but any one of methods tried didn't worked.

This is how I normally execute my commands before:

my $t = new Net::Telnet(Telnetmode => 1, Errmode => 'return'); $t->prompt('/C:\\\\(.*)>/i'); $t->open(Host => '192.168.0.10'); $ok = $t->login('myUsername', 'myPassword'); my $cmd = "blah blah blah (...)"; # 1287 characters of a VALID comm +and my @r = $t->cmd(String => $cmd, Timeout => 60);

This will result in timeout. If the command have at maximum of 1024 characters, it passes normally.

Then I decided use this way, instead of  $t->cmd()

my @r = $t->print(String => $cmd, Timeout => 60); ($prematch, $match) = $t->waitfor(String => 'More? ', Timeout=>60) +or die("MORE NOT FOUND");

And the waitfor times out... I'm pretty lost here.

So my questions are:

1) Am I missing anything about telnet and the limitation of 1024 input characters? So maybe this could be solved by using a different configuration? I have googled a lot about this subject and found't anything useful...

2) Is the commands $t->print() and $t->waitfor() right?

Where do we go from here?

Thanks for your help monks!

Replies are listed 'Best First'.
Re: Telnet command with more than 1024 chars (updated)
by thanos1983 (Parson) on Jul 19, 2017 at 13:59 UTC

    Hello ElectricCheese,

    Welcome to the monastery. Well I want to ask one question, why telnet and not any other alternative? See Best module to execute administrator commands on external operating systems (Linux) for possible alternatives with samples of code. I know the title says Linux but the modules and samples of code should apply on WindowsOS also.

    Update: Check max_buffer_length - maximum size of input buffer. From the module documentation:

    $len = $obj->max_buffer_length; $prev = $obj->max_buffer_length($len);

    Update2: Or you can use cmd - issue command and retrieve output use die for the command and eval to capture error:

    $ok = $obj->cmd($string); $ok = $obj->cmd(String => $string, [Output => $ref,] [Cmd_remove_mode => $mode,] [Errmode => $mode,] [Input_record_separator => $chars,] [Ors => $chars,] [Output_record_separator => $chars,] [Prompt => $match,] [Rs => $chars,] [Timeout => $secs,]);

    Sample from Trying to capture error message using telnet::net, sample of code untested.

    eval { my @lines = $obj->( String => 'who', # your long command here Errmode => 'die' ); }; if($@) { print $@; } else { print "success\n"; }

    Maybe this could help.

    Looking forward to your reply, if you only want telnet and nothing else, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      Thank you for you quickly response thanos!

      I have updates... first of all, my fellow monk, apologizes this young grasshopper. My command line have a lot of e-mails, and I forgot to escape them in my test script. So part of the problem is solved: the "More? " is coming in the result, and the waitfor is working. NICE.

      However, in the ideal world, I shouldn't be concerned about a command line with 1024 chars limitation, this is awkward. The "More? " shouldn't NEVER be asked... So I'm opened to ideas that fix this. So I checked the max_buffer_length and returned its default value: 1048576 bytes, 1 MiB. Well, I'm not sure, but I suspect this behavior should be on server side.

      Regarding about why telnet, your're absolutely right. AFAIK, the best option should be SSH. But this server is maintained by another team, I'll ask them if it is possible to change to SSH. Thanks for minding about this.

      I'm still working on print and waitfor... updates will come.

        Hello again ElectricCheese,

        Why don't you try to get the byte size of the command (145 characters) with Devel::Size or bytes.

        By doing this you will now if you are exceeding the maxim transmission size (1,048,576 bytes) see the documentation from the module Net::Telnet for max_buffer_length - maximum size of input buffer.

        I am not only suggesting to use ssh because you will avoid all these problems, I am suggesting it because of many many reasons. Read an short article about the comparison Telnet & SSH.

        Hope this helps, BR.

        Seeking for Perl wisdom...on the process of learning...not there...yet!
        While I'd second going to a different protocol than telnet, I know SSH has to be installed from external sources, e.g. Cygwin or KpyM. Perhaps on newish systems with their "Linux subsystem" it might be included.

        However, your problem sounds similiar to one that occurs when accessing routers, which typically default to paging after 24 lines and/or inserting line breaks after 80 characters. So my suggestion is: telnet in manually, issue the "escape character" (usually something like ctrl+] ), and enter "display". According to this, that should show you the parameters that are in effect. Perhaps you can see/set line width and/or page length there. In that case, Net::Telnet's option_state, option_send, etc. could be used.

Re: Telnet command with more than 1024 chars
by karlgoethebier (Abbot) on Jul 19, 2017 at 16:42 UTC
    "...telnet ...Windows Server...a series of commands..."

    Perhaps using Powershell remote commands (Cmdlet Invoke-Command?) might be a better option?

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help