1.ssh一键免密登录

首先需要按照sshpass

#!/bin/bash

ssh-keygen -f /root/.ssh/id_rsa -P ''
pass=root

for host in node1 node2 node3
do
    sshpass -p$pass ssh-copy-id -i /root/.ssh/id_rsa.pub -o StrictHostKeyChecking=no root@$host
done

2.hive一键启停脚本

#!/bin/bash

#nohup /opt/module/hive/bin/hive --service metastore &
#nohup /opt/module/hive/bin/hive --service hiveserver2 &

case $1 in
start)
    nohup /opt/module/hive/bin/hive --service metastore >/dev/null 2>&1 &
    nohup /opt/module/hive/bin/hive --service hiveserver2 >/dev/null 2>&1 &
;;
stop)
    ps -ef | grep hive | grep -v grep | awk '{print $2}'| xargs kill -9
;;
*)

;;
esac

3.查看各个虚拟机的jps进程

#!/bin/bash

for host in node1 node2 node3
do
    echo "**************$host***********************"
    ssh $host "source /etc/profile;jps"
done

4.一键启动kafka生产者、消费者还有查看topic

#!/bin/bash

topic=$2
case $1 in
consumer)
    /opt/module/kafka/bin/kafka-console-consumer.sh --bootstrap-server node1:9092 --topic $topic $3
;;
topic)
    /opt/module/kafka/bin/kafka-topics.sh --bootstrap-server node1:9092 --list
;;
producer)
    /opt/module/kafka/bin/kafka-console-producer.sh --bootstrap-server node1:9092 --topic $topic
;;
esac

5.一键启动kafka集群

#!/bin/bash

[ $# -lt 1 ] && echo "No Arguments" && exit

hosts=(node1 node2 node3)

for host in ${hosts[@]}
do
    case $1 in
    start)
        echo " --------启动 $i Kafka-------"
        ssh $host "source /etc/profile;/opt/module/kafka/bin/kafka-server-start.sh -daemon /opt/module/kafka/config/server.properties"
    ;;
    stop)
        echo " --------关闭 $i Kafka-------"
        ssh $host "source /etc/profile;/opt/module/kafka/bin/kafka-server-stop.sh"
    ;;
    *)
    exit
    ;;
    esac
done

6.一键分发目录、文件脚本rsync

需要先下载rsync

#!/bin/bash

[ $# -lt 1 ] && echo "No Arguments" && exit

hosts=(node2 node3)

for host in ${hosts[*]}
do
    for i in $@
    do
        echo $i
        realPath=`realpath $i`
        echo $realPath
        if [ ! -f  $realPath ] && [ ! -d $realPath ];then
            echo "请输入文件或文件夹"
            exit;
        fi
        pdir=`dirname $realPath`
        if [ ! -e $pdir ];then
            ssh $host "mkdir -p $pdir"
        fi
        echo "向$host分发$realPath,上一级目录是$pdir"
        rsync -av $realPath root@$host:$pdir
    done
done

7.一键启动zk脚本

#!/bin/bash

[ $# -lt 1 ] && echo "No Arguments" && exit

case $1 in
start)
    for host in node1 node2 node3
    do
        ssh $host "source /etc/profile;zkServer.sh start"
    done
;;
stop)
    for host in node1 node2 node3
    do
        ssh $host "source /etc/profile;zkServer.sh stop"
    done
;;
*)
    echo "Arguments error"
    exit;
;;
esac

结束语

希望能够帮到大家

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