Practical operation and maintenance script

Disclaimer: This article is a blogger original article, follow the CC 4.0 by-sa copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/u014589856/article/details/96438081

Recently clusters do some maintenance work, we intend to develop some useful scripts to assist cluster management:

1, multi-machine handling and File Copy

Remote operation and bulk copy based sshpass module, a precursor to rely on offline or online mode installation sshpass command on the machine running the script (Python2 to use and seems to have default module integrated into the centos system); there are clusters password for all nodes to be unified, if not uniform slightly different modifications can specify a user account and password to achieve a unified control.

sshUtil.py reads as follows:

#! /usr/bin/python
# -*- coding:UTF-8 -*-
import os
import sys
import commands
import datetime
import re
import argparse
class Run(object):
        def __init__(self,user,host,password):
                self.__host=host
                self.__password=password
                self.__user=user
        def sshdo(self, cmd):
                cmd = "sshpass -p'%s' ssh -o 'StrictHostKeyChecking=no' %s@%s '%s'" %(self.__password,self.__user,self.__host, cmd)
                if os.system(cmd)!=0:
                        print("[err]: "+cmd)
        def sshcopy(self, srcfile, destfile):
                cmd = "sshpass -p'%s' scp -r %s %s@%s:%s" % (self.__password, srcfile, self.__user, self.__host, destfile)
                (status, output) = commands.getstatusoutput(cmd)
                print("[status]"+str(status))
                return status
def runHosts(args):
        showArgs(args)
        user = "root"
        password = "root"
        for line in open(args.f):
                line=line.replace("\n", "").replace("\r", "").strip()
                if not line.startswith("192"):
                        continue
                host = re.split(" |\t",line)[-1]
                print("-----["+host+"]------")
                run = Run(user,host,password)
                if args.cmd=="run":
                        run.sshdo(args.c)
                elif args.cmd=="cp":
                        run.sshcopy(args.s, args.d)
                elif args.cmd=="chage_passwd_time":
                        #通过修改密码过期时间,做到密码防止过期(用于不能修改密码过期策略的集群)
                        run.sshdo("chage -d %s %s" %(datetime.datetime.today().strftime('%Y-%m-%d'),user))
                        run.sshdo("chage -l  %s| head -n 1" %(user))
                else:
                        print("error cmd!!!!!")
def showArgs(args):
        if args.cmd=="run":
                print("[cmd]run:"+args.c)
        elif args.cmd=="cp":
                print("[cmd]cp:"+args.s +" to "+args.d)
        elif args.cmd=="chage_passwd_time":
                print("[cmd]chage_passwd_time")
        else:
                print("error cmd!!!!!")
if __name__ == "__main__" :
        parser = argparse.ArgumentParser(description='run or cp for each node.')
        parser.add_argument('cmd',type=str,help='run cmd[run/cp/chage_passwd_time]')
        parser.add_argument('-s',type=str,help='srcFile [cp]')
        parser.add_argument('-d',type=str,help='destPath [cp]')
        parser.add_argument('-c',type=str,help='cmd  [run]')
        parser.add_argument('-f',type=str,help='hosts file',default='/etc/hosts')
        args = parser.parse_args()
        print("[begin]...............")
        runHosts(args)
        print("[finish]!!!!!!!!!!!!!!")

Use the following demo, which you can also specify the configuration file path to be used by -f

[root@n101 python]# python sshUtil.py --help
usage: sshUtil.py [-h] [-s S] [-d D] [-c C] [-f F] cmd

run or cp for each node.

positional arguments:
  cmd         run cmd[run/cp/chage_passwd_time]

optional arguments:
  -h, --help  show this help message and exit
  -s S        srcFile [cp]
  -d D        destPath [cp]
  -c C        cmd [run]
  -f F        hosts file
[root@n101 python]# python sshUtil.py cp -s /root/shell/python/test -d /root
[begin]...............
[cmd]cp:/root/shell/python/test to /root
-----[n101]------
[status]0
-----[n102]------
[status]0
-----[n103]------
[status]0
[finish]!!!!!!!!!!!!!!
[root@n101 python]# python sshUtil.py run -c "ls -l /root"
[begin]...............
[cmd]run:ls -l /root
-----[n101]------
总用量 171364
-rw-------.  1 root root      1231 6月  16 09:43 anaconda-ks.cfg
drwxr-xr-x.  3 root root       134 7月   9 23:09 shell
drwxr-xr-x.  2 root root        15 7月  18 17:07 test
-----[n102]------
总用量 4
-rw-------. 1 root root 1231 6月  16 09:43 anaconda-ks.cfg
drwxr-xr-x. 2 root root   15 7月  18 17:07 test
-----[n103]------
总用量 4
-rw-------. 1 root root 1231 6月  16 09:43 anaconda-ks.cfg
drwxr-xr-x. 2 root root   15 7月  18 17:07 test
[finish]!!!!!!!!!!!!!!
[root@n101 python]# python sshUtil.py chage_passwd_time
[begin]...............
[cmd]chage_passwd_time
-----[n101]------
最近一次密码修改时间                                    :7月 18, 2019
-----[n102]------
最近一次密码修改时间                                    :7月 18, 2019
-----[n103]------
最近一次密码修改时间                                    :7月 18, 2019
[finish]!!!!!!!!!!!!!!

 

Guess you like

Origin blog.csdn.net/u014589856/article/details/96438081