转载于运维笔记

Categories:Shell

也许很多人认为shell不能并发任务,其实可通过其它一些方式来实现。下面的脚本是我批量快速管理500+服务器脚本,阅读该脚本前建议先看《自动执行远程主机命令expect脚本》、《自动远程拷贝expect脚本》和《getopt:命令行选项、参数处理

用法:

Usage: ./multi_main.sh [-h|--help]
[-v|-V|--version]
[-l|--iplist ... ]
[-c|--config ... ]
[-t|--sshtimeout ... ]
[-T|--fttimeout ... ]
[-L|--bwlimit ... ]
[-n|--ignore]

cat config.txt #上传文件和执行命令

file:::~/scripts/test.sh /root/ push
com:::./test.sh

cat iplist.txt  #ip列表

# Usage:
#ip port user password [password_2] [password_3] [password_4]
# Example:
#192.168.0.100 22 root 123456
192.168.0.200 22 root 123456
192.168.0.201 22 root 123456
...
./multi_main.sh -c config.txt -l iplist.txt #开始执行,可查看result目录下的日志来分析是否执行成功

脚本如下:

  1. mssh.exp 执行远程服务器命令expect脚本
  2. mscp.exp 向远程服务器上传或下载文件expect脚本(rsync)
  3. thread.sh 向一台服务器发起动作
  4. ckssh.py 检查ssh是否通
  5. multi_main.sh 批量执行,对每台调用thread.sh

mssh.exp:

#!/usr/bin/expect --

if { [llength $argv] < 4 } {
puts "Usage: $argv0 ip user passwd port commands timeout"
exit 1
} match_max 600000 set ipcode [lindex $argv 0]
set ip [exec dc -e $ipcode]
set user [lindex $argv 1]
set passwdcode [lindex $argv 2]
set passwd [exec dc -e $passwdcode]
set portcode [lindex $argv 3]
set port [exec dc -e $portcode]
set commands [lindex $argv 4]
set timeoutflag [lindex $argv 5] set yesnoflag 0
set timeout $timeoutflag for {} {1} {} {
# for is only used to retry when "Interrupted system call" occured spawn /usr/bin/ssh -o GSSAPIAuthentication=no -q -l$user -p$port $ip expect { "assword:" {
send "$passwd\r"
break;
} "yes/no)?" {
set yesnoflag 1
send "yes\r"
break;
} "FATAL" {
puts "\nCONNECTERROR: $ip occur FATAL ERROR!!!\n"
exit 1
} timeout {
puts "\nCONNECTERROR: $ip Logon timeout!!!\n"
exit 1
} "No route to host" {
puts "\nCONNECTERROR: $ip No route to host!!!\n"
exit 1
} "Connection Refused" {
puts "\nCONNECTERROR: $ip Connection Refused!!!\n"
exit 1
} "Connection refused" {
puts "\nCONNECTERROR: $ip Connection Refused!!!\n"
exit 1
} "Host key verification failed" {
puts "\nCONNECTERROR: $ip Host key verification failed!!!\n"
exit 1
} "Illegal host key" {
puts "\nCONNECTERROR: $ip Illegal host key!!!\n"
exit 1
} "Connection Timed Out" {
puts "\nCONNECTERROR: $ip Logon timeout!!!\n"
exit 1
} "Interrupted system call" {
puts "\n$ip Interrupted system call!!!\n"
}
}
} if { $yesnoflag == 1 } {
expect {
"assword:" {
send "$passwd\r"
} "yes/no)?" {
set yesnoflag 2
send "yes\r"
}
}
} if { $yesnoflag == 2 } {
expect {
"assword:" {
send "$passwd\r"
}
}
} expect {
"]" {send "$commands \r"}
"assword:" {
send "$passwd\r"
puts "\nPASSWORDERROR: $ip Password error!!!\n"
exit 1
}
} expect {
"]" {send "sleep 1 \r"}
} expect {
"]" {send "exit\r"}
} expect eof {
puts "OK_SSH: $ip\n"
exit 0;
}

mscp.exp:

#!/usr/bin/expect --

