名称:satool PS:(可让同事门叫成了“傻兔”)linux
功能:此脚本能够批量执行命令,拷贝文件,简易登陆,无需记住应用服务器具体IP便可快捷登陆bash
说明:能够分组批量管理,单线程,不适用每组过百台设备(用也能够,就是等待返回结果时间长短问题)服务器
用法:须要作ssh免密码登陆。由于是分组管理,因此须要分组配置文件 /tools/function-all-ips (名称能够本身定,可是后面脚本须要引用此配置文件)配置文件格式网络
log_server="10.88.1.78 10.88.1.47 192.168.3.117" lvs="192.168.1.141 192.168.1.142"
组名=“IP IP IP IP” 这种格式app
工具参数:ssh
-l 简易登陆模式 ide
satool -l 组名工具
代码:优化
[root@tools-server sbin]# satool -l lvs 1) 192.168.1.141 2) 192.168.1.142 选择要链接的服务器编号(Ctrl+C退出):
只须要输入1,或者2 就能够直接登陆,咱们就不须要记住LVS的具体IP是多少了spa
-g 批量执行命令 【能够加索引】命令须要
satool -g 组名 "cmd" "num"(可选)
代码:
[root@tools-server sbin]# satool -g lvs "df -h" 192.168.1.141 Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup00-LogVol00 13G 6.9G 5.5G 56% / /dev/sda1 99M 13M 82M 14% /boot tmpfs 1006M 0 1006M 0% /dev/shm 192.168.1.142 Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup00-LogVol00 13G 6.9G 5.5G 56% / /dev/sda1 99M 13M 82M 14% /boot tmpfs 1006M 0 1006M 0% /dev/shm
这个功能大多数公司都会有,可是咱们有这种状况,好比修改了全部某业务服务器的配置文件,服务器有20台,我不能一会儿所有重启,那样就会影响业务了,咱们须要的是先重启前5个观察一下,没问题以后在所有重启。为了解决这个问题 -g 加入了第四个参数 就是 索引(须要引号),例如:WWW组中有15台机器,我只看前5台的负载状况。
代码:
[root@tools-server sbin]# satool -l www 1) 192.168.1.2 6) 192.168.1.166 11) 192.168.7.187 16) 192.168.7.153 2) 192.168.1.3 7) 192.168.3.1 12) 192.168.7.149 17) 192.168.7.154 3) 192.168.1.4 8) 192.168.10.6 13) 192.168.7.150 18) 192.168.7.155 4) 192.168.1.5 9) 192.168.7.185 14) 192.168.7.151 5) 192.168.1.164 10) 192.168.7.186 15) 192.168.7.152 选择要链接的服务器编号(Ctrl+C退出): [root@tools-server sbin]# satool -g www "w" "3" 192.168.1.2 23:47:34 up 57 days, 8:48, 0 users, load average: 0.00, 0.01, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT 192.168.1.3 23:47:34 up 57 days, 8:48, 0 users, load average: 1.04, 0.70, 0.66 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT 192.168.1.4 23:47:34 up 57 days, 8:48, 0 users, load average: 3.29, 1.34, 0.84 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
索引参数还能够支持负数,即反向选取
代码:
[root@tools-server sbin]# satool -g www "w" "-3" 192.168.7.155 23:50:36 up 164 days, 21:52, 0 users, load average: 0.80, 0.40, 0.24 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT 192.168.7.154 23:50:36 up 168 days, 11 min, 0 users, load average: 0.11, 0.23, 0.19 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT 192.168.7.153 23:50:37 up 160 days, 6:02, 0 users, load average: 0.91, 0.33, 0.21 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
还能够支持非连续的单选
代码:
[root@tools-server sbin]# satool -g www "w" "1 4 6" 192.168.1.2 23:51:37 up 57 days, 8:52, 0 users, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT 192.168.1.5 23:51:37 up 57 days, 9:34, 0 users, load average: 0.00, 0.01, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT 192.168.1.166 23:51:37 up 301 days, 11:22, 0 users, load average: 0.17, 0.31, 0.48 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
3. -cp 批量把本地的文件推送到远端服务器上(单向)【能够加索引】
satool -cp 组名 src dest “num"(可选)
代码
[root@tools-server tmp]# satool -cp lvs shdmt.txt /tmp/ 192.168.1.141 shdmt.txt 100% 4973 4.9KB/s 00:00 192.168.1.142 shdmt.txt 100% 4973 4.9KB/s 00:00
主要功能就是以上这些,其余功能都是辅助的了下面是完整代码共300多行,有点臃肿,欢迎讨论优化
#!/bin/bash ##2012-11-21####### ##make by G.M###### ##version 3.0###### red="\033[31m" end="\033[0m" yel="\033[33m" pur="\033[35m" #function-all-ips DIR## . /tools/function-all-ips ll=`cat /tools/function-all-ips |awk -F = '{print $1}' |grep -v '#'` grop () { if [ -z "$app" ] then echo -e "${yel}$ll${end}"|xargs -n 4 |sed 's/ /\] \[/g'|sed 's/^/\[/g'|sed 's/$/\]/g' elif [ -z "$cmd" ] then echo $ll |grep -w $app > /dev/null if [ $? -eq 0 ] then eval echo '$'$app |xargs -n 1 else echo -e "${red} 输入的组不在 function-all-ips!! $end" fi elif [ $# -eq 2 ] then for ip in `eval echo "$"$app` do echo -e "$red $ip $end" ssh $ip "$cmd" done elif [ $# -eq 3 ] then total=`eval echo '$'$app |wc -w` tmpnum=`echo $num|wc -w` shuzu=(`eval echo "$"$app`) echo $num | egrep "[a-zA-Z]" >/dev/null 2>&1 satus=$? if [ $satus -ne 0 ] then if [ $tmpnum -eq 1 ] && [ ${num#-} -le $total ] then if [ $num -gt 0 ] then for s in `seq 0 $(($num-1))` do ip=${shuzu[$s]} echo -e "$red $ip $end" ssh $ip "$cmd" done elif [ $num -lt 0 ] then num=${num#-} rshuzu=(`eval echo "$"$app |xargs -n 1|tac|xargs`) for s in `seq 0 $(($num-1))` do ip=${rshuzu[$s]} echo -e "$red $ip $end" ssh $ip "$cmd" done fi elif [ $tmpnum -gt 1 ] then for s in $num do if [ $s -le $total ];then ip=${shuzu[$(($s-1))]} echo -e "$red $ip $end" ssh $ip "$cmd" else echo "$s is more than $app total" fi done else echo "$num is more than $app total" fi else echo "$num is not number" fi fi } ip () { if [ -z "$app" ] then echo -e "${yel}$ll${end}"|xargs -n 4 |sed 's/ /\] \[/g'|sed 's/^/\[/g'|sed 's/$/\]/g' else echo "$app" | egrep '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' >/dev/null stat=$? if [ "$stat" -eq 0 ] then ping -c 1 $app > /dev/null if [ "$?" -eq 0 ] then if [ -z "$cmd" ] then echo -e "${red} 缺乏命令 参数 !!! $end" elif [ $# -eq 2 ] then echo -e "$red $app $end" ssh $app "$cmd" fi else echo -e "${red}$app 网络不可达${end}" fi else echo -e "${red}无效的IP输入${end}" fi fi } SSH () { if [ -z "$app" ] then echo -e "${yel}$ll${end}"|xargs -n 4 |sed 's/ /\] \[/g'|sed 's/^/\[/g'|sed 's/$/\]/g' else echo $ll |grep -w $app > /dev/null if [ $? -ne 0 ] then echo -e "${red} 输入的组不在 function-all-ips!! $end" else PS3="选择要链接的服务器编号(Ctrl+C退出):" lt=`eval echo "$"$app` select ip in $lt do ssh $ip break done fi fi } fort () { pid=$$ for i in $ll do lt=`eval echo "$"$i` cat << EOF >>/tmp/$pid.log $i () { select a in $lt do ssh \$a break done } EOF done . /tmp/$pid.log rm /tmp/$pid.log PS3="Select group (Ctrl+C to exit):" select ip in $ll do PS3="Select server: " $ip break done } search () { if [ -z $app ];then echo '-s is search mode' echo 'please input IP ' else echo "$app" | egrep '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' >/dev/null stat=$? if [ "$stat" -eq 0 ] then echo $app grep -w $app /tools/function-all-ips |awk -F '=' '{print $1}' else echo "无效的IP" fi fi } cps () { if [ -z "$app" ] then echo -e "${yel}$ll${end}"|xargs -n 4 |sed 's/ /\] \[/g'|sed 's/^/\[/g'|sed 's/$/\]/g' elif [ ! -z $app ] && [ -z $sfile ] || [ ! -f $sfile ] then echo $ll |grep -w $app > /dev/null if [ $? -eq 0 ] then echo -e "源文件未指定或不存在!!" eval echo '$'$app |xargs -n 1 else echo -e "${red} 输入的组不在 function-all-ips!! $end" fi elif [ $# -eq 3 ] then for ip in `eval echo "$"$app` do echo -e "$red $ip $end" scp $sfile $ip:$dfile done elif [ $# -eq 4 ] then total=`eval echo '$'$app |wc -w` tmpnum=`echo $num|wc -w` shuzu=(`eval echo "$"$app`) echo $num | egrep "[a-zA-Z]" >/dev/null 2>&1 satus=$? if [ $satus -ne 0 ] then if [ $tmpnum -eq 1 ] && [ ${num#-} -le $total ] then if [ $num -gt 0 ] then for s in `seq 0 $(($num-1))` do ip=${shuzu[$s]} echo -e "$red $ip $end" scp $sfile $ip:$dfile done elif [ $num -lt 0 ] then num=${num#-} rshuzu=(`eval echo "$"$app |xargs -n 1|tac|xargs`) for s in `seq 0 $(($num-1))` do ip=${rshuzu[$s]} echo -e "$red $ip $end" scp $sfile $ip:$dfile done fi elif [ $tmpnum -gt 1 ] then for s in $num do if [ $s -lt $total ];then ip=${shuzu[$(($s-1))]} echo -e "$red $ip $end" scp $sfile $ip:$dfile else echo "$s is more than $app total" fi done else echo "$num is more than $app total" fi else echo "$num is not number" fi fi } case $1 in -i) shift app=$1 shift cmd=$* ip $app "$cmd" ;; -g) shift app=$1 shift cmd=$1 shift num=$1 if [ -z "$num" ];then grop $app "$cmd" else grop $app "$cmd" "$num" fi ;; -cp) shift app=$1 shift sfile=$1 shift dfile=$1 shift num=$1 if [ -z "$num" ] then if [ -z $sfile ] || [ -z $dfile ] then cps $app else cps $app "$sfile" "$dfile" fi else if [ -z $sfile ] || [ -z $dfile ] then echo cps $app else cps $app "$sfile" "$dfile" "$num" fi fi ;; -l) shift app=$1 SSH $app ;; -a) fort ;; -s) shift app=$1 search ;; *) echo "$0 [-i|-g]" echo "-i IP cmd" echo "-g group cmd [index] 常常用" echo "-cp group src dest [index]" echo "-s search mode" echo "-l group (ssh usage) 经常使用" echo "-a 堡垒机模式." ;; esac
免责声明:使用工具为自愿选择,若是你对linux的硬引用和软引用不是很精通的同窗,请尽可能不要使用带单引号和双引号的组合命令,尽可能避免使用通配符*(星号),使用的时候要考虑是否转义。若是使用本工具形成的损失,做者概不负责