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

I want to telnet to an Adtran box, run a cmd and get the output to file. I can log in and run the cmd, but the output doesn't write to file. The cmd is "debug isdn verbose", this will generate a constant stream of data, and I need to collect the data for several hours. How can I get the output to a file?

Replies are listed 'Best First'.
Re: Newbee needs help
by blindluke (Hermit) on Sep 25, 2014 at 21:37 UTC

    I can only assume that you are using Net::Telnet, based on the code snippet you provided, since your post does not say how you are trying to do what you want. If my assumption is correct, the problem is probably related to the way you defined the prompt. Anyway, start with this:

    my $log_fh = $t->input_log($filename); $t->cmd(String => "debug isdn verbose");

    Then examine the log, and see what it contains.

    If you would provide a more complete example of what you are doing (at least the fragments when you initialize Net::Telnet and set up the prompt), I would gladly help you with your problem.

    regards,
    Luke Jefferson

      Luke
      Thank you for the help, I was able to get the data to the file.
      Here is the code.

      if ($CL_logdebug){ while ($t){ for (my $A = 0;$A <= 10;$A++){ my $filename = sprintf ("AdtranLogInfo%02d.txt",$A); my $log_fh = $t->input_log("/home/jmahaffey/$filename"); $t->cmd(String => "debug isdn verbose",errmode => "return",Timeout => +3600); my @time = $t->cmd(String => "show clock",Prompt => '/[#]/',Timeout => + 10); printf @time; if ($A == 10){$A = 0}; } } }

      Thanks for the help.
      John

Re: Newbee needs help
by thanos1983 (Parson) on Sep 26, 2014 at 00:48 UTC

    Hello again Anonymous Monk,

    I noticed that the monks have helped you already and propose some alternative solutions to your problem.

    As blindluke guessed you are propably using Net::Telnet, but I would also like to propose an alternative solution that it will work if the router supports ssh.

    At this point I remembered that in the past I used Net::OpenSSH on a Sierra modem to execute a few commands (if it supports ssh).

    I created a sample of code that it does exactly what you need and pipes the output to your local OS. I am using a command for Linux, so you have to change the command and test it. I think it will work, I am not 100% sure because you said it is VoIP router but worth the effort of trying.

    Sample of running code with output printed from the file.

    #!/usr/bin/perl use strict; use warnings; use Net::OpenSSH; my $host = "127.0.0.1"; # remote IP my $port = 22; # ssh default 22 change it my $passwd = "username"; my $user = "password"; my %opts = ( passwd => $passwd, port => $port, user => $user ); my $ssh = Net::OpenSSH->new( $host , %opts ); $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error; my ($rout, $pid) = $ssh->pipe_out("vmstat") or die "pipe_out method failed: " . $ssh->error; open my $write, ">", "test.txt" or die $!; while (<$rout>) { print $write $_; } close $rout; close $write; open my $read, "<", "test.txt" or die $!; while (my $row = <$read>) { chomp $row; print "$row\n"; } close $read; __END__ Check also the test.txt file it contains the same output procs -----------memory---------- ---swap-- -----io---- -system-- ---- +--cpu----- r b swpd free buff cache si so bi bo in cs us s +y id wa st 1 0 68716 390444 9520 674036 0 1 25 27 217 235 8 +2 89 1 0

    I hope this helps, let me know if it worked.

    Ps: please change the title of your question to something more appropriate, read I want to ask a question of the Perl Monks; where do I start?, How do I post a question effectively? and importantly Select an informative title.. It will only take you a few minutes.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Newbee needs help
by thewalledcity (Friar) on Sep 25, 2014 at 19:58 UTC
    I would probably just avoid perl for this and run
    debug isdn verbose &> output.log
    Then do whatever you need to the file after it is done running.

      The Adtran doesn't support that command. It is a VoIP router.
      Thanks
      John

        But the router runs perl? I think we need more explicit information about where you are running the perl, where you expect the output, what do you see when running the code in the original question.
Re: Newbee needs help
by thanos1983 (Parson) on Sep 25, 2014 at 16:37 UTC

    Hello Anonymous Monk,

    I never deal with your problem, but why not complete the process that you want and then why not just copy the file? You can use the Net::FTP module for copying the file.

    This could be a possible solution to your problem.

    Update: If I understand correctly your problem you could execute your command, and pipe it to a file where you can copy after. Take a look at System command using array and pipe and also executing external commands.

    Hope this helps, let me know if it worked.

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

      Thanks for the information. Let me try to explain better.
      When you enter the cmd "debug isdn verbose", the Adtran starts returning data.
      WilliamsFudge904#debug isdn verbose
      WilliamsFudge904#
      11:46:18:576 ISDN.RES_MGR Reserving an outbound channel.
      11:46:18:577 ISDN.RES_MGR ISDN Channel reserved.
      11:46:18 ISDN.CC_MSG PRI 1 Host>>CC: 00 c633 SETUP_REQ
      11:46:18 ISDN.CC_IE PRI 1 ie: 00 04 0A 80 80 80 90 00 00 00 00 00 82
      11:46:18 ISDN.CC_IE PRI 1 ie: 00 6C 0E 80 80 80 80 33 33 34 37 30 33 36 33 35 37
      11:46:18 ISDN.CC_IE PRI 1 ie: 00 70 0C 80 80 38 30 33 33 36 36 31 36 36 33
      11:46:18 ISDN.CC_IE PRI 1 Qie: 1C 0E 9F 8B 01 00 A1 08 02 01 01 02 01 00 84 00
      11:46:18 ISDN.L2_MSG PRI 1 Sent = 02 01 D6 4E 08 02 46 34 05 04 03 80 90 A2 18 03
      11:46:18 ISDN.L2_MSG PRI 1 A1 83 85 1C 0E 9F 8B 01 00 A1 08 02 01 01 02 01
      11:46:18 ISDN.L2_MSG PRI 1 00 84 00 6C 0C 00 80 33 33 34 37 30 33 36 33 35
      11:46:18 ISDN.L2_MSG PRI 1 37 70 0B 80 38 30 33 33 36 36 31 36 36 33

      I need to send this data to a file.
      Here is the part of the code where I try to gather the information and send it to the file.

      my @output = $t->cmd(String => "debug isdn verbose"); foreach my $line (@output){ printf ADTRANLOGINFO "%s \n",$line; }

        Please show more code - what is $t? Also we need a more detailed problem description than "but the output doesn't write to file" - is the file created or not, does it have zero size or not, and are there any warnings or error messages? See How do I post a question effectively?