名称:satool PS:(可以让同事门叫成了“傻兔”)
功能:此脚本可以批量执行命令,拷贝文件,简易登录,无需记住应用服务器具体IP即可快捷登录
说明:可以分组批量管理,单线程,不适用每组过百台设备(用也可以,就是等待返回结果时间长短问题)
用法:需要做ssh免密码登录。因为是分组管理,所以需要分组配置文件 /tools/function-all-ips (名称可以自己定,但是后面脚本需要引用此配置文件)配置文件格式
1 2 | 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 组名
代码:
1234[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"(可选)
代码:
12345678910111213[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台的负载情况。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [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 |
索引参数还可以支持负数,即反向选取
代码:
1 2 3 4 5 6 7 8 9 10 | [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 |
还可以支持非连续的单选
代码:
1 2 3 4 5 6 7 8 9 10 | [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"(可选)
代码
1 2 3 4 5 | [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多行,有点臃肿,欢迎讨论优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | #!/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 |