Steward_Xu

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/1/22 23:18
# @Author  : xuxuedong
# @Site    : 
# @File    : sys.py
# @Software: PyCharm
import os, sys,platform

#for linux
# if platform.system() == "windows":
#     Base_DIR = '\\'.join(os.path.abspath(os.path.dirname(__file__)).split('\\')[:-1])
#     print(Base_DIR)
# else:
#     Base_DIR = '/'.join(os.path.abspath(os.path.dirname(__file__)).split('/')[:-1])
BASE_DIR =os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

print(BASE_DIR)
sys.path.append(BASE_DIR)
from salt.core import Handler
if __name__ == '__main__':
    print(sys.argv)
    t = Handler.ArgvHandler(sys.argv)
    # c = t.val
    # print("t",c)
bin/cmd.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/1/23 9:34
# @Author  : xuxuedong
# @Site    : 
# @File    : ArgvHandler.py
# @Software: PyCharm
import os, sys,datetime,paramiko,time,traceback,threading,logging
import commands,subprocess
from logging import handlers


class ArgvHandler(object):
    def __init__(self,argv_list):
        self.argvs = argv_list
        self.parse_argv()

    def parse_argv(self):
        if len(self.argvs)>5:
            # if hasattr(self,str(self.argvs[1])):
            #     func = getattr(self,str(self.argvs[1]))
            #     func()
            self.argv_ckeck()
            print("get_status",self.servers_file,self.local,self.remote,self.cmd)
            self.server_get()
            # else:
            #     self.help_msg()
        elif len(self.argvs)== 4:
            self.argv_ckeck()
            print("cmd",self.cmd )

            self.cmd = self.cmd.strip().split(',')
            print("cmd_list",self.cmd)
            self.server_get()
            # else:
        else:
            self.help_msg()
    def server_get(self):
        self.get_servers(self.servers_file)
        print("self.SERVERS",self.SERVERS)
        # self.ssh_connect(self.SERVERS)
        self.val = []
        for server in self.SERVERS:
            print("server_list",server['host'])
            self.ip = server['host']
            user = server['user']
            passwd = server['passwd']
            port = server['port']
            print("val",self.ip,user,passwd,port)
            t1 = threading.Thread(target=self.ssh_connect,args=[self.ip,port,user,passwd,self.cmd])
            t1.start()
            t1.join()
        #     print("getname:",t1.getName())
        #     self.val.append(t1)
        # for i in self.val:
        #     print("i",i)
        #     i.join()
        # print("---main---")
        # print("self.val",self.val)
        # return self.val
    def logger(self,log_message):
        self.log = logging.getLogger(__name__)
        self.log.setLevel(logging.INFO)
        # create console handler and set level to debug
        ch = logging.StreamHandler()
        ch.setLevel(logging.INFO)
        ti = time.strftime('%Y-%m-%d',time.localtime(time.time()))
        log_file = os.getcwd()+"/"+"logs/"+ti+"-"+"access.log"
        # create file handler an"d set level to warning
        if log_file:
            self.fh = logging.FileHandler(log_file)
            self.fh.setLevel(logging.WARNING)
        else:
            self.fh = handlers.TimedRotatingFileHandler(filename=log_file,when="S",interval=5,backupCount=3)
            self.fh.setLevel(logging.WARNING)
        # create formatter
        self.formatter = logging.Formatter('%(asctime)s %(module)s:%(lineno)d %(message)s')
        self.fh.setFormatter(self.formatter)
        self.log.addHandler(self.fh)
        self.log.error(log_message)
        self.log.warning(log_message)
        self.log.info(log_message)
    def help_msg(self):
        msg = '''
        -f
            1.upload file use "up"
                example: python cmd.py  -f server.txt upload_file_name  remote_file_name up
            2.download file use "get"
                example: python cmd.py  -f server.txt download_file_name  remote_file_name up
            3.remote_file_name must path
            example :
            python cmd.py  -f server.txt auto.mysql.sh /service/script/aouto.mysql.sh get
            python cmd.py  -f server.txt auto.mysql.sh /service/script/aouto.mysql.sh up
        -c
            1.Host batch execution command
             example: python cmd.py  -c server.txt "ls -l /service/script,uname -a"
        server.text format:lists_format:192.168.223.128,root,123456,22,1(ip,user,passwd,port,status)
        '''
        print(msg)
    def argv_ckeck(self):
        logs_path = os.getcwd() + u'/logs'
        if os.path.exists(logs_path):
            if os.path.isfile(logs_path):
                print u"日志目录" + logs_path + u"存在,但是文件,终止"
                sys.exit(1)
        else:
            os.mkdir(logs_path)
        if sys.argv[1] == "-f":
            self.servers_file = sys.argv[2]
            self.local = sys.argv[3]
            self.remote = sys.argv[4]
            self.cmd = sys.argv[5]
        if sys.argv[1] == "-c":
            self.servers_file = sys.argv[2]
            self.cmd = sys.argv[3]
            print(self.cmd)
        # except:
        #     pass
        #     # self.help_msg()
        #

    def get_servers(self,file):
        print("file:",file)
        self.SERVERS = []
        try:
            with open(file, 'r') as file:
                while True:
                    line = file.readline()
                    if not line:
                        break
                    try:
                        res = line.strip().split(',')
                        server = {
                            'host': res[0],
                            'user': res[1],
                            'passwd': res[2],
                            'port': int(res[3]),
                            # 'status': int(res[4])
                        }
                        self.SERVERS.append(server)
                    except:
                        self.logger(self.SERVERS)
            return self.SERVERS
        except:
            pass
            # out_log = u"failed to read server lists file:{0}\n{1}".format(file, traceback.format_exc())
            # self.logger(out_log, 'error')
            # sys.exit(1)
    def upload__callback(self,trans, total):
        # sys.stdout.write('Data Transmission %10d [%3.2f%%]\r' %(a, a*100/int(b)))
        # sys.stdout.flush()
        print 'Data upload %10d [%3.2f%%]\r' % (trans, trans*100/int(total)),;
        if trans >= total:
            self.logger('Data Transmission %10d [100.00%%]' % trans, 'info', False)
        # 和上面一样:作用始终保持同一行,即新打印出来的一行,替换掉之前的那行,保持同一行,不换行。
    def download__callback(self,trans, total):
        # sys.stdout.write('Data Transmission %10d [%3.2f%%]\r' %(a, a*100/int(b)))
        # sys.stdout.flush()
        print 'Data download %10d [%3.2f%%]\r' % (trans, trans*100/int(total)),;
        if trans >= total:
            self.logger('Data Transmission %10d [100.00%%]' % trans, 'info', False)
        # 和上面一样:作用始终保持同一行,即新打印出来的一行,替换掉之前的那行,保持同一行,不换行。
    def ssh_connect(self,host, port, user, passwd,status):
        self.ssh = paramiko.SSHClient()
        try:
            #paramiko.util.log_to_file('logs/params.log')
            self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())  # 允许连接不在know_hosts文件中的主机
            # ssh.connect(host, port, user, passwd, timeout=5, banner_timeout=5, auth_timeout=5)
            self.ssh.connect(host, port, user, passwd, timeout=3, banner_timeout=3)
            # print("self.ssh",self.ssh)
            if status=='up':
                obj = getattr(self,'up'+ '_file')
                obj(self.ssh,self.local,self.remote)
                print("obj",obj)
                print("status",status)
            elif status=='get':
                obj = getattr(self,'get'+ '_file')
                obj(self.ssh,self.ip,self.remote)
                print("obj",obj)
                print("status",status)
            else:
                print("command is:",status)
                for item in status:
                    print("item",item)
                    stdin, stdout, stderr = self.ssh.exec_command(item)
                    self.err_list = stderr.readlines()
                    for item in stdout.readlines():
                        # print item,
                        print item
                    if len( self.err_list ) > 0:
                        print 'ERROR:' + self.err_list[0]
                        exit()
        except Exception as e:
            out_log = u'ip: {0}\tuser:{1}\ttimeout'.format(host,user)
            err_log = traceback.format_exc()
            out_log = u'{0}\n{1}'.format(out_log, err_log)
            self.logger(out_log)
            # exit()

            # self.ssh.close()
        # return self.ssh
    # def ssh_exec_cmd(self,ssh,cmd ):
    #     return self.ssh.exec_command(self,cmd)

    def up_file(self,ssh, localfile, remotefile):

        try:
            print("obj is up")
            sftp = ssh.open_sftp()
            print("localfile",localfile)
            print("remotefile",remotefile)

            sftp.put(localfile, remotefile)
            sftp.close()
        except:
            out_log = u'ip: {0}\tuser:{1}\ttimeout'.format(ssh,remotefile)
            err_log = traceback.format_exc()
            out_log = u'{0}\n{1}'.format(out_log, err_log)
            self.logger(out_log)
        # self.ssh.close()

    def get_file(self,ssh, ip, remotefile):
        if len(remotefile.split('/', 10 )) > 2:
            self.File = remotefile.split('/', 10 )[len(remotefile.split('/', 10 ))-1]
            print("FILE",self.File)
        try:
            sftp = ssh.open_sftp()
            sftp.get(remotefile,self.ip + self.File)
            sftp.close()
        except:
            out_log = u'local: {0}\tremot:{1}\ttimeout'.format(self.ip,remotefile)
            err_log = traceback.format_exc()
            ut_log = u'{0}\n{1}'.format(out_log, err_log)
            self.logger(out_log)

        # self.ssh.clos 
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/1/23 9:34
# @Author  : xuxuedong
# @Site    : 
# @File    : ArgvHandler.py
# @Software: PyCharm
import os, sys,datetime,paramiko,time,traceback,threading,logging
import commands,subprocess
from logging import handlers


