我尝试运行此脚本,但修改后出现不同的错误。这是代码和输出。请帮忙。
文章末尾有更新调试信息
#!/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 个命令。
更新:调试信息-修改为
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"
答案1
假设您不是故意发送错误的密码,请使用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
答案2
致谢:格伦·杰克曼
抱歉,Glenn,你的代码需要一些修改,我在这里添加了这些修改并使其正常运行。
这是其他可能需要它的人的修复代码。您可以将 IP 放在文本文件中,然后在 servers_addresses 内进行 cat。例如 servers_addresses=('cat 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"
搜索和替换
./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