linux - 在多个主机上复制、安装和执行的脚本

标签 linux bash shell unix

我正在尝试将几个文件复制到多个主机,根据操作系统类型在每个主机上安装/配置这些文件并运行特定命令。每个主机的 IP 地址都是从 host.txt 文件中读取的。

当我运行脚本时出现,它不会在远程主机上执行。有人可以帮助确定此脚本的问题吗?很抱歉这个基本的,我是 shell 脚本的新手。

#!/bin/bash


export AGENT=agent-x86-64-linux-5.8.1.tar.gz
export AGENT_PROPERTIES_NONDMZ=agent.properties.nondmz
export agent_INIT=agent.sh

echo "####Installing hqagent####"

while read host; do
  scp $AGENT $AGENT_PROPERTIES_NONDMZ $agent_INIT root@$host:/opt
  if ssh -n root@$host '[ "$(awk "/CentOS/{print}" /etc/*release)" ] '
  then
    cd /opt
    tar -xvzf $AGENT
    mv -f /opt/agent.properties.nondmz /opt/agent-5.8.1/conf/agent.properties
    mkdir /opt/hqagent/
    ln -s /opt/agent-5.8.1/ /opt/hqagent/agent-current
    useradd hqagent
    groupadd hqagent
    chown -R hqagent:hqagent /opt/hqagent /opt/agent-5.8.1/
    cd /etc/init.d
    chmod 755 hqagent.sh
    chkconfig --add hqagent.sh
    su - hqagent
    /opt/agent-5.8.1/bin/hq-agent.sh start
  else
    cd /opt
    tar -xvzf $AGENT
    mv -f /opt/agent.properties.nondmz /opt/agent-5.8.1/conf/agent.properties
    rm -rf /opt/hqagent.sh
    mkdir /opt/hqagent/
    ln -s /opt/agent-5.8.1/ /opt/hqagent/agent-current
    useradd hqagent
    groupadd hqagent
    chown -R hqagent:hqagent /opt/hqagent /opt/agent-5.8.1
    cd /etc/init.d
    ln -s /opt/hqagent/agent-current/bin/hq-agent.sh hqagent.sh
    cd /etc/init.d/rc3.d/
    ln -s /etc/init.d/hqagent.sh S99hqagent
    ln -s /etc/init.d/hqagent.sh K01hqagent
    cd ../rc5.d
    ln -s /etc/init.d/hqagent.sh S99hqagent
    ln -s /etc/init.d/hqagent.sh K01hqagent
    chkconfig --add hqagent.sh
    su - hqagent
    /opt/agent-5.8.1/bin/hq-agent.sh start
  fi
done < hosts.txt

错误:

tar (child): agent-x86-64-linux-5.8.1.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
mv: cannot stat `/opt/agent.properties.nondmz': No such file or directory
mkdir: cannot create directory `/opt/hqagent/': File exists
ln: creating symbolic link `/opt/hqagent/agent-current': File exists
useradd: user 'hqagent' already exists
groupadd: group 'hqagent' already exists
chown: cannot access `/opt/agent-5.8.1/': No such file or directory
chmod: cannot access `hqagent.sh': No such file or directory
error reading information on service hqagent.sh: No such file or directory
-bash: line 1: 10.145.34.6: command not found
-bash: line 2: 10.145.6.10: command not found
./hq-install.sh: line 29: /opt/agent-5.8.1/bin/hq-agent.sh: No such file or directory

最佳答案

看来问题是您在“主”服务器上运行此脚本,但不知何故期望您的 if 语句的分支在远程主机上运行。您需要将这些分支分解成它们自己的文件,将它们与其他文件一起复制到远程主机,并且在您的 if 语句中,每个分支应该只是对远程主机的 ssh 命令,触发您复制的脚本.

所以你的主脚本看起来像这样:

#!/bin/bash
export AGENT=agent-x86-64-linux-5.8.1.tar.gz
export AGENT_PROPERTIES_NONDMZ=agent.properties.nondmz
export agent_INIT=agent.sh

# Scripts containing the stuff you want done on the remote hosts
centos_setup=centos_setup.sh
other_setup=other_setup.sh

echo "####Installing hqagent####"

while read host; do
  echo "  ++ Copying files to $host"
  scp $AGENT $AGENT_PROPERTIES_NONDMZ $agent_INIT root@$host:/opt

  echo -n "  ++ Running remote part on $host "
  if ssh -n root@$host '[ "$(awk "/CentOS/{print}" /etc/*release)" ] '
  then
    echo "(centos)"
    scp $centos_setup root@$host:/opt
    ssh root@host "/opt/$centos_setup"
  else
    echo "(generic)"
    scp $other_setup root@$host:/opt
    ssh root@host "/opt/$other_setup"
  fi
done < hosts.txt

这两个辅助脚本的内容将是您原始文件中 if 分支的当前内容。

关于linux - 在多个主机上复制、安装和执行的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22501863/

相关文章:

ruby - 基本 ruby​​ 脚本未按预期工作

linux - 如何在特定编号的目录中查找具有特定模式的文件? Linux

linux - 用户进程如何访问我的模块加载的字符设备

linux - 加载共享库时出错,没有这样的文件或目录

linux - 在 Linux 中打印特定文本行的单个部分

php - 将 Bash 与 PHP 集成

linux - 在进程替换中的命令失败后,bash pipefail 仍在运行 w/set -e

javascript - 从 MongoDB 对象数组中删除元素

bash - 匹配字符串中几个可能的字符之一

Shell 脚本 - 处理数组中的 float 数学运算,而不先将值放入 var 中?