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

Hi, I am using NET::TELNET module in perl to connect to linux and windows servers. I can accomplish this for linux. But having problems with windows servers. First, the windows servers have Microsoft Telnet server, with telnet service enabled. I am having a problem with the syntax in the perl program. Let me show you what I am doing...
#!/usr/bin/perl use Net::Telnet(); $telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die', Prompt => '/[>]/'); $telnet->open('servername'); $telnet->login('username', 'password'); print $telnet->cmd( 'dir' );
The line giving problems in the above code is : print $telnet->cmd( `dir` );
When I run this program I get an error : bad named parameter " 21 File(s) 22,117 bytes " given to Net::Telnet::cmd() at C:\Status Script\servername.pl line 9
If I modify the line like this i.e. using backticks: print $telnet->print( `dir` ); I get the output as number "1".
I have tried lots of combinations like using double quotes, single quotes or back ticks around "dir" but I always get the output as number "1".
Please let me know, how can I use windows commands in the above script.
Thank You.

Replies are listed 'Best First'.
Re: NET::TELNET to access Windows Servers
by ikegami (Patriarch) on Nov 29, 2004 at 06:01 UTC
    Using backticks is definitely wrong here. Backticks causes dir to run on your own machine, and the output of that dir is used as the command. 'dir' and "dir" are identical. Maybe it has to be "dir\n"? In any case, you should be looking beyond which quotes you are using.
Re: NET::TELNET to access Windows Servers
by rupesh (Hermit) on Nov 29, 2004 at 06:19 UTC
    Soul,
    Check whether you have established the connection with the remote server by checking the return value of each the  $telnet -> ... commands.
    Also, in your code, you have used single quotes as in  print $telnet->cmd( 'dir' );, but in your problem statement, you mention backticks "`".

    You might also want to have a look at this node.
    Perform a Super Search, where you might get more useful hints.

    Cheers,
    Rupesh.
Re: NET::TELNET to access Windows Servers
by Juerd (Abbot) on Nov 29, 2004 at 12:33 UTC

    It's Net::Telnet, not NET::TELNET or net::telnet. Module names are case sensitive. Realising that helps avoiding unexpected behaviour on a case-insensitive filesystem (as with "use Strict;").

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re: NET::TELNET to access Windows Servers
by Heretyk (Scribe) on Nov 30, 2004 at 02:02 UTC

    soul,

    The responses above have addressed the problem you're currently asking about, but there's something else that you should know about using Net::Telnet to communicate with Windows Telnet servers, something that appears in the documentation of the Net::Telnet module, and that should definitely be taken on board, as it can lead to a whole world of hurt.

    Connecting to a Remote MS-Windows Machine

    By default MS-Windows doesn't come with a TELNET server. However third party TELNET servers are available. Unfortunately many of these servers falsely claim to be a TELNET server. This is especially true of the so-called "Microsoft Telnet Server" that comes installed with some newer versions MS-Windows.

    When a TELNET server first accepts a connection, it must use the ASCII control characters carriage-return and line-feed to start a new line (see RFC854). A server like the "Microsoft Telnet Server" that doesn't do this, isn't a TELNET server. These servers send ANSI terminal escape sequences to position to a column on a subsequent line and to even position while writing characters that are adjacent to each other. Worse, when sending output these servers resend previously sent command output in a misguided attempt to display an entire terminal screen.

    Connecting Net::Telnet to one of these false TELNET servers makes your job of parsing command output very difficult. It's better to replace a false TELNET server with a real TELNET server. The better TELNET servers for MS-Windows allow you to avoid the ANSI escapes by turning off something some of them call console mode.

    This doesn't necessarily mean that you won't be able to achieve the results that you want, but it is definitely something of which to be aware, as the responses that you get from the telnet server might seem very odd indeed...

Re: NET::TELNET to access Windows Servers
by soul (Initiate) on Dec 01, 2004 at 07:04 UTC
    Thanks for all the information. First thing, I am able to connect to the server using the script, since I do not get any error message when I run it. I tried using "dir\n", but I still got the output as "1". The thing that I forgot to mention in my earlier post is that when I use "cmd" with double or single quotes around "dir", the output is blank, but when i use backticks with "cmd", I get the error message "bad named parameter....". Thank you again.