简体   繁体   English

如何在 Bash 脚本中使用 Expect

[英]How to use Expect inside a Bash script

This is the code snippet I am using in the following Bash script:这是我在以下Bash 脚本中使用的代码片段:

  for user_input in `awk '{print}' testfile_$$.txt`
    do
    ipaddress=`echo $user_input | cut -d';' -f 1`
    command="${config_mode}`echo $user_input | cut -d';' -f 2-`"
            ping -w 1 $ipaddress 1> /dev/null 2> $ERR_LOG_FILE 1> $LOG_FILE
    if [ $? -eq 0 ];then
            ssh "$USERNAME@$ipaddress" "$command"
                                                  >> $LOG_FILE


    fi
    done

How do I use Expect to automate the SSH login in this script?我如何在这个脚本中使用 Expect 来自动化 SSH 登录?

I am very new to Expect and started testing this (it failed):我对 Expect 非常陌生并开始测试这个(它失败了):

#!/usr/bin/bash


set force_conservative 0  ;# Set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .1}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}

#

set timeout -1
spawn ssh auto21@10.38.227.229 {uname -a; df -h}
match_max 100000
expect "*?assword: "
send -- "bar01\r"
expect eof

Do I need to write the Bash script all over again in an Expect script or can Expect be used inside a Bash script?我是否需要在 Expect 脚本中重新编写 Bash 脚本,或者可以在 Bash 脚本中使用 Expect 吗?

If it can be done:如果可以做到:

Moreover, I need to get the Bash variables $command , $username , $password , and $ipaddress and use it in the Expect part.此外,我需要获取 Bash 变量$command$username$password$ipaddress并在 Expect 部分使用它。

What solution would you suggest?你会建议什么解决方案?

Or can I create an Expect script and call it from the Bash script just for login, error handling, execution, and logfiles.或者我可以创建一个 Expect 脚本并从 Bash 脚本中调用它,仅用于登录、错误处理、执行和日志文件。

Well, you will need to run two separate scripts, a shell script that calls an Expect script:好吧,您将需要运行两个单独的脚本,一个调用 Expect 脚本的 shell 脚本:

#!/usr/bin/bash


set force_conservative 0  ;

Change the above to把上面的改成

#!/usr/bin/expect

set force_conservative 0  ;

Or alternatively in your shell script I am unsure about the format, but you can send expect -c with the command to execute:或者,在您的 shell 脚本中,我不确定格式,但是您可以发送带有要执行的命令的expect -c

expect -c "send \"hello\n\"" -c "expect \"#\""
expect -c "send \"hello\n\"; expect \"#\""

Actually, there is also one other alternative:实际上,还有另一种选择:

#!/bin/bash

echo "shell script"

/usr/bin/expect<<EOF

set force_conservative 0  ;# Set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .1}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}

#

set timeout -1
spawn ssh auto21@10.38.227.229 {uname -a; df -h}
match_max 100000
expect "*?assword: "
send -- "bar01\r"
expect eof
EOF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM