簡易登入,批量執行命令,批量copy檔案shell指令碼【Linux營運之道之指令碼案例】

來源:互聯網
上載者:User

 

    名稱:satool      PS:可以讓同事門叫成了“傻兔”)

    功能:此指令碼可以批量執行命令,拷貝檔案,簡易登入,無需記住應用伺服器具體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"

    組名=“IP IP IP IP”  這種格式

 

    工具參數:

  1. -l      簡易登入模式    

    satool -l  組名

    代碼:

    [root@tools-server sbin]# satool -l lvs1) 192.168.1.1412) 192.168.1.142選擇要已連線的服務器編號(Ctrl+C退出):

    只需要輸入1,或者2 就可以直接登入,我們就不需要記住LVS的具體IP是多少了

  2. -g   批量執行命令 可以加索引】命令需要

    satool -g  組名  "cmd"  "num"(可選)

    代碼:

    [root@tools-server sbin]# satool -g lvs "df -h"192.168.1.141Filesystem            Size  Used Avail Use% Mounted on/dev/mapper/VolGroup00-LogVol0013G  6.9G  5.5G  56% //dev/sda1              99M   13M   82M  14% /boottmpfs                1006M     0 1006M   0% /dev/shm192.168.1.142Filesystem            Size  Used Avail Use% Mounted on/dev/mapper/VolGroup00-LogVol0013G  6.9G  5.5G  56% //dev/sda1              99M   13M   82M  14% /boottmpfs                1006M     0 1006M   0% /dev/shm

        這個功能大多數公司都會有,但是我們有這種情況,比如修改了所有某商務服務器的設定檔,伺服器有20台,我不能一下子全部重啟,那樣就會影響業務了,我們需要的是先重啟前5個觀察一下,沒問題之後在全部重啟。為瞭解決這個問題 -g 加入了第四個參數 就是 索引需要引號),例如:WWW組中有15台機器,我只看前5台的負載情況。

        代碼:

 

[root@tools-server sbin]# satool  -l www1) 192.168.1.2      6) 192.168.1.166  11) 192.168.7.187  16) 192.168.7.1532) 192.168.1.3      7) 192.168.3.1    12) 192.168.7.149  17) 192.168.7.1543) 192.168.1.4      8) 192.168.10.6   13) 192.168.7.150  18) 192.168.7.1554) 192.168.1.5      9) 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.00USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT192.168.1.323:47:34 up 57 days,  8:48,  0 users,  load average: 1.04, 0.70, 0.66USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT192.168.1.423:47:34 up 57 days,  8:48,  0 users,  load average: 3.29, 1.34, 0.84USER     TTY      FROM              LOGIN@   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.24USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT192.168.7.15423:50:36 up 168 days, 11 min,  0 users,  load average: 0.11, 0.23, 0.19USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT192.168.7.15323:50:37 up 160 days,  6:02,  0 users,  load average: 0.91, 0.33, 0.21USER     TTY      FROM              LOGIN@   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.00USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT192.168.1.523:51:37 up 57 days,  9:34,  0 users,  load average: 0.00, 0.01, 0.00USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT192.168.1.16623:51:37 up 301 days, 11:22,  0 users,  load average: 0.17, 0.31, 0.48USER     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.141shdmt.txt                                                                     100% 4973     4.9KB/s   00:00192.168.1.142shdmt.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-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 Bastion Host模式.";;esac

     免責聲明:使用工具為自願選擇,如果你對linux的硬引用和軟引用不是很精通的同學,請盡量不要使用帶單引號和雙引號的組合命令,盡量避免使用萬用字元*(星號),使用的時候要考慮是否轉義。如果使用本工具造成的損失,作者概不負責

本文出自 “高明” 部落格,請務必保留此出處http://gaoming.blog.51cto.com/822334/1350271

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.