proc Usage_Exit {self} {
puts ""
puts "Usage: $self ip user passwd port sourcefile destdir direction bwlimit timeout"
puts ""
puts " sourcefile: a file or directory to be transferred"
puts " 需要拷贝目录时目录名后不要带 /, 否则会拷贝该目录下的所有文件"
puts " destdir: the location that the sourcefile to be put into"
puts " direction: pull or push"
puts " pull: remote -> local"
puts " push: local -> remote"
puts " bwlimit: bandwidth limit, kbit/s, 0 means no limit"
puts " timeout: timeout of expect, s, -1 means no timeout"
puts ""
exit 1
} if { [llength $argv] < 9 } {
Usage_Exit $argv0
} set ipcode [lindex $argv 0]
set ip [exec dc -e $ipcode]
set user [lindex $argv 1]
set passwduncode [lindex $argv 2]
set passwd [exec dc -e $passwduncode]
set portcode [lindex $argv 3]
set port [exec dc -e $portcode]
set sourcefile [lindex $argv 4]
set destdir [lindex $argv 5]
set direction [lindex $argv 6]
set bwlimit [lindex $argv 7]
set timeoutflag [lindex $argv 8] set yesnoflag 0
set timeout $timeoutflag for {} {1} {} {
# for is only used to retry when "Interrupted system call" occured if { $direction == "pull" } { if { $bwlimit > 0 } {
spawn rsync -crazP --bwlimit=$bwlimit -e "/usr/bin/ssh -o GSSAPIAuthentication=no -q -l$user -p$port" $ip:$sourcefile $destdir
} elseif { $bwlimit == 0 } {
spawn rsync -crazP -e "/usr/bin/ssh -o GSSAPIAuthentication=no -q -l$user -p$port" $ip:$sourcefile $destdir
} else {
Usage_Exit $argv0
} } elseif { $direction == "push" } { if { $bwlimit > 0 } {
spawn rsync -crazP --bwlimit=$bwlimit -e "/usr/bin/ssh -o GSSAPIAuthentication=no -q -l$user -p$port" $sourcefile $ip:$destdir
} elseif { $bwlimit == 0 } {
spawn rsync -crazP -e "/usr/bin/ssh -o GSSAPIAuthentication=no -q -l$user -p$port" $sourcefile $ip:$destdir
} else {
Usage_Exit $argv0
} } else {
Usage_Exit $argv0
} expect { "assword:" {
send "$passwd\r"
break;
} "yes/no)?" {
set yesnoflag 1
send "yes\r"
break;
} "FATAL" {
puts "\nCONNECTERROR: $ip occur FATAL ERROR!!!\n"
exit 1
} timeout {
puts "\nCONNECTERROR: $ip Logon timeout!!!\n"
exit 1
} "No route to host" {
puts "\nCONNECTERROR: $ip No route to host!!!\n"
exit 1
} "Connection Refused" {
puts "\nCONNECTERROR: $ip Connection Refused!!!\n"
exit 1
} "Connection refused" {
puts "\nCONNECTERROR: $ip Connection Refused!!!\n"
exit 1
} "Host key verification failed" {
puts "\nCONNECTERROR: $ip Host key verification failed!!!\n"
exit 1
} "Illegal host key" {
puts "\nCONNECTERROR: $ip Illegal host key!!!\n"
exit 1
} "Connection Timed Out" {
puts "\nCONNECTERROR: $ip Logon timeout!!!\n"
exit 1
} "Interrupted system call" {
puts "\n$ip Interrupted system call!!!\n"
}
} } if { $yesnoflag == 1 } {
expect {
"assword:" {
send "$passwd\r"
} "yes/no)?" {
set yesnoflag 2
send "yes\r"
}
}
} if { $yesnoflag == 2 } {
expect {
"assword:" {
send "$passwd\r"
}
}
} expect {
"assword:" {
send "$passwd\r"
puts "\nPASSWORDERROR: $ip Password error!!!\n"
exit 1
} eof {
puts "OK_SCP: $ip\n"
exit 0;
}
}

thread.sh:

#!/bin/bash

