设为首页 收藏本站
查看: 543|回复: 0

LazyManage[shell版]批量管理

[复制链接]

尚未签到

发表于 2018-8-21 06:49:18 | 显示全部楼层 |阅读模式
#!/bin/bash  
#ShellName:LazyManage.sh
  
#Conf:serverlist.conf
  
#By:peter.li six
  
#2013-11-28
  
#LazyManage.sh version update address:
  
#http://hi.baidu.com/quanzhou722/item/4ccf7e88a877eaccef083d1a
  
#shell examples manual:
  
#http://hi.baidu.com/quanzhou722/item/f4a4f3c9eb37f02d46d5c0d9
  
#python examples manual:
  
#http://hi.baidu.com/quanzhou722/item/cf4471f8e23d3149932af2a7
  
LANG="en_US.UTF-8"
  
while true
  
do
  
Set_Variable(){
  
ServerList=serverlist.conf
  
Port=22
  
TimeOut="-1"
  
Task=30
  
RemoteUser='peterli'
  
RemotePasswd='123456'
  
RemoteRootUser='root'
  
RemoteRootPasswd='xuesong'
  
KeyPasswd=''
  
ScpPath="lazy.txt"
  
ScpRemotePath="/tmp/"
  
ScriptPath="Remote.sh"
  
}
  
System_Check(){
  
#Kill the CTRL + z sleep process
  
if [ "$1" == kill ];then
  
    ps -eaf |awk '$NF~/.*'${0##*/}'/&&$6~/tty|pts.*/{print $2}' |xargs -t -i kill -9 {}
  
    exit
  
fi
  
#Check the configuration file
  
if [ ! -s serverlist.conf ];then
  
    echo "error:IP list serverlist.conf file does not exist or is null"
  
    exit
  
fi
  
#rpm check
  
for i in dialog expect
  
do
  
    rpm -q $i >/dev/null
  
    [ $? -ge 1 ] && echo "$i does not exist,Please root yum -y install $i to install,exit" && exit
  
done
  
#The current user
  
#LazyUser=`whoami`
  
#LazyManage path
  
LazyPath=`pwd`
  
#System parameters
  
#BitNum=`getconf LONG_BIT`
  
#SystemNum=`lsb_release -a|grep Release |awk '{print $2}'`
  
}
  
Select_Type() {
  
while true
  
do
  
clear
  
    case $Operate in
  
    1)
  
        Type=`dialog --no-shadow --stdout --backtitle "LazyManage" --title "System work content"  --menu "select" 10 60 0 \
  
        1a "[Common operations]" \
  
        0 "[exit]"`
  
    ;;
  
    2)
  
        Type=`dialog --no-shadow --stdout --backtitle "LazyManage" --title "Custom work content"  --menu "select" 10 60 0 \
  
        1b "[web upgrade]" \
  
        2b "[db   manage]" \
  
        0 "[exit]"`
  
    ;;
  
    0)
  
        echo -e "\e[34mLazyManage exit\e[m"
  
        exit
  
    ;;
  
    esac
  
    [ $? -eq 0 ] && Select_Work $Type || break
  
done
  
}
  
Select_Work() {
  
while true
  
do
  
clear
  
    case $Type in
  
    1a)
  
        Work=`dialog --no-shadow  --stdout --backtitle "LazyManage" --title "Common operations" --menu "select" 20 60 0 \
  
        1aa "[custom cmd ]" \
  
        2aa "[scp file   ]" \
  
        3aa "[exec script]" \
  
        0 "[exit]"`
  
    ;;
  
    1b)
  
        Work=`dialog --no-shadow  --stdout --backtitle "LazyManage" --title "web upgrade" --menu "select" 20 60 0 \
  
        1ba "[job1]" \
  
        2ba "[job2]" \
  
        3ba "[job3]" \
  
        0 "[exit]"`
  
    ;;
  
    2b)
  
        Work=`dialog --no-shadow  --stdout --backtitle "LazyManage" --title "db   manage" --menu "select" 20 60 0 \
  
        1bb "[job1]" \
  
        2bb "[job2]" \
  
        3bb "[job3]" \
  
        0 "[exit]"`
  
    ;;
  
    0)
  
        echo -e "\e[34mLazyManage exit\e[m"
  
        exit
  
    ;;
  
    esac
  
    [ $? -eq 0 ] && Get_Ip $Work || break
  
