简易登录,批量执行命令,批量copy文件shell脚本【Linux运维之道之脚本事例】51CTO博客 - 众发娱乐

简易登录,批量执行命令,批量copy文件shell脚本【Linux运维之道之脚本事例】51CTO博客

2019年04月14日12时31分22秒 | 作者: 曜栋 | 标签: 登录,批量,能够 | 浏览: 1590

 

    称号: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 lvs
    1) 192.168.1.141
    2) 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.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台的负载状况。

        代码:

 

[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

  3. -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/s 00:00
192.168.1.142
shdmt.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-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

     免责声明:运用东西为自愿挑选,假如你对linux的硬引证和软引证不是很通晓的同学,请尽量不要运用带单引号和双引号的组合指令,尽量防止运用通配符*(星号),运用的时分要考虑是否转义。假如运用本东西形成的丢失,作者概不负责

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表众发娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章