Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Net:SSH::Expect and microcom?

by Anonymous Monk
on Nov 07, 2011 at 21:20 UTC ( [id://936581]=perlquestion: print w/replies, xml ) Need Help??

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

O wise monks,

I have run into an issue at work where I need to automate the following:

  • SSH to an element
  • Run "Microcom /tmp/dev/mux1"
  • send "AT+CSQ" to that program
  • Retrieve the output in a variable in my script
  • I've accomplished the first (SSH to element and run command) but when it comes to actually sending anything to the interface and retrieve the output, I just cant get it to work.

    Relevant part of the code:

    $ssh->exec("microcom /tmp/dev/mux1"); $ssh->send("AT+CSQ"); $ssh->waitfor('^\+CSQ', 3) or warn "No output!"; $myOutput = $ssh->match(); print "CSQ result: $myOutput";

    I've tried with ssh->exec("AT+CSQ") as well with no success. It simply warns on "No output!"

    Anyone done something similar before and can provide me with a hint or two on how to overcome this obstacle?

    If only I could pass the AT command directly on the commandline, but alas, thats not possible. :-(

    Thanks in advance!

    Replies are listed 'Best First'.
    Re: Net:SSH::Expect and microcom?
    by salva (Canon) on Nov 08, 2011 at 08:37 UTC
      Using Net::OpenSSH and Expect:
      # untested use Net::OpenSSH; my $ssh = Net::OpenSSH->new(...); my ($pty, $pid) = $ssh->open2pty('microcom /tmp/dev/mux1'); my $exp = Expect->init($pty); $exp->send("AT+CSQ\r\n"); $exp->expect($timeout, [qr/^OK$/ => sub { ... }], [qr/^ERROR$/ => sub { ... }]); $exp->soft_close(); waitpid($pid, 0);

        Thanks, but it doesnt work. I am experiencing some very odd behaviour with the suggested snipped.

        When run manually, the AT command returns to OK within 500ms. Since the RegEx would match and it still times out, I believe it safe to conclude that it still doesnt pass in the AT command.

        More important is the fact that no matter what timeout I set, it throws the warning after ~2 seconds. I've set 50 seconds in the example below:

        print "Before timeout: " . localtime() . "\n"; my $timeout = 50; $exp->expect($timeout, [qr/^OK$/ => sub { print "Success!\n"; }], [qr/^ERROR$/ => sub { print "Failure!\n"; }]) or warn "Timeout!"; print "After timeout: " . localtime() . "\n";
        and the output I get from the above:
        Before timeout: Tue Nov 8 03:31:02 2011 Timeout! at getdbm.pl line 18. After timeout: Tue Nov 8 03:31:04 2011

        Am I experiencing a bug here, or am I (still) an idiot?

          Maybe you have to wait for microcom to settle before sending anything. Try sleeping for 1 second before sending the modem command.
    Re: Net:SSH::Expect and microcom?
    by williams554 (Sexton) on Nov 07, 2011 at 22:33 UTC

      Hello,

      You need to tell it newline or return.

      $ssh->send("AT+CSQ\n"); or
      $ssh->send("AT+CSQ\r");
      Also, you can log what's going on
      string log_file
      Used as argument to the internal Expect->log_file() method.
      The default is no logfile.

      Good luck, Rob

    Re: Net:SSH::Expect and microcom?
    by Anonymous Monk on Nov 07, 2011 at 22:15 UTC

      I've gotten a bit closer, but not quite there yet

      #!/usr/bin/perl use strict; use Net::SSH2; my $ssh = Net::SSH2->new(); $ssh->connect('10.10.10.10') or die; if ($ssh->auth_password('zzz','xxx')) { $ssh->debug(1); my $chan = $ssh->channel(); $chan->shell(); print $chan "microcom /tmp/dev/mux1\n"; sleep(10); print $chan "AT+CSQ"; select(undef,undef,undef,3.0); my ($len, $buf); print $buf while ($len = $chan->read($buf,1)) > 0; $chan->close; } else { warn "auth failed.\n"; }

      This one clearly shows me that the AT+CSQ is not passed on - when connecting to the modem, it will report "ERROR" after ~20 seconds of nothing going on. And it does just that. Any ideas on how to move on from here?

    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Node Status?
    node history
    Node Type: perlquestion [id://936581]
    Front-paged by Arunbear
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others learning in the Monastery: (6)
    As of 2024-04-20 02:30 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found