done
  
}
  
Get_Ip(){
  
while true
  
do
  
clear
  
case $Work in
  
[1-9]a[a-z])
  
    List=`awk '$1!~"^#"&&$1!=""{print $1" "$1" on"}' $ServerList`
  
;;
  
1ba)
  
    List=`awk '$1!~"^#"&&$1!=""&&$2=="job1"&&$3=="web"{print $1" "$2"_"$3" on"}' $ServerList`
  
;;
  
2ba)
  
    List=`awk '$1!~"^#"&&$1!=""&&$2=="job2"&&$3=="web"{print $1" "$2"_"$3" on"}' $ServerList`
  
;;
  
3ba)
  
    List=`awk '$1!~"^#"&&$1!=""&&$2=="job3"&&$3=="web"{print $1" "$2"_"$3" on"}' $ServerList`
  
;;
  
1bb)
  
    List=`awk '$1!~"^#"&&$1!=""&&$2=="job1"&&$3=="db"{print $1" "$2"_"$3" on"}' $ServerList`
  
;;
  
2bb)
  
    List=`awk '$1!~"^#"&&$1!=""&&$2=="job2"&&$3=="db"{print $1" "$2"_"$3" on"}' $ServerList`
  
;;
  
3bb)
  
    List=`awk '$1!~"^#"&&$1!=""&&$2=="job3"&&$3=="db"{print $1" "$2"_"$3" on"}' $ServerList`
  
;;
  
0)
  
    echo -e "\e[34mLazyManage exit\e[m"
  
    exit
  
;;
  
*)
  
    echo "Dialog list does not exist"
  
    break
  
;;
  
esac
  
IpList=`dialog --no-shadow  --stdout --backtitle "LazyManage" --title "ip list" --separate-output --checklist "select IP" 0 60 0 $List |sort -u`
  
if [ "X$IpList" == "X" ];then
  
    break
  
fi
  
