【发布时间】:2015-12-10 17:08:22
【问题描述】:
我有一个使用 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: ()
【问题讨论】:
-
添加一些调试输出:
$exp->debug(1); -
是的,
@将插入双引号字符串。但是,它不会出现在单引号字符串中;要使用单个send调用,您可以执行->send( '@password' . "\n" )之类的操作来连接换行符。 -
我似乎记得一个错误,您可以过快地回复密码:提示,而遥控器正在执行 ioctl 以抑制回显,因此它可能会丢失字符。尝试在匹配密码提示后添加一个简短的
sleep 1,然后再发送密码。
标签: perl authentication interpolation telnet expect.pm