简易登录,批量执行命令,批量copy文件shell脚本【Linux运维之道之脚本案例】

名称:satoolPS:(可以让同事门叫成了“傻兔”)

功能:此脚本可以批量执行命令,拷贝文件,简易登录,无需记住应用服务器具体IP即可快捷登录

说明:可以分组批量管理,单线程,不适用每组过百台设备(用也可以,就是等待返回结果时间长短问题)

用法:需要做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″

组名=“IPIPIPIP”这种格式

工具参数:

-l简易登录模式

satool-l组名

代码:

[root@tools-server sbin]# satool -l lvs1) 192.168.1.1412) 192.168.1.142选择要连接的服务器编号(Ctrl+C退出):

只需要输入1,或者2就可以直接登录,我们就不需要记住LVS的具体IP是多少了

-g批量执行命令【可以加索引】命令需要

satool-g组名”cmd””num”(可选)

代码:

[root@tools-server sbin]# satool -g lvs “df -h”192.168.1.141FilesystemSize Used Avail Use% Mounted on/dev/mapper/VolGroup00-LogVol0013G 6.9G 5.5G 56% //dev/sda199M 13M 82M 14% /boottmpfs1006M0 1006M 0% /dev/shm192.168.1.142FilesystemSize Used Avail Use% Mounted on/dev/mapper/VolGroup00-LogVol0013G 6.9G 5.5G 56% //dev/sda199M 13M 82M 14% /boottmpfs1006M0 1006M 0% /dev/shm

这个功能大多数公司都会有,但是我们有这种情况,比如修改了所有某业务服务器的配置文件,服务器有20台,我不能一下子全部重启,那样就会影响业务了,我们需要的是先重启前5个观察一下,没问题之后在全部重启。为了解决这个问题-g加入了第四个参数就是索引(需要引号),例如:WWW组中有15台机器,我只看前5台的负载情况。

代码:

[root@tools-server sbin]# satool -l www1) 192.168.1.26) 192.168.1.166 11) 192.168.7.187 16) 192.168.7.1532) 192.168.1.37) 192.168.3.1 12) 192.168.7.149 17) 192.168.7.1543) 192.168.1.48) 192.168.10.6 13) 192.168.7.150 18) 192.168.7.1554) 192.168.1.59) 192.168.7.185 14) 192.168.7.1515) 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.223:47:34 up 57 days, 8:48, 0 users, load average: 0.00, 0.01, 0.00USERTTYFROMLOGIN@ IDLE JCPU PCPU WHAT192.168.1.323:47:34 up 57 days, 8:48, 0 users, load average: 1.04, 0.70, 0.66USERTTYFROMLOGIN@ IDLE JCPU PCPU WHAT192.168.1.423:47:34 up 57 days, 8:48, 0 users, load average: 3.29, 1.34, 0.84USERTTYFROMLOGIN@ IDLE JCPU PCPU WHAT

索引参数还可以支持负数,即反向选取

代码:

[root@tools-server sbin]# satool -g www “w” “-3″192.168.7.15523:50:36 up 164 days, 21:52, 0 users, load average: 0.80, 0.40, 0.24USERTTYFROMLOGIN@ IDLE JCPU PCPU WHAT192.168.7.15423:50:36 up 168 days, 11 min, 0 users, load average: 0.11, 0.23, 0.19USERTTYFROMLOGIN@ IDLE JCPU PCPU WHAT192.168.7.15323:50:37 up 160 days, 6:02, 0 users, load average: 0.91, 0.33, 0.21USERTTYFROMLOGIN@ IDLE JCPU PCPU WHAT

还可以支持非连续的单选

代码:

[root@tools-server sbin]# satool -g www “w” “1 4 6″192.168.1.223:51:37 up 57 days, 8:52, 0 users, load average: 0.00, 0.00, 0.00USERTTYFROMLOGIN@ IDLE JCPU PCPU WHAT192.168.1.523:51:37 up 57 days, 9:34, 0 users, load average: 0.00, 0.01, 0.00USERTTYFROMLOGIN@ IDLE JCPU PCPU WHAT192.168.1.16623:51:37 up 301 days, 11:22, 0 users, load average: 0.17, 0.31, 0.48USERTTYFROMLOGIN@ IDLE JCPU PCPU WHAT

3.-cp批量把本地的文件推送到远端服务器上(单向)【可以加索引】

satool-cp组名srcdest“num”(可选)

代码

