0

I have an active ssh session created with Expect 1.15. In this session I execute the following commands for a second level of authentication. Unfortunately i have to use a strange password which starts with @ character (example @johndoe)

$exp->send("telnet 10.0.0.1\n");
$exp->expect(
    10,
    [
        qr/login: / => sub {
            $exp->send("johndoe\n");
            +exp_continue;
        }
    ],
    [
        qr/assword: / => sub {
            $exp->send('@johndoe' . "\n");
            +exp_continue;
        }
    ],
    [
        qr/\[\w+\]\#/ => sub {
            print "Success\n";
        }
    ],
    [
        qr/Login incorrect/ => sub {
            print "Login Incorrect\n";
        }
    ]
);

I tried manually and login is successfull, so credentials are correct. When run the code it goes always in error (Login Incorrect), so i believe that expect doesn't send displayed password but another string.

I tried too many type of interpolations and interpolated strings also with ascii codes.

Anybody knows if the @ character can cause interpolation errors in already interpolated string?

I can't change password otherwise I could exclude an expect bug.

An expert can help me? Thanks!

EDIT. Added logs

login:
spawn id(3): Does `telnet 10.0.0.1\r\nTrying 10.0.0.1...\r\nConnected to 10.0.0.1.\r\n\r\nlogin: '
match:
  pattern #1: -re `(?-xism:login: )'? YES!!
    Before match string: `telnet 10.0.0.1\r\nTrying 10.0.0.1...\r\nConnected to 10.0.0.1.\r\n'
    Match string: `login: '
    After match string: `'
    Matchlist: ()
Calling hook CODE(0x4064e0ac)...
Sending 'johndoe\n' to spawn id(3)
        Expect::print('Expect=GLOB(0x405d6920)','johndoe\x{a}') called at my_script.pl line 195
        main::__ANON__('Expect=GLOB(0x405d6920)') called at /opt/perl_32/lib/site_perl/5.8.3/Expect.pm line 733
        Expect::_multi_expect(10,'undef','ARRAY(0x40653f4c)') called at /opt/perl_32/lib/site_perl/5.8.3/Expect.pm line 536
        Expect::expect('Expect=GLOB(0x405d6920)',10,'ARRAY(0x4064e040)','ARRAY(0x40653ea4)','ARRAY(0x405d6c44)','ARRAY(0x40653f04)') called at my_script.pl line 224
Continuing expect, restarting timeout...

spawn id(3): Does `'
match:
  pattern #1: -re `(?-xism:login: )'? No.
  pattern #2: -re `(?-xism:assword: )'? No.
  pattern #3: -re `(?-xism:\[\w+\]\w+\#)'? No.
  pattern #4: -re `(?-xism:Login incorrect)'? No.

Waiting for new data (10 seconds)...
spawn id(3): new data.
spawn id(3): read 9 byte(s).
johndoe

spawn id(3): Does `johndoe\r\n'
match:
  pattern #1: -re `(?-xism:login: )'? No.
  pattern #2: -re `(?-xism:assword: )'? No.
  pattern #3: -re `(?-xism:\[\w+\]\w+\#)'? No.
  pattern #4: -re `(?-xism:Login incorrect)'? No.

Waiting for new data (10 seconds)...
spawn id(3): new data.
spawn id(3): read 10 byte(s).
Password:
spawn id(3): Does `johndoe\r\nPassword: '
match:
  pattern #1: -re `(?-xism:login: )'? No.
  pattern #2: -re `(?-xism:assword: )'? YES!!
    Before match string: `johndoe\r\nP'
    Match string: `assword: '
    After match string: `'
    Matchlist: ()
Calling hook CODE(0x404b4f44)...
Sending '@johndoe\n' to spawn id(3)
        Expect::print('Expect=GLOB(0x405d6920)','@johndoe\x{a}') called at my_script.pl line 205
        main::__ANON__('Expect=GLOB(0x405d6920)') called at /opt/perl_32/lib/site_perl/5.8.3/Expect.pm line 733
        Expect::_multi_expect(10,'undef','ARRAY(0x40653f4c)') called at /opt/perl_32/lib/site_perl/5.8.3/Expect.pm line 536
        Expect::expect('Expect=GLOB(0x405d6920)',10,'ARRAY(0x4064e040)','ARRAY(0x40653ea4)','ARRAY(0x405d6c44)','ARRAY(0x40653f04)') called at my_script.pl line 224
Continuing expect, restarting timeout...

spawn id(3): Does `'
match:
  pattern #1: -re `(?-xism:login: )'? No.
  pattern #2: -re `(?-xism:assword: )'? No.
  pattern #3: -re `(?-xism:\[\w+\]\w+\#)'? No.
  pattern #4: -re `(?-xism:Login incorrect)'? No.

Waiting for new data (10 seconds)...
spawn id(3): new data.
spawn id(3): read 2 byte(s).


spawn id(3): Does `\r\n'
match:
  pattern #1: -re `(?-xism:login: )'? No.
  pattern #2: -re `(?-xism:assword: )'? No.
  pattern #3: -re `(?-xism:\[\w+\]\w+\#)'? No.
  pattern #4: -re `(?-xism:Login incorrect)'? No.

Waiting for new data (10 seconds)...
spawn id(3): new data.
spawn id(3): read 24 byte(s).
Login incorrect
login:
spawn id(3): Does `\r\nLogin incorrect\r\nlogin: '
match:
  pattern #1: -re `(?-xism:login: )'? YES!!
    Before match string: `\r\nLogin incorrect\r\n'
    Match string: `login: '
    After match string: `'
    Matchlist: ()
pynexj
  • 19,215
  • 5
  • 38
  • 56
user3402702
  • 91
  • 1
  • 11
  • add some debug output: `$exp->debug(1);` – meuh Sep 14 '15 at 17:15
  • Yes, `@` will interpolate in a double-quoted string. However, it will not in a single-quoted string; to use a single `send` call, you could do something like `->send( '@password' . "\n" )` to concatenate a newline. – thrig Sep 14 '15 at 17:17
  • I seem to remember of a bug whereby you can reply too quickly to the password: prompt, whilst the remote is doing an ioctl to suppress echo, and so it can lose characters. Try adding a short `sleep 1` after matching the password prompt before sending the password. – meuh Sep 18 '15 at 18:24

1 Answers1

0

When you type the passwords manually, you press Enter, not LineFeed, so better send @password\r not \n. When the terminal is in raw non-echo mode, it might be picky about it.

P.Earl
  • 1