我创建了2个tcl文件,login.tcl和x.tcl,预计在ssh之后将pwd命令发送到服务器,但是它不起作用。 x.tcl中的脚本如下所示。
package require Expect
source ./login.tcl
set ipaddr 172.19.3.4
set passwd 123456
exp_internal 1
global g_prompt
global g_pwd_prompt
set g_prompt #
set g_pwd_prompt password
set loginName root@$ipaddr
set spawnid_test [login $loginName $passwd]
if {$spawnid_test == -1} {
puts "ssh failed, exit!\n"
exit 1
} else {
exp_send -i $spawnid_test "pwd\n"
}
输出结果为:
[jeff@jeff ~]$ tclsh x.tcl
spawn ssh root@172.19.3.4
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {31879}
expect: does "" (spawn_id exp6) match glob pattern "yes/no"? no
"password"? no
"#"? no
"Connection refused"? no
root@172.19.3.4's password:
expect: does "root@172.19.3.4's password: " (spawn_id exp6) match
glob pattern "yes/no"? no
"password"? yes
expect: set expect_out(0,string) "password"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "root@172.19.3.4's password"
send: sending "123456\n" to { exp6 }
expect: continuing expect
expect: does ": " (spawn_id exp6) match glob pattern "yes/no"? no
"password"? no
"#"? no
"Connection refused"? no
expect: does ": \r\n" (spawn_id exp6) match glob pattern "yes/no"? no
"password"? no
"#"? no
"Connection refused"? no
Last login: Sun May 4 12:59:35 2014 from 172.19.3.3
expect: does ": \r\nLast login: Sun May 4 12:59:35 2014 from 172.19.3.3
\r\r\n"(spawn_id exp6) match glob pattern "yes/no"? no
"password"? no
"#"? no
"Connection refused"? no
expect: does ": \r\nLast login: Sun May 4 12:59:35 2014
from 172.19.3.3\r\r\n\u001b]0;root@172.19.3.4:~\u0007" (spawn_id exp6) match glob
pattern "yes/no"? no
"password"? no
"#"? no
"Connection refused"? no
[root@172.19.3.4 ~]#
expect: does ": \r\nLast login: Sun May 4 12:59:35 2014
from 172.19.3.3\r\r\n\u001b]0;root@172.19.3.4:~\u0007[root@172.19.3.4 ~]# " (spawn_id
exp6) match glob pattern "yes/no"? no
"password"? no
"#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) ": \r\nLast login: Sun May 4 12:59:35 2014
from 172.19.3.3\r\r\n\u001b]0;root@172.19.3.4:~\u0007[root@172.19.3.4 ~]#"
login successful, spawn_id = exp6
send: sending "pwd\n" to { exp6 }
从日志中,问题是为什么"发送:发送" 123456 \ n"至{exp6}"有效,但是"发送:发送" pwd \ n"至{exp6}"不?