Linux 用于在多台主机上复制、安装和执行的脚本
我试图将几个文件复制到多个主机中,根据操作系统类型,在每个主机上安装/配置这些文件,并运行特定命令。从host.txt文件读取每个主机的IP地址 当我运行脚本时,它不会在远程主机上执行。是否有人可以帮助识别此脚本的问题?很抱歉,这是一个基本的问题,我对shell脚本非常陌生Linux 用于在多台主机上复制、安装和执行的脚本,linux,bash,shell,unix,Linux,Bash,Shell,Unix,我试图将几个文件复制到多个主机中,根据操作系统类型,在每个主机上安装/配置这些文件,并运行特定命令。从host.txt文件读取每个主机的IP地址 当我运行脚本时,它不会在远程主机上执行。是否有人可以帮助识别此脚本的问题?很抱歉,这是一个基本的问题,我对shell脚本非常陌生 #!/bin/bash export AGENT=agent-x86-64-linux-5.8.1.tar.gz export AGENT_PROPERTIES_NONDMZ=agent.properties.nondm
#!/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
问题似乎是您在“主”服务器上运行此脚本,但不知何故,您希望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
#/bin/bash
导出代理=AGENT-x86-64-linux-5.8.1.tar.gz
导出代理\u属性\u NONDMZ=AGENT.PROPERTIES.NONDMZ
导出代理_INIT=agent.sh
#包含要在远程主机上执行的操作的脚本
centos_setup=centos_setup.sh
其他设置=其他设置.sh
echo“##########安装hqagent###”
读主机时;做
echo“++将文件复制到$host”
scp$AGENT$AGENT\u PROPERTIES\u NONDMZ$AGENT\u INIT root@$host:/opt
echo-n“++在$host上运行远程部件”
如果ssh-n root@$host'[“$(awk)/CentOS/{print}”/etc/*release]”
然后
echo“(centos)”
scp$centos_安装根@$host:/opt
sshroot@host“/opt/$centos_设置”
其他的
回声”(通用)
scp$other_安装根@$host:/opt
sshroot@host“/opt/$other\u设置”
fi
完成
两个辅助脚本的内容将是原始脚本中if分支的当前内容。请给出一个简单的示例,说明如何运行脚本,并指定您认为结果有什么问题。另外,适当地缩进代码…这对眼睛来说真的很难。
无法打开:没有这样的文件或目录。你检查过文件是否在办公室吗?
#!/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