0

我有一个使用 Expect 1.15 创建的活动 ssh 会话。在本次会议中,我执行以下命令进行第二级身份验证。不幸的是,我必须使用以@字符开头的奇怪密码(例如@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";
        }
    ]
);

我手动尝试并且登录成功,所以凭据是正确的。运行代码时它总是出错(登录不正确),所以我相信期望不会发送显示的密码而是另一个字符串。

我尝试了太多类型的插值,也用 ascii 代码插值了字符串。

任何人都知道@ 字符是否会导致已插值字符串中的插值错误?

我无法更改密码,否则我可以排除预期错误。

有高手能帮帮我吗?谢谢!

编辑。添加的日志

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

1 回答 1

0

当您手动输入密码时,您按 Enter,而不是 LineFeed,因此最好发送 @password\r 而不是 \n。当终端处于原始非回显模式时,它可能会很挑剔。

于 2020-04-17T16:43:08.217 回答