您的位置:首页 > 运维架构 > Shell

ssh远程到目标机器执行命令shell脚本

2017-06-30 09:50 816 查看
  linux下ssh到目标机器首先需要配置免密码登陆,才能执行如下脚本

  hostnames服务器列表文件

master.hadoop
w1.hadoop
w2.hadoop
w3.hadoop
w4.hadoop 运行shell脚本
#!/bin/bash

function usage() {
echo "Wrong arguments input..."
echo "Usage: `basename $0` topicName handler[lookup|kill]" >&2
echo " eg: `basename $0` service_log lookup"
exit 1
}

topic_array=("service_log" "service_log_nh" "service_log_agent" "service_log_flow" \
"service_log_rec" "search_log" "user_trace" "user_trace_internal")
handler_array=("lookup" "kill")

if [ $# -lt 2 ]; then
usage
fi

topic=$1
handler=$2

## flume agent处理方式判断
if echo "${handler_array[@]}" | grep -w "$handler" &>/dev/null; then
echo "Fount process handler: $handler"
else
echo "Not found process handler: $handler ...."
usage
fi

## 判断是否为需要处理的flume agent名称
if echo "${topic_array[@]}" | grep -w "$topic" &>/dev/null; then
echo "Fount flume agent name: $topic"
else
echo "Not found flume agent name: $topic ...."
usage
fi

## 根据handler名称处理flume agent
hosts='hostnames' # hostname列表文件

while read line
do
hostname=$line
echo "================ $hostname ============="
case $handler in
lookup)
ssh root@$hostname -nq "ps -ef | grep flume | grep -w $topic | grep -v grep | wc -l"
;;
kill)
ssh root@$hostname -nq "ps -ef | grep flume | grep -w $topic |awk '{print $2}' |xargs kill -15"
;;
*)
usage
;;
esac
done < $hosts

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: