用于在多个主机上复制,安装和执行的脚本

时间:2022-06-03 04:49:44

I am trying to copy few files into multiple hosts, install/configure those on each with running specific commands depending on OS type. The IP addresses for each host are read from host.txt file.

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

It appears when I run the script, it does not execute on the remote hosts. Can someone help identify the issues with this script? Sorry for this basic one, I am quite new into shell scripting.

它出现在我运行脚本时,它不会在远程主机上执行。有人可以帮助识别此脚本的问题吗?对于这个基本的,我很擅长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

error:

错误:

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

1 个解决方案

#1


1  

It appears that the problem is that you run this script on the "master" server, but somehow expect the branches of your if-statement to be run on the remote hosts. You need to factor those branches out into their own files, copy them to the remote hosts along with the other files, and in your if-statement, each branch should just be a ssh command to the remote host, triggering the script you copied over.

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

So your master script would look something like:

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

#!/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

The contents of the two auxiliary scrips would be the current contents of the if-branches in your original.

两个辅助描述的内容将是原始if分支的当前内容。

#1


1  

It appears that the problem is that you run this script on the "master" server, but somehow expect the branches of your if-statement to be run on the remote hosts. You need to factor those branches out into their own files, copy them to the remote hosts along with the other files, and in your if-statement, each branch should just be a ssh command to the remote host, triggering the script you copied over.

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

So your master script would look something like:

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

#!/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

The contents of the two auxiliary scrips would be the current contents of the if-branches in your original.

两个辅助描述的内容将是原始if分支的当前内容。