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

Monks, in the process of creating some automation I have ran into a roadblock. I need to send the device the key sequence <ctrl>_ but nothing I have tried has worked so far. My efforts include:

\032\137
\c_
\x1F
\x032\x5F

Any help would be greatly appreciated!!
  • Comment on Control:CLI or Net::SSH2 - Send <ctrl>_

Replies are listed 'Best First'.
Re: Control:CLI or Net::SSH2 - Send <ctrl>_
by huck (Prior) on Feb 20, 2017 at 18:18 UTC

    NOTE: While it doesnt show here there is a character between the two quotes in my $a='';

    use strict; use warnings; my $a=''; # entered by holding down cntrl pressing _ (shift+-) print "ord:".ord($a)."\n"; print 'hex:'.unpack("H*", $a)."\n"; my $b="\x1f"; if ($a eq $b) {print '$a eq $b'."\n";} exit;
    result
    ord:31 hex:1f $a eq $b
    but!!
    #use strict; use warnings; my $a=''; # entered by holding down cntrl pressing _ (shift+-) print "ord:".ord($a)."\n"; print 'hex:'.unpack("H*", $a)."\n"; my $b="\x1f"; if ($a eq $b) {print '$a eq $b'."\n";} my $bb=\x1f; print $bb."\n";
    Result
    ord:31 hex:1f $a eq $b SCALAR(0xa3e67c)
    so did you try printing \x1f or "\x1f" ?

      Thank you very much for the feedback. I tried both ways, neither worked. :(
Re: Control:CLI or Net::SSH2 - Send <ctrl>_
by ImJustAFriend (Scribe) on Feb 21, 2017 at 16:50 UTC
    Monks, I am forever indebted! Through your feedback, talking to some folks, and some experimentation... it now works as expected!

    So it turns out you MUST push enter before sending Ctrl_. So I coded that as a "put("\n")" (print and cmd instead of put returned a bunch of gobbledygook, put comes back clean). Glory be, the combo of that and your suggestions worked! Here is the final relevant chunk of code:

    if (defined($port)) { my $exit = ""; print " Connected To Switch $switch!\n"; $output = $obj->put("\n"); my $switchexit = $obj->put("\037"); sleep 2; $output = $obj->put("D"); } else { print " Not Connected To Switch $switch...\n"; } }
Re: Control:CLI or Net::SSH2 - Send <ctrl>_
by Mr. Muskrat (Canon) on Feb 20, 2017 at 23:07 UTC

    Without an SSCCE of the problem you are having, all we can do is guess but I'd rather not do that.

      I created a test script with the relevant code for this. The latest iteration of that code is seen below, with IP addresses/usernames/passwords changed for public sharing...

      #!C:/Perl64/bin/perl.exe use Control::CLI; my $mrvip = "1.2.6.4"; my $mrvuser = "mrvuser"; my $mrvpass = "mrvpass"; my $mrvport = "5"; my $mrvenpass = "mrvenpass"; my $oauser = "oauser"; my $oapass = "oapass"; my $bladeuser = "bladeuser"; my $bladepass = "bladepass"; my $obj; my $ok; my $output; my @blades = qw/1 2 3 4 5 6/; my @switches = qw/1 2/; $obj = new Control::CLI ( Use => 'SSH', Timeout => 10800, Input_log => "mrv_oa.input.log", Output_log => "mrv_oa.output.log", Dump_log => "mrv_oa.dump.log", ); $ok = $obj->connect( Host => $mrvip, Username => $mrvuser, Password => + $mrvpass,); $ok = $obj->login( Password => $mrvpass,); $output = $obj->cmd(""); my $oaconn = $obj->cmd("conn port asy $mrvport\n"); if ( $oaconn =~ m/.*Unable to connect to port.*/s ) { $output = $obj->cmd("en"); $output = $obj->cmd("$mrvenpass"); $output = $obj->cmd("logout port asy $mrvport"); $oaconn = $obj->cmd("conn port asy $mrvport"); } $output = $obj->cmd(""); my $oaloginu = $obj->cmd("$oauser"); my $oaloginp = $obj->cmd("$oapass"); foreach my $blade (<@blades>) { print "Connecting To Blade $blade...\n"; my $bladeconn = $obj->cmd("connect server serial $blade\n"); if ($bladeconn =~ m/service is unavailable/s ) { next; } my $port = $obj->port; if (defined($port)) { print " Connected To Blade $blade!\n"; my $bladexit1 = $obj->cmd("\033\050"); } else { print " Not Connected To Blade $blade...\n"; } } foreach my $switch (<@switches>) { print "Connecting To Switch $switch...\n"; my $bladeconn = $obj->cmd("connect interconnect $switch\n"); my $port = $obj->port; if (defined($port)) { print " Connected To Switch $switch!\n"; my $switchexit = $obj->print("\037"); # <== PROBLEM $output = $obj->put("D"); # <== PROBLEM } else { print " Not Connected To Switch $switch...\n"; } } #$output = $obj->cmd("quit"); #$output = $obj->print("\032"); #$output = $obj->print("\145"); #$output = $obj->cmd("e");

      The worst part is, in the dump log, I can see it sending the HEX to the remote machine, but it does not respond properly to the code (in this case, the "1f" 2 lines from bottom)...

      ... redacted for readability... < 0x00020: 0d 0a 45 73 63 61 70 65 20 63 68 61 72 61 63 74 ..Escap +e charact < 0x00000: 65 72 20 69 73 20 27 3c 43 74 72 6c 3e 5f 27 20 er is ' +<Ctrl>_' < 0x00010: 28 43 6f 6e 74 72 6f 6c 20 2b 20 53 68 69 66 74 (Contro +l + Shift < 0x00020: 20 2b 20 55 6e 64 65 72 73 63 6f 72 65 29 0d 0d + Unde +rscore).. < 0x00000: 0a 0d 0a 50 72 65 73 73 20 5b 45 6e 74 65 72 5d ...Pres +s [Enter] < 0x00010: 20 74 6f 20 64 69 73 70 6c 61 79 20 74 68 65 20 to dis +play the < 0x00020: 73 77 69 74 63 68 20 63 6f 6e 73 6f 6c 65 3a 20 switch +console: > 0x00000: 1f 0a .. > 0x00000: 44 D

        At the risk of going out on another limb today might i suggest you try replacing my $switchexit = $obj->print("\037"); # <== PROBLEM with  my $switchexit = $obj->put("\037"); sleep 2;, the sleep 2 serving to present a time break in case the switch implements a "time guard interval" like the old hayes AT command set did.

        > 0x00000: 1f 0a
        That's your escape character followed by a newline!

        So you aren't sending "Escape-D", but "Escape-Newline-D". You should use ->put instead of ->print for this.

        Of course, waiting some seconds, as huck recommends, doesn't hurt, but ->print's trailing output_record_separator would undo the escaping anyway…
Re: Control:CLI or Net::SSH2 - Send <ctrl>_
by ImJustAFriend (Scribe) on Feb 21, 2017 at 14:42 UTC
    Hi Monks, thanks for the assistance thus far. I made both changes listed above (put instead of print, add sleep 2) and unfortunately neither worked. Here's the new HEX output (relevant part)...

    < 0x00000: 0a 0d 0a 50 72 65 73 73 20 5b 45 6e 74 65 72 5d ...Pres +s [Enter] < 0x00010: 20 74 6f 20 64 69 73 70 6c 61 79 20 74 68 65 20 to dis +play the < 0x00020: 73 77 69 74 63 68 20 63 6f 6e 73 6f 6c 65 3a 20 switch +console: > 0x00000: 1f . > 0x00000: 44
      What should happen? IOW: what happens, if you connect using a ssh client and manually type these characters in?

      Ok, one more limb, this is but a mere hailmary guess.

      what if you first have to be in the console before even escape works?

      so in the original test code replace

      my $switchexit = $obj->print("\037"); # <== PROBLEM
      with
      $output = $obj->put("\x0a"); Sleep 1; # give it time to start the console; $output = $obj->put("\x1f"); Sleep 1; # give it time to for a "time guard interval" ;
      Sorry about going back to hex, i dont think as well in octal.