> shell编程 >

批量copy文件shell脚本

名称: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”  这种格式




   工具参数:

-l      简易登录模式   

satool -l  组名

代码:

1
2
3
4 [root@tools-server sbin]# satool -l lvs
1) 192.168.1.141
2) 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.141
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
13G  6.9G  5.5G  56% /
/dev/sda199M   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/sda199M   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

-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/s00:00
192.168.1.142
shdmt.txt                                                                     100% 4973     4.9KB/s00: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[ $? -eq0 ]
then
evalecho'$'$app |xargs-n 1
else
echo-e "${red} 输入的组不在  function-all-ips!! $end"
fi
elif[ $# -eq 2 ]
then
forip in`evalecho"$"$app`
do
echo-e "$red $ip $end"
ssh$ip "$cmd"
done
elif[ $# -eq 3 ]
then
total=`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 ]
then
if[ $tmpnum -eq1 ]   && [ ${num#-} -le $total  ]
then
if[ $num -gt 0 ]
then
fors in`seq0 $(($num-1))`
do
ip=${shuzu[$s]}
echo-e "$red $ip $end"
ssh$ip "$cmd"
done
elif[ $num -lt 0 ]
then
num=${num#-}
rshuzu=(`evalecho"$"$app |xargs-n 1|tac|xargs`)
fors in`seq0 $(($num-1))`
do
ip=${rshuzu[$s]}
echo-e "$red $ip $end"
ssh$ip "$cmd"
done
fi
elif[ $tmpnum -gt 1 ]
then
fors 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"-eq0 ]
then
ping-c 1  $app > /dev/null
if[ "$?"-eq0 ]
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[ $? -ne0 ]
then
echo-e "${red} 输入的组不在  function-all-ips!! $end"
else
PS3="选择要连接的服务器编号(Ctrl+C退出):"
lt=`evalecho"$"$app`
selectip in$lt
do
ssh$ip
break
done
fi
fi
}
fort () {
pid=$$
fori in$ll
do
lt=`evalecho"$"$i`
cat<< EOF >>/tmp/$pid.log
$i () {
selecta in$lt
do
ssh\$a
break
done
}
EOF
done
. /tmp/$pid.log
rm/tmp/$pid.log
PS3="Select group (Ctrl+C to exit):"
selectip 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"-eq0 ]
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[ $? -eq0 ]
then
echo-e "源文件未指定或不存在!!"
evalecho'$'$app |xargs-n 1
else
echo-e "${red} 输入的组不在  function-all-ips!! $end"
fi
elif[ $# -eq 3 ]
then
forip in`evalecho"$"$app`
do
echo-e "$red $ip $end"
scp$sfile  $ip:$dfile
done
elif[ $# -eq 4 ]
then
total=`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 ]
then
if[ $tmpnum -eq1 ]   && [ ${num#-} -le $total  ]
then
if[ $num -gt 0 ]
then
fors in`seq0 $(($num-1))`
do
ip=${shuzu[$s]}
echo-e "$red $ip $end"
scp$sfile  $ip:$dfile
done
elif[ $num -lt 0 ]
then
num=${num#-}
rshuzu=(`evalecho"$"$app |xargs-n 1|tac|xargs`)
fors in`seq0 $(($num-1))`
do
ip=${rshuzu[$s]}
echo-e "$red $ip $end"
scp$sfile  $ip:$dfile
done
fi
elif[ $tmpnum -gt 1 ]
then
fors 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的硬引用和软引用不是很精通的同学,请尽量不要使用带单引号和双引号的组合命令,尽量避免使用通配符*(星号),使用的时候要考虑是否转义。如果使用本工具造成的损失,作者概不负责

本文出自 “高明” 博客,请务必保留此出处http://gaoming.blog.51cto.com/822334/1350271

(责任编辑:IT)