class ArgvHandler(object):
    def __init__(self,argv_list):
        self.argvs = argv_list
        self.parse_argv()

    def parse_argv(self):
        if len(self.argvs)>5:
            # if hasattr(self,str(self.argvs[1])):
            #     func = getattr(self,str(self.argvs[1]))
            #     func()
            self.argv_ckeck()
            print("get_status",self.servers_file,self.local,self.remote,self.cmd)
            self.server_get()
            # else:
            #     self.help_msg()
        elif len(self.argvs)== 4:
            self.argv_ckeck()
            print("cmd",self.cmd )

            self.cmd = self.cmd.strip().split(',')
            print("cmd_list",self.cmd)
            self.server_get()
            # else:
        else:
            self.help_msg()
    def server_get(self):
        self.get_servers(self.servers_file)
        print("self.SERVERS",self.SERVERS)
        # self.ssh_connect(self.SERVERS)
        self.val = []
        for server in self.SERVERS:
            print("server_list",server['host'])
            self.ip = server['host']
            user = server['user']
            passwd = server['passwd']
            port = server['port']
            print("val",self.ip,user,passwd,port)
            t1 = threading.Thread(target=self.ssh_connect,args=[self.ip,port,user,passwd,self.cmd])
            t1.start()
            # t1.join()

            print("getname:",t1.getName())
            self.val.append(t1)
        for list in self.val:
            print("i",list)
            list.join()
        print("---main---")
        print("self.val",self.val)
        return self.val
    def logger(self,log_message):
        self.log = logging.getLogger(__name__)
        self.log.setLevel(logging.INFO)
        # create console handler and set level to debug
        ch = logging.StreamHandler()
        ch.setLevel(logging.INFO)
        ti = time.strftime('%Y-%m-%d',time.localtime(time.time()))
        log_file = os.getcwd()+"/"+"logs/"+ti+"-"+"access.log"
        # create file handler an"d set level to warning
        if log_file:
            self.fh = logging.FileHandler(log_file)
            self.fh.setLevel(logging.WARNING)
        else:
            self.fh = handlers.TimedRotatingFileHandler(filename=log_file,when="S",interval=5,backupCount=3)
            self.fh.setLevel(logging.WARNING)
        # create formatter
        self.formatter = logging.Formatter('%(asctime)s %(module)s:%(lineno)d %(message)s')
        self.fh.setFormatter(self.formatter)
        self.log.addHandler(self.fh)
        self.log.error(log_message)
        self.log.warning(log_message)
        self.log.info(log_message)
    def help_msg(self):
        msg = '''
        -f
            1.upload file use "up"
                example: python cmd.py  -f server.txt upload_file_name  remote_file_name up
            2.download file use "get"
                example: python cmd.py  -f server.txt download_file_name  remote_file_name up
            3.remote_file_name must path
            example :
            python cmd.py  -f server.txt auto.mysql.sh /service/script/aouto.mysql.sh get
            python cmd.py  -f server.txt auto.mysql.sh /service/script/aouto.mysql.sh up
        -c
            1.Host batch execution command
             example: python cmd.py  -c server.txt "ls -l /service/script,uname -a"
        server.text format:lists_format:192.168.223.128,root,123456,22,1(ip,user,passwd,port,status)
        '''
        print(msg)
    def argv_ckeck(self):
        logs_path = os.getcwd() + u'/logs'
        if os.path.exists(logs_path):
            if os.path.isfile(logs_path):
                print u"日志目录" + logs_path + u"存在,但是文件,终止"
                sys.exit(1)
        else:
            os.mkdir(logs_path)
        if sys.argv[1] == "-f":
            self.servers_file = sys.argv[2]
            self.local = sys.argv[3]
            self.remote = sys.argv[4]
            self.cmd = sys.argv[5]
        if sys.argv[1] == "-c":
            self.servers_file = sys.argv[2]
            self.cmd = sys.argv[3]
            print(self.cmd)
        # except:
        #     pass
        #     # self.help_msg()
        #

    def get_servers(self,file):
        print("file:",file)
        self.SERVERS = []
        try:
            with open(file, 'r') as file:
                while True:
                    line = file.readline()
                    if not line:
                        break
                    try:
                        res = line.strip().split(',')
                        server = {
                            'host': res[0],
                            'user': res[1],
                            'passwd': res[2],
                            'port': int(res[3]),
                            # 'status': int(res[4])
                        }
                        self.SERVERS.append(server)
                    except:
                        self.logger(self.SERVERS)
            return self.SERVERS
        except:
            pass
            # out_log = u"failed to read server lists file:{0}\n{1}".format(file, traceback.format_exc())
            # self.logger(out_log, 'error')
            # sys.exit(1)
    def upload__callback(self,trans, total):
        # sys.stdout.write('Data Transmission %10d [%3.2f%%]\r' %(a, a*100/int(b)))
        # sys.stdout.flush()
        print 'Data upload %10d [%3.2f%%]\r' % (trans, trans*100/int(total)),;
        if trans >= total:
            self.logger('Data Transmission %10d [100.00%%]' % trans, 'info', False)
        # 和上面一样:作用始终保持同一行,即新打印出来的一行,替换掉之前的那行,保持同一行,不换行。
    def download__callback(self,trans, total):
        # sys.stdout.write('Data Transmission %10d [%3.2f%%]\r' %(a, a*100/int(b)))
        # sys.stdout.flush()
        print 'Data download %10d [%3.2f%%]\r' % (trans, trans*100/int(total)),;
        if trans >= total:
            self.logger('Data Transmission %10d [100.00%%]' % trans, 'info', False)
        # 和上面一样:作用始终保持同一行,即新打印出来的一行,替换掉之前的那行,保持同一行,不换行。
    def ssh_connect(self,host, port, user, passwd,status):
        ssh = paramiko.SSHClient()
        try:
            #paramiko.util.log_to_file('logs/params.log')
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())  # 允许连接不在know_hosts文件中的主机
            # ssh.connect(host, port, user, passwd, timeout=5, banner_timeout=5, auth_timeout=5)
            ssh.connect(host, port, user, passwd, timeout=3, banner_timeout=3)
            # print("ssh",ssh)
            if status=='up':
                obj = getattr(self,'up'+ '_file')
                obj(ssh,self.local,self.remote)
                print("obj",obj)
                print("status",status)
            elif status=='get':
                obj = getattr(self,'get'+ '_file')
                obj(ssh,self.ip,self.remote)
                print("obj",obj)
                print("status",status)
            else:
                print("command is:",status)
                for item in status:
                    print("item",item)
                    stdin, stdout, stderr = ssh.exec_command(item)
                    self.err_list = stderr.readlines()
                    for item in stdout.readlines():
                        # print item,
                        print item
                    if len( self.err_list ) > 0:
                        print 'ERROR:' + self.err_list[0]
                        exit()

        except Exception as e:
            out_log = u'ip: {0}\tuser:{1}\ttimeout'.format(host,user)
            err_log = traceback.format_exc()
            out_log = u'{0}\n{1}'.format(out_log, err_log)
            self.logger(out_log)
            # exit()
        time.sleep(10)
    def up_file(self,ssh, localfile, remotefile):

        try:
            print("obj is up")
            sftp = ssh.open_sftp()
            print("localfile",localfile)
            print("remotefile",remotefile)

            sftp.put(localfile, remotefile)
            sftp.close()
        except:
            out_log = u'ip: {0}\tuser:{1}\ttimeout'.format(ssh,remotefile)
            err_log = traceback.format_exc()
            out_log = u'{0}\n{1}'.format(out_log, err_log)
            self.logger(out_log)

    def get_file(self,ssh, ip, remotefile):
        if len(remotefile.split('/', 10 )) > 2:
            self.File = remotefile.split('/', 10 )[len(remotefile.split('/', 10 ))-1]
            print("FILE",self.File)
        try:
            sftp = ssh.open_sftp()
            sftp.get(remotefile,self.ip + self.File)
            sftp.close()
        except:
            out_log = u'local: {0}\tremot:{1}\ttimeout'.format(self.ip,remotefile)
            err_log = traceback.format_exc()
            ut_log = u'{0}\n{1}'.format(out_log, err_log)
            self.logger(out_log)
core/Handler.py

server.txt文件:

172.22.0.60,root,P@ssw0rd,22  (主机、用户名、密码、端口)
172.22.0.225,root,P@ssw0rd,22

执行命令:查看主机名以及主机ip为例:

python cmd.py -c server.txt "hostname , ifconfig eth0 | grep 'inet addr' | awk '{ print $2}' | awk -F: '{print $2}'"

上传下载命令:

python cmd.py  -f server.txt auto.mysql.sh /service/script/aouto.mysql.sh get
python cmd.py -f server.txt auto.mysql.sh /service/script/aouto.mysql.sh up

 

posted on 2018-03-19 17:08  Steward_Xu  阅读(1988)  评论(0编辑  收藏  举报