我试图运行这个脚本,但修改时有不同的错误。 这里是代码和输出。 请帮忙。
在post结尾更新debugging信息
#!/bin/bash (( $# != 1 )) && { echo >&2 "Usage: $0 \"[COMMAND]\""; exit 1; } servers_addresses=(10.10.10.10 ) for server_address in ${servers_addresses[@]}; do expect <<EOF spawn ssh -t root@$server_address "$*" expect -timeout 2 "Are you sure you want to continue connecting (yes/no)?" { send "yes\n" } expect "s password:" { send "Correct_Password\n" } expect "s password:" { send "Wrong_Password_22222\n" } expect "s password:" { send "Wrong_Password_33333\n" } expect eof EOF done
输出如下所示:
goldberg188@Test-Server ~$ ./test.sh "sudo cat /etc/hosts" spawn ssh -t root@10.10.10.10 sudo cat /etc/hosts root@10.10.10.10's password: # Do not remove the following line, or various programs # that require network functionality will fail. 10.10.10.10 TEST-004 localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6 Connection to 10.10.10.10 closed. expect: spawn id exp4 not open while executing "expect "s password:" { send "Wrong_Password_33333\n" }"
如果我这样修改,那么输出会有点不同
expect "s password:" { send "Wrong_Password_11111\n" } expect "s password:" { send "Correct_Password\n" } expect "s password:" { send "Wrong_Password_33333\n" } goldberg188@Test-Server ~$ ./test.sh "sudo cat /etc/hosts" spawn ssh -t root@10.10.10.10 sudo cat /etc/hosts root@10.10.10.10's password: # Do not remove the following line, or various programs # that require network functionality will fail. 10.10.10.10 TEST-004 localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6 Connection to 10.10.10.10 closed. expect: spawn id exp4 not open while executing "expect eof"
如果第三行的密码正确,那么根本就没有错误。 在这一个工作很好。
expect "s password:" { send "Wrong_Password_11111\n" } expect "s password:" { send "Wrong_Password_22222\n" } expect "s password:" { send "Correct_Password\n" } goldberg188@Test-Server ~$ ./test.sh "sudo cat /etc/hosts" spawn ssh -t root@10.10.10.10 sudo cat /etc/hosts root@10.10.10.10's password: # Do not remove the following line, or various programs # that require network functionality will fail. 10.10.10.10 TEST-004 localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6 Connection to 10.10.10.10 closed.
更新:debugging信息 – 修改为
exp_internal 1 expect "s password:" { send "Wrong_Password_11111\n" } expect "s password:" { send "Correct_Password\n" } expect "s password:" { send "Wrong_Password_33333\n" }
输出:
goldberg188@Test-Server ~$ ./test.sh "sudo cat /etc/host" spawn ssh -t root@10.10.10.10 sudo cat /etc/host root@10.10.10.10's password: expect: does "root@10.10.10.10's password: " (spawn_id exp4) match glob pattern "s password:"? yes expect: set expect_out(0,string) "s password:" expect: set expect_out(spawn_id) "exp4" expect: set expect_out(buffer) "root@10.10.10.10's password:" send: sending "Wrong_Password_11111\n" to { exp4 } expect: does " " (spawn_id exp4) match glob pattern "s password:"? no expect: does " \r\n" (spawn_id exp4) match glob pattern "s password:"? no Permission denied, please try again. root@10.10.10.10's password: expect: does " \r\nPermission denied, please try again.\r\r\nroot@10.10.10.10's password: " (spawn_id exp4) match glob pattern "s password:"? yes expect: set expect_out(0,string) "s password:" expect: set expect_out(spawn_id) "exp4" expect: set expect_out(buffer) " \r\nPermission denied, please try again.\r\r\nroot@10.10.10.10's password:" send: sending "Correct_Password\n" to { exp4 } expect: does " " (spawn_id exp4) match glob pattern "s password:"? no expect: does " \r\n" (spawn_id exp4) match glob pattern "s password:"? no cat: /etc/host: No such file or directory Connection to 10.10.10.10 closed. expect: does " \r\ncat: /etc/host: No such file or directory\r\r\nConnection to 10.10.10.10 closed.\r\r\n" (spawn_id exp4) match glob pattern "s password:"? no expect: read eof expect: set expect_out(spawn_id) "exp4" expect: set expect_out(buffer) " \r\ncat: /etc/host: No such file or directory\r\r\nConnection to 10.10.10.10 closed.\r\r\n" expect: spawn id exp4 not open while executing "expect eof"
根据你的代码,看起来ssh
连接在给ssh会话的密码之后关闭了。
每当使用spawn
命令产生一个新进程时, expect
会将该期望进程的spawn_id
保存到expect_out(spawn_id)
。
根据你的代码,期望的spawn_id是在遇到时产生的
spawn ssh -t root@$server_address "$*"
你看到的调试如下。
spawn ssh -t root@10.10.10.10 sudo cat /etc/host root@10.10.10.10's password: expect: does "root@10.10.10.10's password: " (spawn_id exp4) match glob pattern "s password:"? yes expect: set expect_out(0,string) "s password:" expect: set expect_out(spawn_id) "exp4"
正如你可以在调试信息中看到的, expect_out(spawn_id)
包含spawn_id
,在你的情况下,它的值是exp4
。
正如你所看到的,连接在几条错误路径后被关闭,从而使exp4
进程不再在上下文中退出。 由于spawn_id
持有引用相同,期望将尝试期望从该过程失败。
你可以参考这个问题来了解这个spawn_id
如何与标准输入(从控制台读取输入)