[root@tools-server tmp]# satool -cp lvs shdmt.txt /tmp/192.168.1.141shdmt.txt100% 49734.9KB/s 00:00192.168.1.142shdmt.txt100% 49734.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-ipsll=`cat /tools/function-all-ips |awk -F = ‘{print $1}’ |grep -v ‘#’`grop () {if [ -z “$app” ]thenecho -e “${yel}$ll${end}”|xargs -n 4 |sed ‘s/ /\] \[/g’|sed ‘s/^/\[/g’|sed ‘s/$/\]/g’elif [ -z “$cmd” ]thenecho $ll |grep -w $app > /dev/nullif [ $? -eq 0 ]theneval echo ‘$’$app |xargs -n 1elseecho -e “${red} 输入的组不在 function-all-ips!! $end”fielif [ $# -eq 2 ]thenfor ip in `eval echo “$”$app`doecho -e “$red $ip $end”ssh $ip “$cmd”doneelif [ $# -eq 3 ]thentotal=`eval echo ‘$’$app |wc -w`tmpnum=`echo $num|wc -w`shuzu=(`eval echo “$”$app`)echo $num | egrep “[a-zA-Z]” >/dev/null 2>&1satus=$?if [ $satus -ne 0 ]thenif [ $tmpnum -eq 1 ] && [ ${num#-} -le $total ]thenif [ $num -gt 0 ]thenfor s in `seq 0 $(($num-1))`doip=${shuzu[$s]}echo -e “$red $ip $end”ssh $ip “$cmd”doneelif [ $num -lt 0 ]thennum=${num#-}rshuzu=(`eval echo “$”$app |xargs -n 1|tac|xargs`)for s in `seq 0 $(($num-1))`doip=${rshuzu[$s]}echo -e “$red $ip $end”ssh $ip “$cmd”donefielif [ $tmpnum -gt 1 ]thenfor s in $numdoif [ $s -le $total ];thenip=${shuzu[$(($s-1))]}echo -e “$red $ip $end”ssh $ip “$cmd”elseecho “$s is more than $app total”fidoneelseecho “$num is more than $app total”fielseecho “$num is not number”fifi}ip () {if [ -z “$app” ]thenecho -e “${yel}$ll${end}”|xargs -n 4 |sed ‘s/ /\] \[/g’|sed ‘s/^/\[/g’|sed ‘s/$/\]/g’elseecho “$app” | egrep ‘^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$’ >/dev/nullstat=$?if [ “$stat” -eq 0 ]thenping -c 1 $app > /dev/nullif [ “$?” -eq 0 ]thenif [ -z “$cmd” ]thenecho -e “${red} 缺少命令 参数 !!! $end”elif [ $# -eq 2 ]thenecho -e “$red $app $end”ssh $app “$cmd”fielseecho -e “${red}$app 网络不可达${end}”fielseecho -e “${red}无效的IP输入${end}”fifi}SSH () {if [ -z “$app” ]thenecho -e “${yel}$ll${end}”|xargs -n 4 |sed ‘s/ /\] \[/g’|sed ‘s/^/\[/g’|sed ‘s/$/\]/g’elseecho $ll |grep -w $app > /dev/nullif [ $? -ne 0 ]thenecho -e “${red} 输入的组不在 function-all-ips!! $end”elsePS3=”选择要连接的服务器编号(Ctrl+C退出):”lt=`eval echo “$”$app`select ip in $ltdossh $ipbreakdonefifi}fort () {pid=$$for i in $lldolt=`eval echo “$”$i`cat << EOF >>/tmp/$pid.log$i () {select a in $ltdossh \$abreakdone}EOFdone. /tmp/$pid.logrm /tmp/$pid.logPS3=”Select group (Ctrl+C to exit):”select ip in $lldoPS3=”Select server: “$ipbreakdone}search () {if [ -z $app ];thenecho ‘-s is search mode’echo ‘please input IP ‘elseecho “$app” | egrep ‘^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$’ >/dev/nullstat=$?if [ “$stat” -eq 0 ]thenecho $appgrep -w $app /tools/function-all-ips |awk -F ‘=’ ‘{print $1}’elseecho “无效的IP”fifi}cps () {if [ -z “$app” ]thenecho -e “${yel}$ll${end}”|xargs -n 4 |sed ‘s/ /\] \[/g’|sed ‘s/^/\[/g’|sed ‘s/$/\]/g’elif [ ! -z $app ] && [ -z $sfile ] || [ ! -f $sfile ]thenecho $ll |grep -w $app > /dev/nullif [ $? -eq 0 ]thenecho -e “源文件未指定或不存在!!”eval echo ‘$’$app |xargs -n 1elseecho -e “${red} 输入的组不在 function-all-ips!! $end”fielif [ $# -eq 3 ]thenfor ip in `eval echo “$”$app`doecho -e “$red $ip $end”scp $sfile $ip:$dfiledoneelif [ $# -eq 4 ]thentotal=`eval echo ‘$’$app |wc -w`tmpnum=`echo $num|wc -w`shuzu=(`eval echo “$”$app`)echo $num | egrep “[a-zA-Z]” >/dev/null 2>&1satus=$?if [ $satus -ne 0 ]thenif [ $tmpnum -eq 1 ] && [ ${num#-} -le $total ]thenif [ $num -gt 0 ]thenfor s in `seq 0 $(($num-1))`doip=${shuzu[$s]}echo -e “$red $ip $end”scp $sfile $ip:$dfiledoneelif [ $num -lt 0 ]thennum=${num#-}rshuzu=(`eval echo “$”$app |xargs -n 1|tac|xargs`)for s in `seq 0 $(($num-1))`doip=${rshuzu[$s]}echo -e “$red $ip $end”scp $sfile $ip:$dfiledonefielif [ $tmpnum -gt 1 ]thenfor s in $numdoif [ $s -lt $total ];thenip=${shuzu[$(($s-1))]}echo -e “$red $ip $end”scp $sfile $ip:$dfileelseecho “$s is more than $app total”fidoneelseecho “$num is more than $app total”fielseecho “$num is not number”fifi}case $1 in-i)shiftapp=$1shiftcmd=$*ip $app “$cmd”;;-g)shiftapp=$1shiftcmd=$1shiftnum=$1if [ -z “$num” ];thengrop $app “$cmd”elsegrop $app “$cmd” “$num”fi;;-cp)shiftapp=$1shiftsfile=$1shiftdfile=$1shiftnum=$1if [ -z “$num” ]thenif [ -z $sfile ] || [ -z $dfile ]thencps $appelsecps $app “$sfile” “$dfile”fielseif [ -z $sfile ] || [ -z $dfile ]thenechocps $appelsecps $app “$sfile” “$dfile” “$num”fifi;;-l)shiftapp=$1SSH $app;;-a)fort;;-s)shiftapp=$1search;;*)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为什么?答:点线杆上贴着”“此处不许小便!”

简易登录,批量执行命令,批量copy文件shell脚本【Linux运维之道之脚本案例】

相关文章:

你感兴趣的文章:

标签云: