名稱: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的硬引用和軟引用不是很精通的同窗,請儘可能不要使用帶單引號和雙引號的組合命令,儘可能避免使用通配符*(星號),使用的時候要考慮是否轉義。若是使用本工具形成的損失,做者概不負責