linux – Expect脚本错误发送:Spawn id exp4在执行时未打开

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

信用额度:https://serverfault.com/users/30957/glenn-jackman

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

对不起Glenn您的代码需要进行一些更改,我在这里添加并使其工作.谢谢你!抱歉我的粗鲁.
以下是其他需要它的人的固定代码.您可以将IP放在文本文件中,然后将cat放在servers_addresses中.像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

我正在尝试运行此脚本,但在修改时出现了不同的错误.这是代码和输出.请帮忙.

使用调试信息更新帖子的末尾

#!/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.

忽略编辑:我知道如何一次运行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 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"

解决方法

假设您没有故意发送错误的密码,请使用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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


查找全部容器的日志文件 $ sudo find /var/lib/docker/containers -name *.log 查看日志位置 $ docker inspect --format=&#39;{{.LogPath}}&#39; &lt;container_name&gt; 实时查询内容 $
Linux日志文件中列属性的详细解析
在Linux系统中没有duf命令,如何有效地管理磁盘空间?
深入探讨EncryptPad在Linux操作系统中的功能和优势
原理和应用场景:Linux中ttyload工具的工作原理和实际用途
深度解析SELinux的三种策略类型
评估Linux系统性能的ttyload工具使用效果
分享在Linux系统中检测SSH版本的方法
介绍Linux平台上的数据加密工具EncryptPad
在Linux系统中,如何查看和诊断块设备信息?
在Linux环境下如何查看块设备信息?
探索Linux操作系统下的数据加密工具EncryptPad
学会在Linux系统中查看硬盘信息
分析SELinux:原理与实践
掌握SELinux策略类别
技巧:有效解读和管理Linux日志文件
查看Linux系统中的所有用户
了解Linux系统中各种不同类型的日志文件
深入理解Linux PS命令
方法:在Linux操作系统中查看用户