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

来源:互联网 发布:vmware文件共享 ubuntu 编辑:程序博客网 时间:2024/05/22 02:14

  linux下ssh到目标机器首先需要配置免密码登陆,才能执行如下脚本

  hostnames服务器列表文件

master.hadoopw1.hadoopw2.hadoopw3.hadoopw4.hadoop
 运行shell脚本

#!/bin/bashfunction 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   usagefitopic=$1handler=$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 ...."   usagefi## 判断是否为需要处理的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 ...."   usagefi## 根据handler名称处理flume agenthosts='hostnames'  # hostname列表文件while read linedo  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       ;;  esacdone  < $hosts