Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 预期ssh登录后脚本不会执行_Bash_Unix_Expect - Fatal编程技术网

Bash 预期ssh登录后脚本不会执行

Bash 预期ssh登录后脚本不会执行,bash,unix,expect,Bash,Unix,Expect,我正在尝试编写一个expect脚本: 连接到提供用户和密码的远程服务器 循环读取每一行的本地文件 在远程服务器上为每一行执行特定命令 我可以成功地完成步骤1,并用一个简单的场景测试步骤3,但还不能让它工作。不幸的是,在脚本的第8行中,在发送密码后,我只是登录到服务器,就像手动登录一样(我可以与控制台交互),其余的都没有执行 我怎样才能避开这个问题 以下是脚本: #!/usr/bin/expect set timeout 20 set ip [lindex $argv 0] set user [l

我正在尝试编写一个expect脚本:

  • 连接到提供用户和密码的远程服务器
  • 循环读取每一行的本地文件
  • 在远程服务器上为每一行执行特定命令
  • 我可以成功地完成步骤1,并用一个简单的场景测试步骤3,但还不能让它工作。不幸的是,在脚本的第8行中,在发送密码后,我只是登录到服务器,就像手动登录一样(我可以与控制台交互),其余的都没有执行

    我怎样才能避开这个问题

    以下是脚本:

    #!/usr/bin/expect
    set timeout 20
    set ip [lindex $argv 0]
    set user [lindex $argv 1]
    set password [lindex $argv 2]
    spawn ssh -t -t "$user\@$ip"
    expect "Password:"
    send "$password\r";
    expect "NYXOOBPN402(config)$"
    send "delete decoders:ASX-Trade24-FIX.mdp\r"
    expect "Are you sure you want to delete 'decoders:ASX-Trade24-FIX.mdp' (y/n)?"
    send "y\r";
    
    这就是我执行它的方式:

    ./test_expect.sh 172.18.250.20 admin admin
    

    问题是我的期望在第17行中不正确。我不应该使用“NYXOOBPN402(config)$”,而应该使用“*(config)*”,因为在这部分之前有很多文本没有匹配

    这是我为遇到同样问题的人编写的最后一个脚本:

    #!/usr/bin/expect
    set timeout 9
    # Check if the parameters are correct
    if {[llength $argv] == 0} {
          send_user "Usage: ./test_expect.sh ip username password\n"
            exit 1
        }
    
    # Read the file with all the decoders names to be deleted
    set f [open "decoders.txt"]
    set decoders [split [read $f] "\n"]
    close $f
    
    # debug mode - very useful:
    #exp_internal 1
    
    # Connect to the server
    set ip [lindex $argv 0]
    set user [lindex $argv 1]
    set password [lindex $argv 2]
    spawn ssh -t "$user\@$ip"
    expect "Password: "
    send "$password\r";
    sleep 3
    # send ctrl+c since this terminal shows a lot of decoders
    #send \x03
    
    expect {
        default { send_user "\nCould not find the expected value.\n"; exit 1 }
        "*(config)*" {
            # Loop through all the decoders
            foreach decoder $decoders {
                #send_user "Removing $decoder\n"
                send "delete decoders:$decoder\r"
                expect {
                    "Are you sure you want to delete*" { send "y\r" }
                    "*decoder will still be active*" { send_user "\nRemoved $decoder successfully\n" }
                    "*no such file or directory" { send_user "\nDecoder $decoder already deleted.\n" }
                    default { send_user "\nNot expected value with $decoder, please debug.\n"; exit 1 }
                }
            }
        }
    }
    

    这可能是意料之中的事。你试过记录输出吗?我试过了,问题是这个服务器没有一个正常的控制台。我不知道您是否了解Corvil应用程序,但这是一个Corvil管理控制台,因此我必须使用类似expect的工具来完成此任务。请看一看仅使用shell代码编写“expect”脚本的工具。