How to use heredoc to answers prompt questions

ε祈祈猫儿з 提交于 2019-12-13 23:03:36

问题


I am trying to automate the installation of Icinga2 on many remote clients. The PKI token will get generated on the Icinga server and it's different for each client - Then it should be sent to each client. As part of the client installation, icinga2 node wizard would run and I would like to pipe a series of inputs to a prompt as below. Can you please check to see if I use the heredoc correctly?

#!/bin/bash

while read f; do
   ssh-copy-id myusername"$f"
   ssh myusername@"$f" '
        yum install -y epel-release
        wget --no-check-certificate https://packages.icinga.org/epel/7/release/noarch/icinga-rpm-release-7-1.el7.centos.noarch.rpm
        yum install icinga-rpm-release-7-1.el7.centos.noarch.rpm
        yum install -y icinga2  nagios-plugins-all
        chown -R icinga:icinga /etc/icinga2  /var/lib/icinga2 /var/log/icinga2' </dev/null

   ssh myusername@master.icinga.test.com icinga2 pki ticket --cn "$f" |
   ssh myusername@"$f" 'cat >/tmp/pkicode'

   PKI= echo $/tmp/pkicode
   icinga2 node wizard << EOF
   Y
   Enter
   master.icinga.test.com
   Y
   10.20.20.1
   N
   Y
   $PKI
   Enter
   Enter
   Y
   Y
   Enter
   Enter
   N
   N
   EOF
   scp ./zones.conf myusername@"$f":/etc/icinga2/zones.conf
done < linux-list.txt

Thank you


回答1:


You have a few errors in the code.

#!/bin/bash

while read f; do
   ssh-copy-id myusername"$f"
   ssh myusername@"$f" '
     :
    ' </dev/null

   ssh myusername@master.icinga.test.com icinga2 pki ticket --cn "$f" </dev/null |
   ssh myusername@"$f" '
      PKI=$(cat)
      icinga2 node wizard <<________EOF
Y

master.icinga.test.com
Y
10.20.20.1
N
Y
$PKI


Y
Y


N
N
________EOF
        '
   scp ./zones.conf myusername@"$f":/etc/icinga2/zones.conf
done < linux-list.txt

You'll notice in particular how the last ssh needs to have all the code which should run there inside the single quotes. It receives the ticket on its standard input, and uses $(cat) to directly capture it in a variable. Also, the empty lines in the here document produce an etrer keystroke with nothing else before it in the input to icinga2.

As already suggested in previous questions of yours, you should probably be using icinga2 node setup instead of node wizard in scripts.



来源:https://stackoverflow.com/questions/51564830/how-to-use-heredoc-to-answers-prompt-questions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!