期望脚本错误发送:Spawn id exp4在执行时未打开

更新:为其他人添加工作代码

CREDITS: https : //serverfault.com/users/30957/glenn-jackman

@Glenn对不起! 只是道歉,我的意思是!

对不起,格伦你的代码需要一些改变,我在这里添加并得到它的工作。 谢谢你! 抱歉我的粗鲁。 这里是任何需要它的人的固定代码。 你可以把这些IP放在一个文本文件中,然后放在servers_addresses里面。 像servers_addresses =('猫ip_list.txt')在IP列表添加IP是这样的10.10.10.1 10.10.10.2 10.10.10.3

如果需要还有更多的例子。 运行一个命令:

./test.sh "sudo cat /etc/hosts" 

运行多个命令

  ./test.sh "sudo cat /etc/hosts & /etc/init.d/network status" 

search并replace

  ./test.sh "sed -i -e 's/SEARCH_STRING/REPLACE_STRING/g' /tmp/FileNAME.txt" 

完整的代码:

  #!/bin/bash (( $# != 1 )) && { echo >&2 "Usage: $0 \"[COMMAND]\""; exit 1; } servers_addresses=( 10.10.10.10 192.168.10.1 ) for server_address in ${servers_addresses[@]}; do expect <<EOF set passwds { password1 password2 password3 } set i 0 spawn ssh -t root@$server_address "$*" expect { "Are you sure you want to continue connecting (yes/no)?" { send "yes\r"; exp_continue } "s password:" { send "[lindex \$passwds \$i]\r"; incr i; exp_continue } eof } EOF done 

我试图运行这个脚本,但修改时有不同的错误。 这里是代码和输出。 请帮忙。

在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 [email protected] sudo cat /etc/hosts [email protected]'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 [email protected] sudo cat /etc/hosts [email protected]'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 [email protected] sudo cat /etc/hosts [email protected]'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. 

忽略编辑:我知道如何一次运行2个命令。

更新: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 [email protected] sudo cat /etc/host [email protected]'s password: expect: does "[email protected]'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) "[email protected]'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. [email protected]'s password: expect: does " \r\nPermission denied, please try again.\r\r\[email protected]'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\[email protected]'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" 

假设你没有故意发送错误的密码,使用exp_continue作为循环结构:

 expect <<EOF set passwds {foo bar baz} set i 0 spawn ssh -t root@$server_address "$*" expect { "continue connecting (yes/no)?" { send "yes\r"; exp_continue } " password: " { send "[lindex $passwds $i]\r"; incr i; exp_continue } eof } EOF 

CREDITS: https : //serverfault.com/users/30957/glenn-jackman

@Glenn对不起! 只是道歉,我的意思是!

对不起,格伦你的代码需要一些改变,我在这里添加并得到它的工作。 谢谢你! 抱歉我的粗鲁。 这里是任何需要它的人的固定代码。 你可以把这些IP放在一个文本文件中,然后放在servers_addresses里面。 像servers_addresses =('猫ip_list.txt')在IP列表添加IP是这样的10.10.10.1 10.10.10.2 10.10.10.3

如果需要还有更多的例子。 运行一个命令:

  ./test.sh "sudo cat /etc/hosts" 

运行多个命令

  ./test.sh "sudo cat /etc/hosts & /etc/init.d/network status" 

search并replace

  ./test.sh "sed -i -e 's/SEARCH_STRING/REPLACE_STRING/g' /tmp/FileNAME.txt" 

完整的代码:

  #!/bin/bash (( $# != 1 )) && { echo >&2 "Usage: $0 \"[COMMAND]\""; exit 1; } servers_addresses=( 10.10.10.10 192.168.10.1 ) for server_address in ${servers_addresses[@]}; do expect <<EOF set passwds { password1 password2 password3 } set i 0 spawn ssh -t root@$server_address "$*" expect { "Are you sure you want to continue connecting (yes/no)?" { send "yes\r"; exp_continue } "s password:" { send "[lindex \$passwds \$i]\r"; incr i; exp_continue } eof } EOF done