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

Hi Monks,

I have been working on this for 2 days trying to capture data from a Win2k machine using Net::Telnet. However, I am only getting '1' or empty in the @df array (I printed the contents of the array @df to a text file). I googled many sites with different keywords and could not really find anything that resembles my problem.

Also, the input_log() gave me a bunch 'empty square boxes' separating each line, and they look like they are supposed to have carriage returns (I copied the input_log contents and pasted it in MS Word and saw the empty boxes).

I'm using VT100 emulation in my telnet client(Win XP). I have disabled NTLM authentication in the telnet server(Win2k Advanced Server). The script connects successfully and executes cmd successfully (I did a mkdir and the directory got created, checked dump_log and see the commands executed and also checked input_log). However, I'm not getting any outputs by using @df=$telnet->cmd... I'm trying to capture the output into an array to be used later.

Please help.

Thanks in advance!
auyong

Below is part of the code.
my $write_file=">"; my $initial_data = 'file.txt'; my $dumpfile='storage_dumpfile.txt'; my $inputfile='inputfile.txt'; my $storage_file=$write_file.$directory.'storage_final_file.txt'; open(STORAGE_FILEHANDLE, $storage_file)||die ("Cannot open file please + check if file exists"); $storage_telnet = Net::Telnet->new ( Timeout=>10, Prompt=>'/>/', Dump_Log=>$dumpfile, Input_log=>$inputfile ) or die("Cannot establish connection"); $storage_telnet->login($_[2], $_[3])||die("Cannot login, pleas +e check password\n", dump_log=>$dumpfile); $storage_telnet->cmd ("cd\\Program Files\\Hi\\DManager\\HiCLI\\")|| di +e (dump_log=>$dumpfile); @df=$storage_telnet->cmd(String=>"dir", Prompt=>'/C:\\\\Program Files\ +\\\Hi\\\\DManager\\\\HiCLI*/i')||die(dump_log=>$dumpfile); ##mkdir works, the new directory whatthehell was created ##this was just used to test whether I am in the correct ##directory #@df=$storage_telnet->cmd(String=>"mkdir whatthehell", Prompt=>'/C:\\\ +\Program Files\\\\Hi\\\\DManager\\\\HiCLI*/i')||die(dump_log=>$dumpfi +le); #here $write_file = ">" open (INITIAL_RAW_DATA_FILEHANDLE, $write_file.$initial_data); foreach $element(@df) { print INITIAL_RAW_DATA_FILEHANDLE "$element\n"; } close(INITIAL_RAW_DATA_FILEHANDLE);
Animator, thanks for your input and advise. The reason I had the $write_file is because I have additional logic to decide whether I want to write or append but I guess I could have incorporated the three-param version into my logic instead of having the scalar $write_file.

Latest UPDATE: I have tried waitfor yesterday but still unable to grab the data. I'm not sure if readline can be used with telnet, because it belongs to another package.

Additionally, I have found that the Windows telnet server that I am connecting to does not support streaming data, it is using 'console' mode only. Could this be why I am having problems reading the output? The 5.1 version of windows telnet server supports streaming but I have to install SFU (Support for Unix) on the Windows 2000 Advanced Server machine.

If I can't find a solution to this telnet problem, I will probably have to save the output to a file and ftp to get the file, which presents another dilemma; I have to install IIS with ftp feature on the Windows 2000 Advanced Server machine! So, I'm really hoping that one of you monks out there who knows the telnet module can at least pinpoint the cause of problem that I'm having.

auyong

Replies are listed 'Best First'.
Re: Capturing Telnet Command Outputs on Windows 2000 machines
by Animator (Hermit) on Feb 17, 2005 at 19:09 UTC

    Perhaps the methods waitfor and readline can help you?

    Note, I don't have any experience with Net::Telnet, I only saw that those two methodes where used in the examples in the Net::Telnet-doc.

    Update: you should also be using 'or' instead of '||' for error checking. (It doesn't make any difference when you use 'die', but it is IMHO bad practice.)

    Update2: your open-statement is not that good either. There is a three-param version of open, which would allows you to do: open(FH, ">", "filename"); which is what you try to accomplish by appending $write_file to the file name (which is less safe).

Re: Capturing Telnet Command Outputs on Windows 2000 machines
by Mr. Muskrat (Canon) on Feb 19, 2005 at 14:51 UTC

    If you read the docs for Net::Telnet again, you'll see that it says that "If the command happens to return no output, a list containing one element, the empty string is returned." I'm not 100% sure that Windows 2000 Advanced Server behaves the same way (as every other version of Windows that I have ever run) but in my experience mkdir only displays text where there was a problem. If the mkdir command was successful, @df would would contain one empty string.

    By the way, I've never tried to use dump_log from within a die. The docs for Net::Telnet say that "This method starts or stops dump format logging of all the object's input and output." This doesn't sound like something you would want to do in a die. I've also never used dump_log the way that you are attempting to (dump_log=>$dumpfile). (Without testing) I would say that your dies will output to STDERR something very similar to "dump_logstorage_dumpfile.txt at C:\path\to\file.pl line XXX." It would make more sense to turn on dump_log right after the connection is made and to die with the last error reported (die($storage_telnet->errmsg)).

Re: Capturing Telnet Command Outputs on Windows 2000 machines
by castaway (Parson) on Feb 19, 2005 at 11:04 UTC
    Can you show us what your dump log contains ?

    C.