Simple logon, batch command execution, batch copy file shell script [Linux O & M script case]

Source: Internet
Author: User

 

Name: satool PS: it can be called a "Silly Rabbit" by a colleague ")

Function: This script can run commands in batches, copy files, and perform simple logon. You can quickly log on without remembering the specific IP address of the application server.

Note: batch management can be performed by group, with a single thread. It is not applicable to the problem that hundreds of devices in each group can be used, that is, the duration of waiting for the returned results)

Usage: ssh password-free login is required. Because grouping management is required, you can set the group configuration file/tools/function-all-ips name by yourself, but the configuration file must be referenced by the subsequent script.) Configuration File Format

log_server="10.88.1.78 10.88.1.47 192.168.3.117"lvs="192.168.1.141 192.168.1.142"

Group name = "IP" Format

 

Tool parameters:

  1. -L simple logon Mode

    Satool-l group name

    Code:

    [Root @ tools-server sbin] # satool-l lvs1) 192.168.1.1412) 192.168.1.142 select the server number to be connected (Ctrl + C Exit ):

    You only need to enter 1 or 2 to log on directly. We do not need to remember the specific IP address of LVS.

  2. -G: You can add indexes to batch execute commands.

    Satool-g group name "cmd" "num" (optional)

    Code:

    [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

This function is available in most companies, but we have this situation. For example, if we modify the configuration files of all the business servers and 20 servers, I cannot restart them all at once, in this way, the business will be affected. What we need is to first restart the first five of them, and then restart them all. To solve this problem, g adds the fourth parameter that requires quotation marks for the index. For example, if there are 15 machines in the WWW group, I only check the load of the first 5 machines.

Code:

 

[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 select the server number to connect (Ctrl + C Exit ): [root @ tools-server sbin] # satool-g www "w" "3" 192.168.1.223: 47: 34 up 57 days, 0 users, load average: 0.00, 0.01, 0.00 user tty from login @ idle jcpu pcpu WHAT192.168.1.323: 47: 34 up 57 days, 0 users, load average: 1.04, 0.70, 0.66 user tty from login @ idle jcpu pcpu WHAT192.168.1.423: 47: 34 up 57 days, 0 users, load average: 3.29, 1.34, 0.84 user tty from login @ IDLE JCPU PCPU

Index parameters can also support negative numbers, that is, reverse selection

Code:

 

[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

It also supports non-consecutive radio

Code:

[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.-You can add an index if cp pushes local files to the remote server in batches]

Satool-cp group name src dest "num" (optional)

Code

 

[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

The main function is the above. Other functions are auxiliary. The following code contains more than 300 lines, which is a bit bloated. You are welcome to discuss the optimization.

 

#! /Bin/bash ## 2012-11-21 ######### make by G.M ######## version 3.0 ###### red = "\ 033 [31 m "end =" \ 033 [0 m "yel =" \ 033 [33 m "pur =" \ 033 [35 m "# 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>/de V/nullif [$? -Eq 0] theneval echo '$ app | xargs-n 1 elseecho-e "$ {red} the input group is not in 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> & 1 satus = $? 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} the command parameter is missing !!! $ End "elif [$ #-eq 2] thenecho-e" $ red $ app $ end "ssh $ app" $ cmd "fielseecho-e" $ {red} $ app Network inaccessible $ {end} "fielseecho-e" $ {red} invalid IP address Input $ {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} the input group is not in function-all-ips !! $ End "elsePS3 =" select the server number to connect (Ctrl + C Exit ): "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 "invalid IP Address" 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 "the source file is not specified or does not exist !! "Eval echo '$ app | xargs-n 1 elseecho-e" $ {red} the input group is not in 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> & 1 satus = $? 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 = $1 shiftcmd = $ * ip $ app "$ cmd";-g) shiftapp = $1 shiftcmd = $1 shiftnum = $ 1if [-z "$ num"]; thengrop $ app "$ cmd" elsegrop $ app "$ cmd" "$ num" fi;-cp) shiftapp = $1 shiftsfile = $1 shiftdfile = $1 shiftnum = $ 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 = $1 search ;;*) echo "$0 [-I |-g]" echo "-I IP cmd" echo "-g group cmd [index] often uses" echo "-cp group src dest [index] "echo"-s search mode "echo"-l group (ssh usage) common "echo"-a bastion host mode. "; esac

Disclaimer: the tool is a voluntary choice. If you are not very familiar with linux Hard reference and soft reference, try not to use a combination of single quotes and double quotes, avoid using wildcards * (asterisks) whenever possible, and consider escape. The author is not liable for any losses caused by the use of this tool.

This article from the "Gaoming" blog, please be sure to keep this source http://gaoming.blog.51cto.com/822334/1350271

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.