Bulk copy file shell scripts

Source: Internet
Author: User
Tags egrep

Name: satool PS: (you can call a colleague a "dumb ")

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: You can manage devices in batches by group, with a single thread. It is not applicable to over devices in each group. (You can also use it to wait for the response time)

Usage: ssh password-free login is required. Because grouping management is required, you need to group the configuration file/tools/function-all-ips (the name can be set by yourself, but this configuration file needs to be referenced by subsequent scripts ).


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:

-L simple logon Mode

Satool-l group name

Code:

1234 [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.

-G: You can add indexes to execute the command in batches.

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 /sda199M 13 M 82 M 14%/boottmpfs 1006 M 0 1006 M 0%/dev/shm192.168.1.142Filesystem Size Used Avail Use % Mounted on/dev/mapper/VolGroup00-LogVol0013G 6.9G 5.5G 56%/dev /sda199M 13 M 82 M 14%/boottmpfs 1006 M 0 1006 M 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 to the index (which requires quotation marks). 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, 0 users, load average: 0.80, 0.40, 0.24 user 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.19 user tty from login @ idle jcpu pcpu WHAT192.168.7.15323: 50: 37 up 160 days, 0 users, load average: 0.91, 0.33, 0.21 user tty from login @ IDLE JCPU PCPU

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, 0 users, load average: 0.00, 0.00, 0.00 user tty from login @ idle jcpu pcpu WHAT192.168.1.523: 51: 37 up 57 days, 0 users, load average: 0.00, 0.01, 0.00 user tty from login @ idle jcpu pcpu WHAT192.168.1.16623: 51: 37 up 301 days, 0 users, load average: 0.17, 0.31, 0.48 user tty from login @ IDLE JCPU PCPU WHAT

-Cp pushes local files to the remote server in batches (one-way). [You can add indexes]

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/s00: 00192.168.1.142shdmt.txt 100% 4973 4.9KB/s00: 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>/dev/nullif [$? -Eq0] thenevalecho '$ app | xargs-n 1elseecho-e "$ {red} the input group is not in function-all-ips !! $ End "fielif [$ #-eq 2] thenforip in 'evalecho" $ "$ app 'doecho-e" $ red $ ip $ end "ssh $ ip" $ cmd "doneelif [$ #-eq 3] thentotal = 'evalecho '$ app | wc-W' tmpnum = 'echo $ num | wc-W' shuzu = ('evalecho "$" $ app ') echo $ num | egrep "[a-zA-Z]">/dev/null2> & 1 satus =$? If [$ satus-ne0] thenif [$ tmpnum-eq1] & [$ {num #-}-le $ total] thenif [$ num-gt 0] thenfors in 'seq0 $ ($ num-1 )) 'doip =$ {shuzu [$ s]} echo-e "$ red $ ip $ end" ssh $ ip "$ cmd" doneelif [$ num-lt 0] thennum = $ {num #-} rshuzu = ('evalecho "$" $ app | xargs-n 1 | tac | xargs ') fors in 'seq0 $ ($ num-1 )) 'doip =$ {rshuzu [$ s]} echo-e "$ red $ ip $ end" ssh $ ip "$ cmd" donefielif [$ tmpnum-gt 1] thenfors 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"-eq0] thenping-c 1 $ app>/dev/nullif ["$? "-Eq0] 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 [$? -Ne0] thenecho-e "$ {red} the input group is not in function-all-ips !! $ End "elsePS3 =" select the server number to connect (Ctrl + C Exit): "lt = 'evalecho" $ "$ app' selectip in $ ltdossh $ ipbreakdonefifi} fort () {pid =$ $ fori in $ lldolt = 'evalecho "$" $ I 'cat <EOF>/tmp/$ pid. log $ I () {selecta in $ ltdossh \ $ abreakdone} EOFdone. /tmp/$ pid. logrm/tmp/$ pid. logPS3 = "Select group (Ctrl + C to exit):" selectip in $ lldoPS3 = "Select server:" $ ipbreakdone} search () {if [-z $ app]; thenecho '-s is search mode 'echo 'Please input ip' el Seecho "$ app" | egrep '^ [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3 }\. [0-9] {1, 3} $ '>/dev/nullstat = $? If ["$ stat"-eq0] thenecho $ appgrep-w $ app/tools/function-all-ips | awk-F' = ''{print $1} 'elseecho" invalid 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 [$? -Eq0] thenecho-e "the source file is not specified or does not exist !! "Evalecho '$ app | xargs-n 1elseecho-e" $ {red} the input group is not in function-all-ips !! $ End "fielif [$ #-eq 3] thenforip in 'evalecho" $ "$ app 'doecho-e" $ red $ ip $ end "scp $ sfile $ ip: $ dfiledoneelif [$ #-eq 4] thentotal = 'evalecho '$ app | wc-W' tmpnum = 'echo $ num | wc-W' shuzu = ('evalecho" $ "$ app ') echo $ num | egrep "[a-zA-Z]">/dev/null2> & 1 satus =$? If [$ satus-ne0] thenif [$ tmpnum-eq1] & [$ {num #-}-le $ total] thenif [$ num-gt 0] thenfors in 'seq0 $ ($ num-1 )) 'doip =$ {shuzu [$ s]} echo-e "$ red $ ip $ end" scp $ sfile $ ip: $ dfiledoneelif [$ num-lt 0] thennum =$ {num #-} rshuzu = ('evalecho "$" $ app | xargs-n 1 | tac | xargs ') fors in 'seq0 $ ($ num-1) 'doip =$ {rshuzu [$ s]} echo-e "$ red $ ip $ end" scp $ sfile $ ip: $ dfiledonefielif [$ tmpnum-gt 1] thenfors 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.