简体   繁体   English

spawn:如何传递参数以在Expect脚本中生成?

[英]spawn: How to pass arguments to spawn within an expect script?

I just have a bigger bash-script where I stumbled into a problem, to use ssh to connect to another computer. 我只是遇到了一个更大的bash脚本 ,偶然发现一个问题,可以使用ssh连接到另一台计算机。 That one is visited first time (always, really ;) ) and therefore asks The authenticity of host '[192.168.120.170]:9022 ([192.168.120.170]:9022)' can't be established. 第一次访问(总是,实际上是;)),因此询问主机'[192.168.120.170]:9022([192.168.120.170]:9022)的真实性无法建立。 Are you sure you want to continue connecting (yes/no)? 您确定要继续连接(是/否)吗?

So I tried to answer this question using an additional expect/spawn script. 因此,我尝试使用其他的期望/生成脚本来回答这个问题。

So I have two files: deploy.sh 所以我有两个文件: deploy.sh

#!/bin/bash
... Some functions and more
# call expect script to get a first time onto the target
/usr/bin/expect -d -f ./deployImage_helper.exp -- -p 9022 root@192.168.120.170

deploy_helper.exp deploy_helper.exp

#!/usr/bin/env expect  -f
set args [lrange $argv 1 end]
spawn ssh {*}$args
expect "The authenticity of host'[192.168.120.170]([192.168.120.170]:9022)' can't be established."
send "yes"

If I run deploy.sh I get the error message: invalid command name "192.168.120.170" 如果我运行deploy.sh,则会收到错误消息: 无效的命令名称“ 192.168.120.170”

Somehow I don't get spawn running ssh . 不知何故,我没有让ssh运行。

Any help would be appreciated. 任何帮助,将不胜感激。

EDIT: 编辑:

Changed deploy.sh line 4 更改了deploy.sh第4行

set args [lrange $argv 1 end]
spawn ssh {*}$args
expect -re {The authenticity of .* can't be established.} {
    send "yes"
}

interact

Inside "" variables and square brakets [] (evaluate command inside brackets and place result instead), will be expanded and evaluated. ""变量和方括号[] (将方括号内的命令评估并放置结果),将进行扩展和评估。 You need quote square brackets \\[ or use {} instead of "" . 您需要使用方括号\\[或使用{}代替"" BTW, you can supress asking this question (only warning showing) by openssh options: 顺便说一句,您可以通过openssh选项禁止问这个问题(仅显示警告):

ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p 9022 root@192.168.120.170

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

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