linux - 无法通过 expect 对 bash 命令脚本进行 SSH

标签 linux bash ssh expect

我正在尝试将一组命令推送到多个远程主机。我可以根据个人情况使用以下内容:
ssh user@remoteHost "bash -s" <./commands.sh
我可以把它放到一个循环中,但是我不得不输入 n 次密码。在 SSH 配置文件中禁用密码提示对我来说不是一个选项。 我试图在循环中使用 expect,但我无法让它工作。

#!/bin/bash  
HOSTS="host1 host2"  
read -sp "Password: " PASSWORD  
for HOST in $HOSTS; do  
    expect -c "  
    spawn /usr/bin/ssh user@$HOST "bash -s" <./commands.sh  
    expect_before {
        "*yes/no*" {send "yes"\r;exp_continue}}
    expect {
        "*Password*" {send $PASSWORD\r;interact}}
    exit"
done

我收到以下错误:

spawn /usr/bin/ssh root@host1 bash  
expect: invalid option -- 's'  
usage: expect [-div] [-c cmds] [[-f] cmdfile] [args]  
spawn /usr/bin/ssh root@host2 bash  
expect: invalid option -- 's'  
usage: expect [-div] [-c cmds] [[-f] cmdfile] [args]  

有什么想法吗?似乎 expect 正在尝试解释 bash 命令。我不确定如何阻止它。

最佳答案

解决方法:
替换
spawn /usr/bin/ssh user@$HOST "bash -s" <./commands.sh

spawn sh -c {ssh root@$HOST 'bash -ls' < /tmp/commands.sh}
最终代码:

#!/bin/bash

HOSTS="host1 host2"

read -sp "Password: " PASSWORD

for HOST in $HOSTS; do
        expect -c "
                spawn sh -c {ssh root@$HOST 'bash -ls' < /tmp/commands.sh}
                expect_before {
                "*yes/no*" {send "yes"\r;exp_continue}}
                expect {
                "*assword*" {send $PASSWORD\r;interact}}
                exit"
done

关于linux - 无法通过 expect 对 bash 命令脚本进行 SSH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41858349/

相关文章:

bash - 使用 awk 重复 CSV 中未知数量的第一列

linux - 如何从文本文件追加和打印数字?

linux - 仅查看过去 24 小时的电子邮件日志

git - 将我的 MacBook 更新到 MacOs Mojave 后出现 ssh_dispatch_run_fatal git 错误

c++ - 如何在 Linux 上编译 .cpp 文件?

android - 运行系统应用程序时,sharedUserId 给出共享用户不兼容错误

android - openssl s_client -connect 默认为子域?

linux - Pig 安装错误 : ERROR pig. 主要:错误 2998:未处理的内部错误

bash - 在 solaris 中将给定时间转换为秒

linux - Amazon EC2 Linux 实例(SSH、PuTTY 等)的默认用户名是什么?