通过 ssh 运行命令时从远程机器询问问题 [英] asking to questions from remote machine while running commands over ssh

查看:30
本文介绍了通过 ssh 运行命令时从远程机器询问问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些服务器,我想在所有服务器上安装 SSL我编写了以下脚本(我应该从每个服务器运行它):

I have some servers and I want to install SSL on all of them I wrote the following script(I should run it from each servers):

#!/bin/bash
 #something to install SSL on centos
 #....
 #

注意:我应该像这样运行脚本(因为分配了 ssl 证书):

note: I should run script like this(because of assigning ssl certs):

./ssl-conf <<EOF
   > <CountryNmane>
   > <StateName>
   > <LocalityName>
   > <OrganizationName>
   > <OrganizationUnitName>
   > <CommonName> 
   > <EmailAddress>
   > <ChallengePassword>
   > <CompanyName> 
   >EOF

现在我的问题:我想写一个 bash 来处理我的服务器数量.我写了这样的东西(我应该在我的机器上运行它):

now my question: I want to write a bash to do it with numbers of my servers.I wrote something like this(I should run it on my machine):

#!/bin/bash
while read pass port user ip; do
    sshpass  -p$pass -o 'StrictHostKeyChecking no' -p $port $user@$ip <<EOF <<SSH
       US
       something
       newyork
       test
       test1
       test2
       test3
       challengepassword
       test4
    EOF #End of asking questions

    #commands to install SSL
    #commands to install SSL
    #commands to install SSL
    SSH #end of commands running on remote machine
done <<___HERE
    mypasswd  22  root  192.168.1.1
    mypasswd  22  root  192.168.1.2
    mypasswd  22  root  192.168.1.3
___HERE

这个语法对吗?

不起作用.

有什么想法吗?

先谢谢你

推荐答案

SSH 和 heredoc

你不能在同一个命令行上创建两个here-doc

但是您可以完全在命令行上编写远程 ssh 命令:

But you could write distant ssh command entirely on command line:

Script=$'while read foo ;do\n    echo $HOSTNAME $foo\ndone'
echo "$Script"

注意双引号的使用!

while read pass port user ip; do
  sshpass -p$pass ssh -o 'StrictHostKeyChecking no' -p $port $user@$ip "$Script" <<eoparams
    US
    something
    newyork
    test
    test1
    test2
    test3
    challengepassword
    test4
eoparams
done <<eodests
    mypasswd  22  root  192.168.1.1
    mypasswd  22  root  192.168.1.2
    mypasswd  22  root  192.168.1.3
eodests

这篇关于通过 ssh 运行命令时从远程机器询问问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