# Default Parameters
myIFS=":::" # 配置文件中的分隔符
TOOLDIR=~/scripts
cd $TOOLDIR #BEGINDATETIME=`date "+%F %T"` IP=$1P
PORT=$2P
USER=$3
PASSWD=$4P
CONFIG_FILE=$5 # 命令列表和文件传送配置列表,关键字为com:::和file:::
SSHTIMEOUT=$6 # 远程命令执行相关操作的超时设定,单位为秒
SCPTIMEOUT=$7 # 文件传送相关操作的超时设定,单位为秒
BWLIMIT=$8 # 文件传送的带宽限速,单位为kbit/s # 针对一个$IP,执行配置文件中的一整套操作
while read eachline
do
# 必须以com或file开头
[ -z "`echo $eachline | grep -E '^com|^file'`" ] && continue myKEYWORD=`echo $eachline | awk -F"$myIFS" '{ print $1 }'`
myCONFIGLINE=`echo $eachline | awk -F"$myIFS" '{ print $2 }'` # 配置文件中有关键字file:::,就调用mscp.exp进行文件传送
if [ "$myKEYWORD"x == "file"x ]; then
SOURCEFILE=`echo $myCONFIGLINE | awk '{ print $1 }'`
DESTDIR=`echo $myCONFIGLINE | awk '{ print $2 }'`
DIRECTION=`echo $myCONFIGLINE | awk '{ print $3 }'`
$TOOLDIR/mscp.exp $IP $USER $PASSWD $PORT $SOURCEFILE $DESTDIR $DIRECTION $BWLIMIT $SCPTIMEOUT [ $? -ne 0 ] && echo -e "\033[31mSCP Try Out All Password Failed\033[0m\n" # 配置文件中有关键字com:::,就调用mssh.exp进行远程命令执行
elif [ "$myKEYWORD"x == "com"x ]; then
$TOOLDIR/mssh.exp $IP $USER $PASSWD $PORT "${myCONFIGLINE}" $SSHTIMEOUT
#echo $IP $USER $PASSWD $PORT "${myCONFIGLINE}" $SSHTIMEOUT
[ $? -ne 0 ] && echo -e "\033[31mSSH Try Out All Password Failed\033[0m\n" else
echo "ERROR: configuration wrong! [$eachline] "
echo " where KEYWORD should not be [$myKEYWORD], but 'com' or 'file'"
echo " if you dont want to run it, you can comment it with '#'"
echo ""
exit
fi done < $CONFIG_FILE #ENDDATETIME=`date "+%F %T"` #echo "$BEGINDATETIME -- $ENDDATETIME"
#echo "$0 $* --excutes over!" exit 0

ckssh.py:

#!/usr/bin/python
import socket,sys
sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sk.settimeout(1)
try:
sk.connect((sys.argv[1],int(sys.argv[2])))
print 'ok'
except Exception:
print 'no'
sk.close()

multi_main.sh:

#!/bin/bash
#Blog: blog.linuxeye.com ###################### proc defination ########################
# ignore rule
ignore_init()
{
# ignore password
array_ignore_pwd_length=0
if [ -f ./ignore_pwd ]; then
while read IGNORE_PWD
do
array_ignore_pwd[$array_ignore_pwd_length]=$IGNORE_PWD
let array_ignore_pwd_length=$array_ignore_pwd_length+1
done < ./ignore_pwd
fi # ignore ip address
array_ignore_ip_length=0
if [ -f ./ignore_ip ]; then
while read IGNORE_IP
do
array_ignore_ip[$array_ignore_ip_length]=$IGNORE_IP
let array_ignore_ip_length=$array_ignore_ip_length+1
done < ./ignore_ip
fi
} show_version()
{
echo "version: 1.0"
echo "updated date: 2014-05-28"
} show_usage()
{
echo -e "`printf %-16s "Usage: $0"` [-h|--help]"
echo -e "`printf %-16s ` [-v|-V|--version]"
echo -e "`printf %-16s ` [-l|--iplist ... ]"
echo -e "`printf %-16s ` [-c|--config ... ]"
echo -e "`printf %-16s ` [-t|--sshtimeout ... ]"
echo -e "`printf %-16s ` [-T|--fttimeout ... ]"
echo -e "`printf %-16s ` [-L|--bwlimit ... ]"
echo -e "`printf %-16s ` [-n|--ignore]"
#echo "ignr_flag: 'ignr'-some ip will be ignored; otherwise-all ip will be handled"
} TOOLDIR=~/scripts
cd $TOOLDIR IPLIST="iplist.txt" # IP列表,格式为IP 端口 用户名 密码
CONFIG_FILE="config.txt" # 命令列表和文件传送配置列表,关键字为com:::和file:::
IGNRFLAG="noignr" # 如果置为ignr,则脚本会进行忽略条件的判断
SSHTIMEOUT=100 # 远程命令执行相关操作的超时设定,单位为秒
SCPTIMEOUT=2000 # 文件传送相关操作的超时设定,单位为秒
BWLIMIT=1024000 # 文件传送的带宽限速,单位为kbit/s
[ ! -d "result" ] && mkdir result # 入口参数分析
TEMP=`getopt -o hvVl:c:t:T:L:n --long help,version,iplist:,config:,sshtimeout:,fttimeout:,bwlimit:,ignore -- "$@" 2>/dev/null` [ $? != 0 ] && echo -e "\033[31mERROR: unknown argument! \033[0m\n" && show_usage && exit 1 # 会将符合getopt参数规则的参数摆在前面,其他摆在后面,并在最后面添加--
eval set -- "$TEMP" while :
do
[ -z "$1" ] && break;
case "$1" in
-h|--help)
show_usage; exit 0
;;
-v|-V|--version)
show_version; exit 0
;;
-l|--iplist)
IPLIST=$2; shift 2
;;
-c|--config)
CONFIG_FILE=$2; shift 2
;;
-t|--sshtimeout)
SSHTIMEOUT=$2; shift 2
;;
-T|--fttimeout)
SCPTIMEOUT=$2; shift 2
;;
-L|--bwlimit)
BWLIMIT=$2; shift 2
;;
-n|--ignore)
IGNRFLAG="ignr"; shift
;;
--)
shift
;;
*)
echo -e "\033[31mERROR: unknown argument! \033[0m\n" && show_usage && exit 1
;;
esac
done ################ main #######################
BEGINDATETIME=`date "+%F %T"`
[ ! -f $IPLIST ] && echo -e "\033[31mERROR: iplist \"$IPLIST\" not exists, please check! \033[0m\n" && exit 1 [ ! -f $CONFIG_FILE ] && echo -e "\033[31mERROR: config \"$CONFIG_FILE\" not exists, please check! \033[0m\n" && exit 1 IP_count=$(egrep -v '^#|^$' $IPLIST|wc -l)
IP_init=1
while [[ $IP_init -le $IP_count ]]
do
egrep -v '^#|^$' $IPLIST | sed -n "$IP_init,$(expr $IP_init + 50)p" > $IPLIST.tmp #并发50 IPSEQ=0 while read IP PORT USER PASSWD PASSWD_2ND PASSWD_3RD PASSWD_4TH OTHERS
# while read Line
do
[ -z "`echo $IP | grep -E '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|CNS'`" ] && continue
if [ "`python $TOOLDIR/ckssh.py $IP $PORT`" == 'no' ];then
[ ! -e ipnologin.txt ] && > ipnologin.txt
[ -z "`grep $IP ipnologin.txt | grep $(date +%F)`" ] && echo "`date +%F_%H%M` $IP" >> ipnologin.txt
continue
fi let IPSEQ=$IPSEQ+1 # 如果启用了忽略,则进入忽略流程
if [ $IGNRFLAG == "ignr" ]; then
ignore_init
ignored_flag=0 i=0
while [ $i -lt $array_ignore_pwd_length ]
do
[ ${PASSWD}x == ${array_ignore_pwd[$i]}x ] && ignored_flag=1 && break
let i=$i+1
done [ $ignored_flag -eq 1 ] && continue j=0
while [ $j -lt $array_ignore_ip_length ]
do
[ ${IP}x == ${array_ignore_ip[$j]}x ] && ignored_flag=1 && break
let j=$j+1
done [ $ignored_flag -eq 1 ] && continue
fi ####### Try password from here ####
#for PW in $PASSWD $PASSWD_2ND $PASSWD_3RD $PASSWD_4TH
#do
# PASSWD_USE=$PW
# $TOOLDIR/ssh.exp $IP $USER $PW $PORT true $SSHTIMEOUT
# [ $? -eq 0 ] && PASSWD_USE=$PW && break
#done
PASSWD_USE=$PASSWD IPcode=$(echo "ibase=16;$(echo "$IP" | xxd -ps -u)"|bc|tr -d '\\'|tr -d '\n')
Portcode=$(echo "ibase=16;$(echo "$PORT" | xxd -ps -u)"|bc|tr -d '\\'|tr -d '\n')
#USER=$USER
PWcode=$(echo "ibase=16;$(echo "$PASSWD_USE" | xxd -ps -u)"|bc|tr -d '\\'|tr -d '\n')
Othercode=$(echo "ibase=16;$(echo "$OTHERS" | xxd -ps -u)"|bc|tr -d '\\'|tr -d '\n')
#echo $IPcode $Portcode $USER $PWcode $CONFIG_FILE $SSHTIMEOUT $SCPTIMEOUT $BWLIMIT $Othercode
./thread.sh $IPcode $Portcode $USER $PWcode $CONFIG_FILE $SSHTIMEOUT $SCPTIMEOUT $BWLIMIT $Othercode | tee result/$IP.log &
done < $IPLIST.tmp
sleep 3
IP_init=$(expr $IP_init + 50)
done ENDDATETIME=`date "+%F %T"` echo "$BEGINDATETIME -- $ENDDATETIME"
echo "$0 $* --excutes over!" exit 0