Message=`cat &-
  
}
  
Interactive_Auth(){
  
#RemoteRootPasswd=`awk '$1=='$Ip'{print $5}' $ServerList`
  
/usr/bin/expect -c "
  
proc jiaohu {} {
  
    send_user expect_start
  
    expect {
  
        password {
  
            send ${RemotePasswd}\r;
  
            send_user expect_eof
  
            expect {
  
                \"does not exist\" {
  
                    send_user expect_failure
  
                    exit 10
  
                }
  
                password {
  
                    send_user expect_failure
  
                    exit 5
  
                }
  
                Password {
  
                    send ${RemoteRootPasswd}\r;
  
                    send_user expect_eof
  
                    expect {
  
                        incorrect {
  
                            send_user expect_failure
  
                            exit 6
  
                        }
  
                        eof
  
                    }
  
                }
  
                eof
  
            }
  
        }
  
        passphrase {
  
            send ${KeyPasswd}\r;
  
            send_user expect_eof
  
            expect {
  
                \"does not exist\" {
  
                    send_user expect_failure
  
                    exit 10
  
                }
  
                passphrase{
  
                    send_user expect_failure
  
                    exit 7
  
                }
  
                Password {
  
                    send ${RemoteRootPasswd}\r;
  
                    send_user expect_eof
  
                    expect {
  
                        incorrect {
  
                            send_user expect_failure
  
                            exit 6
  
                        }
  
                        eof
  
                    }
  
                }
  
                eof
  
            }
  
        }
  
        Password {
  
            send ${RemoteRootPasswd}\r;
  
            send_user expect_eof
  
            expect {
  
                incorrect {
  
                    send_user expect_failure
  
                    exit 6
  
                }
  
                eof
  
            }
  
        }
  
        \"No route to host\" {
  
            send_user expect_failure
  
            exit 4
  
        }
  
        \"Invalid argument\" {
  
            send_user expect_failure
  
            exit 8
  
        }
  
        \"Connection refused\" {
  
            send_user expect_failure
  
            exit 9
  
        }
  
        \"does not exist\" {
  
            send_user expect_failure
  
            exit 10
  
        }
  

  
        \"Connection timed out\" {
  
            send_user expect_failure
  
            exit 11
  
        }
  
        timeout {
  
            send_user expect_failure
  
            exit 3
  
        }
  
        eof
  
    }
  
}
  
set timeout $TimeOut
  
switch $1 {
  
    Ssh_Cmd {
  
        spawn ssh -t -p $Port -o StrictHostKeyChecking=no $RemoteUser@$Ip /bin/su - $RemoteRootUser -c \\\"$Cmd\\\"
  
        jiaohu
  
    }
  
    Ssh_Script {
  
        spawn scp -P $Port -o StrictHostKeyChecking=no $ScriptPath $RemoteUser@$Ip:/tmp/${ScriptPath##*/};
  
        jiaohu
  
        spawn ssh -t -p $Port -o StrictHostKeyChecking=no $RemoteUser@$Ip /bin/su - $RemoteRootUser -c  \\\"/bin/sh /tmp/${ScriptPath##*/}\\\" ;
  
        jiaohu
  
    }
  
    Scp_File {
  
        spawn scp -P $Port -o StrictHostKeyChecking=no -r $ScpPath $RemoteUser@$Ip:${ScpRemotePath};
  
        jiaohu
  
    }
  
}
  
"
  
case $? in
  
0)    echo -e "\e[32m`date +%Y-%m-%d_%H:%M` $Ip Ssh_Done: ------------------------  OK \e[m"  ;;
  
1|2)  echo -e "failure\n\e[31m`date +%Y-%m-%d_%H:%M` $Ip Ssh_Error: expect grammar or unknown error \e[m"  ;;
  
3)    echo -e "failure\n\e[31m`date +%Y-%m-%d_%H:%M` $Ip Ssh_Error: connection timeout \e[m"  ;;
  
4)    echo -e "failure\n\e[31m`date +%Y-%m-%d_%H:%M` $Ip Ssh_Error: host not found \e[m"  ;;
  
5)    echo -e "failure\n\e[31m`date +%Y-%m-%d_%H:%M` $Ip Ssh_Error: user passwd error \e[m"  ;;
  
6)    echo -e "failure\n\e[31m`date +%Y-%m-%d_%H:%M` $Ip Ssh_Error: root passwd error \e[m"  ;;
  
7)    echo -e "failure\n\e[31m`date +%Y-%m-%d_%H:%M` $Ip Ssh_Error: key passwd error \e[m"  ;;
  
8)    echo -e "failure\n\e[31m`date +%Y-%m-%d_%H:%M` $Ip Ssh_Error: ssh parameter not correct \e[m"  ;;
  
9)    echo -e "failure\n\e[31m`date +%Y-%m-%d_%H:%M` $Ip Ssh_Error: ssh invalid port parameters \e[m"  ;;
  
10)   echo -e "failure\n\e[31m`date +%Y-%m-%d_%H:%M` $Ip Ssh_Error: root user does not exist \e[m"  ;;
  
11)   echo -e "failure\n\e[31m`date +%Y-%m-%d_%H:%M` $Ip Ssh_Error: ssh timeout  \e[m"  ;;
  
esac
  
}
  
trap "" 2 3
  
System_Check $1
  
Set_Variable
  
#Script entrance
  
Operate=`dialog --no-shadow --stdout --backtitle "LazyManage" --title "manipulation menu"  --menu "select" 10 60 0 \
  
1 "[system operate]" \
  
2 "[custom operate]" \
  
0 "[exit]"`
  
[ $? -eq 0 ] && Select_Type $Operate || exit
  
done
  
#End



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-554411-1-1.html 上篇帖子: shell 必备典型脚本 30道 下篇帖子: shell 之 重定向
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表