【问题标题】:spawn: How to pass arguments to spawn within an expect script?spawn:如何在期望脚本中将参数传递给 spawn?
【发布时间】:2017-09-02 19:59:51
【问题描述】:

我只是有一个更大的 bash-script,我偶然发现了一个问题,使用 ssh 连接到另一台计算机。那是第一次访问(总是,真的;)),因此询问主机'[192.168.120.170]:9022([192.168.120.170]:9022)'的真实性无法建立。 确定要继续连接(是/否)?

所以我尝试使用 additional expect/spawn 脚本来回答这个问题。

所以我有两个文件: 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

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

如果我运行 deploy.sh,我会收到错误消息:invalid command name "192.168.120.170"

不知怎的,我没有让 spawn 运行 ssh

任何帮助将不胜感激。

编辑:

更改了 deploy.sh 第 4 行

【问题讨论】:

  • expect -d -f deploy_helper.exp -- 192.168.120.170 -p 9022 看看有什么问题。
  • 我添加了调试输出并更新了上面的 EDIT。输出为: argv[0] = /usr/bin/expect argv[1] = -d argv[2] = -f argv[3] = ./deployImage_helper.exp argv[4] = -- argv [5] = -p argv[6] = 9022 argv[7] = root@192.168.120.170 set argc 3 set argv0 "./deployImage_helper.exp" set argv "-p 9022 root@192.168.120.170" 从命令执行命令文件 ./deployImage_helper.exp spawn ssh -p 9022 root@192.168.120.170 父:等待同步字节 父:告诉子继续 父:现在与子生成不同步:返回 {6570}

标签: linux bash ssh parameter-passing expect


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

interact

"" 变量和方括号[] 内(在括号内计算命令并放置结果),将被扩展和计算。您需要引用方括号\[ 或使用{} 而不是""。 顺便说一句,您可以通过 openssh 选项禁止询问此问题(仅显示警告):

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

【讨论】:

  • 非常感谢科马尔
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-26
  • 1970-01-01
  • 1970-01-01
  • 2021-07-03
  • 1970-01-01
  • 2015-11-19
  • 1970-01-01
相关资源
最近更新 更多