并发批量管理500台以上服务器脚本分享(shell版)的更多相关文章

  1. phpMyAdmin搭建及管理多台数据库服务器

    phpMyAdmin搭建及管理多台数据库服务器 环境说明: 系统版本    CentOS 6.9 x86_64         软件版本    nginx-1.12.2        php-5.5. ...

  2. puppet批量管理500多台服务器

    前言 puppet使用了有一段时间了,之前写的手顺书一直未发布到blog上来,今天正好有空,写下一点笔记.公司在用的服务器有500多台,基本都为CentOS,版本有5和6两种,管理起来很不方便,尤其是 ...

  3. CentOS7Linux中自动化运维工具Ansible的安装,以及通过模块批量管理多台主机

    使用自动化运维工具Ansible集中化管理服务器 Ansible概述 Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具.它用Python写成,类似于saltstack和Puppet ...

  4. 批量实现多台服务器之间ssh无密码登录的相互信任关系

    最近IDC上架了一批hadoop大数据业务服务器,由于集群环境需要在这些服务器之间实现ssh无密码登录的相互信任关系.具体的实现思路:在其中的任一台服务器上通过"ssh-keygen -t ...

  5. Linux系统——Ansible批量管理工具

    批量管理工具: (1)ansible 操作简单(适用于500台以下服务器) (2)saltstack 比较复杂(一般适用于1000-4w台服务器) (3)puppet超级复杂 systemctl(统一 ...

  6. Fabric 源码学习:如何实现批量管理远程服务器?

    前不久,我写了一篇<Fabric教程>,简单来说,它是一个用 Python 开发的轻量级的远程系统管理工具,在远程登录服务器.执行 Shell 命令.批量管理服务器.远程部署等场景中,十分 ...

  7. windows下运行的linux服务器批量管理工具(带UI界面)

    产生背景: 由于做服务器运维方面的工作,需要一人对近千台LINUX服务器进行统一集中的管理,如同时批量对LINUX服务器执行相关的指令.同时批量对LINUX服务器upload程序包.同时批量对LINU ...

  8. 使用ansible批量管理远程服务器

    使用ansible批量管理远程服务器 背景 本地需要管理远程的一批服务器,主要执行以下任务: 1) 将本地的文件复制到远端所有服务器: 2) 需要在远程服务器中执行一个个命令: 远端服务器路径并非完全 ...

  9. 使用Java管理千台规模Linux服务器_入门

    http://www.oschina.net/code/snippet_222919_11734 代码分享 当前位置: 代码分享 » Java  » 网络编程 搜 索   [饶过] 使用Java管理千 ...

随机推荐

  1. linux, configure --prefix 的作用

    指定安装路径不指定prefix,则可执行文件默认放在/usr /local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc.其它的资源文件放在/usr ...

  2. 基础业务:滚动到指定位置导航固定(CSS实现)

    最近公司做的业务都是使用Vue.Element写的,涉及到的相应的基础业务像轮播.预加载.懒加载,都是使用 NPM上的工具来实现,原理和基础还是要有的,就来实现几个项目中常用到的业务. 经常见到这样的 ...

  3. Daily Scrum1 11.3

    今天是我们团队进入代码实现阶段的第一天,经过一周对上一届项目代码的阅读和研究,队员们已经从代码中分析出我们这次项目将要修改和补充的地方,我们接下来要做的地方就是在两周的时间内将团队项目在alpha阶段 ...

  4. BugPhobia开发篇章:Scurm Meeting-更新至0x03

    0x01 :目录与摘要 If you weeped for the missing sunset, you would miss all the shining stars 索引 提纲 整理与更新记录 ...

  5. 【Alpha】阶段第八次Scrum Meeting

    [Alpha]阶段第八次Scrum Meeting 工作情况 团队成员 今日已完成任务 明日待完成任务 刘峻辰 编写按学院搜索课程接口 编写获得所有学院接口 赵智源 构建前测试点测试框架 编写alph ...

  6. Linux课程学习之我思

    陈民禾,原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000,我的博客中有一部分是出自M ...

  7. 提交内容到版本库:git commit

  8. 读书笔记-《Linux/Unix设计思想》

    本书主要讲的是Unix程序设计思想,具体涉及到linux的内容不多. 整本书的一个基本出发点是开源.其中主要强调的观点包括: 1.小即是美 作者持有的主要观点是程序应该以小为美.小程序实现小功能,每个 ...

  9. 看懂Qt源代码-Qt源码的对象数据存储

    第一次看Qt源代码的人都会被其代码所迷惑,经常会看到代码中的d_ptr成员.d_func(函数)和Q_DECLARE_PRIVATE等奇怪的宏,总是让人一头雾水,下面这篇文章转自http://www. ...

  10. JS 字符串切割成数组

    var cheLin = "字*符*串" // console.log(cheLin) var array = cheLin.split("*");  arra ...