Perl Expect authentication with telnet

时间:2015-09-14 16:07:33

标签: perl authentication interpolation telnet expect.pm

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: ()

1 个答案:

答案 0 :(得分:0)

手动输入密码时,请按Enter键,而不是LineFeed,因此最好发送@password \ r而不是\ n。 当终端处于原始非回波模式时,可能对此有些挑剔。