自动化 SSH 和 运行 命令

Automate SSH and running commands

我正在尝试将 SSH 自动化到一台机器中,然后 运行 一些命令。但是,我卡在了 SSH 部分:

for h in ${hosts[*]}; do
        ssh -i foo.pem bc2-user@$h
        echo "here"
        sudo bash
        cd /data/kafka/tmp/kafka-logs/
        exit 
        exit
done

一旦我 运行 这个脚本,我就可以通过 SSH 登录,但是自动化会在这条消息处停止:

********************************************************************************
This is a private computer system containing information that is proprietary
and confidential to the owner of the system.  Only individuals or entities
authorized by the owner of the system are allowed to access or use the system.
Any unauthorized access or use of the system or information is strictly
prohibited.

All violators will be prosecuted to the fullest extent permitted by law.
********************************************************************************
Last login: Thu Dec 10 10:19:23 2015 from 10.81.120.55
-bash: ulimit: open files: cannot modify limit: Operation not permitted

当我按下 control + d 时,我的 echo "here" 命令执行,然后我的脚本退出。不执行其余命令。

我四处阅读并尝试了这个,但我遇到了这个语法错误:

./kafka_prefill_count.sh: line 38: warning: here-document at line 26 delimited by end-of-file (wanted `EOF')
./kafka_prefill_count.sh: line 39: syntax error: unexpected end of file

脚本:

for h in ${hosts[*]}; do
        ssh -i foo.pem bc2-user@$h << EOF
                echo "here"
                sudo bash
                cd /data/kafka/tmp/kafka-logs/
                ls | grep "$dir_name"
                exit
                exit
                bash -l

        EOF
done

您当前的脚本并不是您使用 ssh 远程执行命令的真正方式。

看起来应该更像这样

shh -i foo.pem user@host 'echo "here" ; hostname'

(主机名命令只是证明它在其他机器上的运行的例子。

Good resource

刚刚看到您的编辑:

EOF 需要一直向左。

  ssh -i foo.pem bc2-user@$h << EOF
            echo "here"
            sudo bash
            cd /data/kafka/tmp/kafka-logs/
            ls | grep "$dir_name"
            exit
            exit
            bash -l

EOF

尝试使用两个 here documents,一个用于 ssh,一个用于 bash:

ssh -i foo.pem bc2-user@$h << EOF1
  echo "remote"
  sudo bash << EOF2
    cd /data/kafka/tmp/kafka-logs/
    ls | grep "$dir_name"
EOF2
EOF1