bash 如何在bash脚本中使用expect

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15133474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:23:54  来源:igfitidea点击:

how to use expect inside a bash script

bashexpect

提问by munish

This is the code snippet i am using in the followingbash 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) :

我很期待并开始测试这个(它失败了):

#!/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 [email protected] {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 expect script or it expect can be used inside bash script if it can be done:

我是否需要在expect脚本中重新编写bash脚本,或者如果可以的话,它可以在bash脚本中使用它:

more over i need to get the bash variable $command, $username, $password, $ipaddressand use it in the expect part

此外,我需要获取 bash 变量$command$username$password$ipaddress并在期望部分使用它

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,logfiles

你会建议什么解决方案?或者我可以创建一个期望脚本并从 bash 脚本中调用它只是为了登录、错误处理、执行、日志文件

回答by V H

well you will need to run two seperate scripts a shell script that calls an expect script

那么你将需要运行两个单独的脚本一个调用期望脚本的 shell 脚本

#!/usr/bin/bash


set force_conservative 0  ;

change above to

将上面更改为

#!/usr/bin/expect


set force_conservative 0  ;

or alternatively in your shell script I am unsure about format but you can send expect -c with 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 [email protected] {uname -a; df -h}
match_max 100000
expect "*?assword: "
send -- "bar01\r"
expect eof
EOF