1
votes

I am trying to connect to switch which is signaling point switch.

I need to execute the below commands to get to login terminal.

  1. telnet IP Port
  2. Send CTRL+A
  3. Get Prompt ">"
  4. Send command login:uid=user.
  5. Requests for Password provide password.
  6. Gets terminal ">"

Then I have to continue executing some commands to go further, but my problem I am facing an issue while sending CTRL+A.

When I send "ctrl+A" using Expect it just prints "^A" and waits doesn't provide me the terminal.

So, I modified the script by changing the command to "ctrl+A\n" which gives me the terminal but with new line on my next terminal prompt.

Like the below output:

^A
^A
^A

>

wait's here at next line.

which fails to match my next command regular expression ">" and doesn't send the login name.

Can somebody tell me why my first command "^A" fails to get me terminal? And why the command was executed three times before I get the terminal?

I manual scenario it works fine for single ctrl+A

My Sample code:

use Expect;

my $exp = Expect->spawn("telnet 10.10.1.35 2020");

$exp->expect($timeout,
               [ qr/]'./ => sub {my $exp = shift;
                                    $exp->send("\cA\n");
                                      } ]
);
$exp->expect($timeout,
               [ qr/>/ => sub { my $exp = shift;
                                     $exp->send("login:uid=user\n");
                                      } ]
);
$exp->expect($timeout,
               [ qr/Enter Password :/ => sub { my $exp = shift;
                                     $exp->send("xxx\n");
                                      } ]
);

Thank you, Pradeep.

1
Try $exp->send(chr(1)); to send a Ctrl-A - glenn jackman
I got the ctrl+A character and it didn't move any further it was just waiting. - user2733721

1 Answers

0
votes

Try With send "\01"

Please consult for more info http://expect.sourceforge.net/FAQ.html#q54

I like to enter most control characters literally - since the resulting script is more readable. However, your editor has to allow this. For example, suppose you want to send a Control-A. Conventionally, most editors display this as ^A, but you can't just enter ^ and A. (This will just send those two characters.) Most editors have some simple quoting mechanism that lets you enter the next character literally. For example, using emacs, I can enter ^Q^A and that will add the single character ^A.

Alternatively, Tcl provides a way of encoding using octal or hex. The octal encoding mechanism is less error-prone than hex so I'll demonstrate using octal. For example, since ^A has the octal value 1 in ASCII, to send a ^A:

send "\01"