gue*_*_90 2 linux ssh bash expect parameter-passing
我只是有一个更大的bash 脚本,我在其中遇到了一个问题,即使用ssh连接到另一台计算机。该主机是第一次访问(总是,真的;)),因此询问主机 '[192.168.120.170]:9022 ([192.168.120.170]:9022)' 的真实性无法确定。您确定要继续连接吗(是/否)?
所以我尝试使用额外的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
Run Code Online (Sandbox Code Playgroud)
部署助手.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"
Run Code Online (Sandbox Code Playgroud)
如果我运行deploy.sh,我会收到错误消息:invalid command name "192.168.120.170"
不知怎的,我没有让 spawn 运行ssh。
任何帮助,将不胜感激。
编辑:
更改了deploy.sh第4行
小智 5
set args [lrange $argv 1 end]
spawn ssh {*}$args
expect -re {The authenticity of .* can't be established.} {
send "yes"
}
interact
Run Code Online (Sandbox Code Playgroud)
内部""变量和方括号[](计算括号内的命令并放置结果)将被扩展和评估。您需要引用方括号\[或使用{}代替""。顺便说一句,您可以通过 openssh 选项禁止询问这个问题(仅显示警告):
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p 9022 root@192.168.120.170
Run Code Online (Sandbox Code Playgroud)