$ more /opt/auth.sh #!/bin/bash # 远程执行脚本 for IP in `cat /opt/IP.txt`;do sshpass -p ‘密码‘ ssh [email protected]$IP "sh /root/init.sh" done
注:sshpass命令需要手动安装
$ yum install sshpass -y
$ more init.sh #!/bin/bash #********************************************************************** # Description :zabbix_agentd install # version :0.3 #********************************************************************** #-------------------VAR------------------------------------------------ IP=`ip addr show | awk ‘/inet/{print $2}‘ | awk -F/ ‘{print $1}‘ | grep -Ev "127.0.0.1|::1" | head -n 1` FULL_NAME=`basename $0` FILE_PATH=`dirname $0` SERVER="172.17.60.199" ping -w 2 $SERVER -c1 &>/dev/null [ $? -eq 0 ] && M=$SERVER && ZONE="hz" GROUP=$1 ONAME=`hostname` NNAME=$GROUP-$IP-$ZONE #-------------------FUN------------------------------------------------ check(){ if [ -z "$GROUP" ];then exit 1;fi echo "export PS1=‘\[\e[1;35m\][\[\e[1;33m\]\u\[\e[1;[email protected]\e[1;36m\H \e[1;31m\A \e[1;32m\]\w\[\e[1;35m\]]\[\e[1;31m\]\n\$ \[\e[0m\]‘ " >> /etc/p rofile source /etc/profile ntpdate cn.ntp.org.cn hwclock -w } name(){ grep "se 6" /etc/redhat-release if [ $? == 0 ] then sed -i "s/^HOSTNAME=\(.*\)/HOSTNAME=$NNAME/" /etc/sysconfig/network sed -i "s/$ONAME/$NNAME/g" /etc/hosts hostname "$NNAME" fi grep "se 7" /etc/redhat-release if [ $? == 0 ] then hostnamectl --static set-hostname $NNAME fi } agent(){ [ -d /soft ] || mkdir -m 755 /soft grep "zabbix" /etc/passwd if [ $? -gt 0 ];then groupadd zabbix && useradd -g zabbix -s /sbin/nologin zabbix fi yum install gcc -y if [ $? == 0 ];then tar xf zabbix-3.0.4.tar.gz sleep 5 cd zabbix-3.0.4 ./configure --prefix=/soft/zabbix --enable-agent --with-net-snmp if [ $? == 0 ];then make && make install fi fi } conf(){ sed ‘s/\/usr\/local/\/soft\/zabbix/g‘ /root/zabbix-3.0.4/misc/init.d/fedora/core/zabbix_agentd > /etc/rc.d/init.d/zabbix_agentd cat << -EOF- > /soft/zabbix/etc/zabbix_agentd.conf LogFile=/tmp/zabbix_agentd.log EnableRemoteCommands=1 Server=172.17.60.199 ServerActive=172.17.60.199 HostMetadataItem=system.uname Include=/soft/zabbix/etc/zabbix_agentd.conf.d/*.conf -EOF- echo "Hostname=$NNAME" >> /soft/zabbix/etc/zabbix_agentd.conf chmod +x /soft/zabbix/etc/zabbix_agentd.conf chmod +x /etc/rc.d/init.d/zabbix_agentd } root(){ [ -d /root/.ssh ] || mkdir -m 700 /root/.ssh chattr -i /root/.ssh/authorized_keys cat << -EOF- > /root/.ssh/authorized_keys ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAzJn4AOFna4BwN3ALyCikbaokKhVvXkaia0gpXctOejXj5+UwAERvncYX1NQeHYYv16eDJa8HdtJsJrFxA5BEGPD14VVVvYF3T6wP/2cV0tro3S 1SgfhyZSDWdxHWx++gzeIDqYCcnGBYDloJEQJ0c755++uS2ZL5rdhBc4p1AVL9aRB3wYOYFQJ8IdNSWh+dcXcVIiqKfvkUFqdvxsExkD1EJxvceeCGIXxNjbRCqQtKn3JmDvr9dlIWmHtT5XVu +6RjPDn6No6GIWmaOSulswuy1dY33KvJgn9qGSmZXBloHX2LYnH+BgOzwT4Aej6s48lLmkZwG0HvoYmgp5NWFQ== [email protected] -EOF- chmod 600 /root/.ssh/authorized_keys chattr +i /root/.ssh/authorized_keys } rc(){ service zabbix_agentd start } main(){ check clean name agent conf root rc rm -f $FILE_PATH/$FULL_NAME } #-------------------PROGRAM-------------------------------------------- main &
时间: 2024-10-11 13:18:22