Пример #1
0
def useradd(user_name, arg=""):  
    '''''  
    useradd封装,添加新用户;  
    linux和windows系统分别使用不同方法;  
    在linux中,arg填写新用户的gid;  
    windows中,arg填写新用户的密码。  
    ''' 
    os_type = platform.system()  
    if os_type == "Linux":  
        str_cmd = "/usr/bin/id %s" % user_name  
        status, result = getso(str_cmd)  
        if status == 0:  
            return 
        if not arg:  
            str_cmd = "/usr/sbin/useradd %s" % user_name  
        else:  
            str_cmd = "/usr/sbin/useradd -g %s %s" % (arg, user_name)  
        status, result = getso(str_cmd)  
        wr_log(str_cmd, status, result)  
    elif os_type == "Windows":  
        try:  
            import win32netcon,win32net,wmi  
            for use in wmi.WMI().Win32_UserAccount():  
                if use.name == user_name:  
                    raise Exception, "user %s is already exists" % user_name  
            udata = {}  
            udata["name"] = user_name  
            udata["password"] = arg  
            udata["flags"] = win32netcon.UF_NORMAL_ACCOUNT | win32netcon.UF_SCRIPT  
            udata["priv"] = win32netcon.USER_PRIV_USER  
            win32net.NetUserAdd(None, 1, udata)  
            wr_log("add user %s" % user_name)  
        except Exception,e:  
            wr_log("add user %s" % user_name, 1, e)  
Пример #2
0
def reset_sambo():
    print 'input sambo passwd..\n'
    s,o = getso("smbpasswd -a %s" %HOST_NAME)
    handle(s,o)
    print 'reset samba server...'
    s,o = getso("service smbd restart")
    handle(s,o)
Пример #3
0
def reset_sambo():
    print 'input sambo passwd..\n'
    s, o = getso("smbpasswd -a %s" % HOST_NAME)
    handle(s, o)
    print 'reset samba server...'
    s, o = getso("service smbd restart")
    handle(s, o)
Пример #4
0
def install_netdriver():
    if  NET_DRIVER_NAME != '': 
        print 'intall NET Driver...\n'
        s,o = getso("cd %s && make clean && make install && modprobe %s  " %(NET_DRIVER_PATH,NET_DRIVER_NAME))
        handle(s,o)
    
    s,o = getso("ifconfig eth0 %s " %IP_ADDRESS)
    handle(s,o)
Пример #5
0
def do():
    fd = open('/etc/apt/apt.conf','w')
    fd.write('%s' %NET_AGENCY)
    fd.close()

    s,o = getso("apt-get update")

    handle(s,o)

    s,o = getso("apt-get upgrade -y")

    handle(s,o)
Пример #6
0
def do():
    fd = open('/etc/apt/apt.conf', 'w')
    fd.write('%s' % NET_AGENCY)
    fd.close()

    s, o = getso("apt-get update")

    handle(s, o)

    s, o = getso("apt-get upgrade -y")

    handle(s, o)
Пример #7
0
def get_pid(arg):  
    '''''  
    linux中,查找进程并返回pid。  
    windows中,查找进程或端口返回pid。  
    ''' 
    os_type = platform.system()  
    if os_type == "Linux":  
        str_cmd = "/bin/ps auxww | grep '%s' | grep -v grep | awk '{print $2}'" % arg  
        status, result = getso(str_cmd)  
        return result  
    elif os_type == "Windows":  
        if type(arg) == int:  
            str_cmd =  "netstat -ano|find \"%s\""%arg  
            try:  
                result = os.popen(str_cmd,"r").read()  
                result = result.split("\n")[0].strip()  
                if result.find("WAIT") != -1:  
                    return 0 
                pid = int(result[result.rfind(" "):].strip())  
                return [pid]  
            except Exception, e:  
                return 0 
        else:  
            import win32con,win32api,win32process  
            pids = []  
            for pid in win32process.EnumProcesses():  
                try:  
                    hProcess = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, False, pid)  
                    hProcessFirstModule = win32process.EnumProcessModules(hProcess)[0]  
                    processName = os.path.splitext(os.path.split(win32process.GetModuleFileNameEx(hProcess, hProcessFirstModule))[1])[0]  
                    if processName == arg:  
                        pids.append(pid)  
                except Exception, e:  
                    pass 
            return pids  
Пример #8
0
def echo(str, file_name):  
    '''''  
    linux适用  
   
    echo封装,添加字符串到文件尾部  
    ''' 
    str_cmd = "/bin/echo '%s' >> %s" % (str, file_name)  
    status, result = getso(str_cmd)  
    wr_log(str_cmd, status, result)  
Пример #9
0
def md5(file_name):  
    '''''  
    linux适用  
   
    md5sum -c 封装,校验md5文件,返回校验成功或失败状态  
    ''' 
    str_cmd="/usr/bin/md5sum -c %s" % file_name  
    status,result=getso(str_cmd)  
    return status  
Пример #10
0
def hostname(host_name):  
    '''''  
    linux适用  
       
    hostname封装,修改主机名。  
    ''' 
    str_cmd = "/bin/sed -i 's/HOSTNAME/#&/;$a HOSTNAME=%s' /etc/sysconfig/network;/bin/hostname %s" % (host_name,host_name)  
    status, result = getso(str_cmd)  
    wr_log(str_cmd, status, result)  
Пример #11
0
def touch(src):  
    '''''  
    linux适用  
       
    touch封装,新建空白文件。  
    ''' 
    str_cmd = "/bin/touch %s" % src  
    status, result = getso(str_cmd)  
    wr_log(str_cmd, status, result)  
Пример #12
0
def grpadd(grp_name):  
    '''''  
    linux适用  
       
    groupadd封装,增加组。  
    ''' 
    str_cmd = "/usr/sbin/groupadd %s" % grp_name  
    status, result = getso(str_cmd)  
    wr_log(str_cmd, status, result)  
Пример #13
0
def tar(dst, src):  
    '''''  
    linux适用  
       
    tar封装,压缩文件。  
       
    例子:  
    tar("/tmp/test.tgz", "/tmp/test.txt")  
    ''' 
    str_cmd = "/bin/tar zcf %s %s" % (dst, src)  
    status, result = getso(str_cmd)  
    wr_log(str_cmd, status, result)  
Пример #14
0
def chmod(num, file_name):  
    '''''  
    linux适用  
       
    chmod封装,修改文件权限。  
    需要传入一个八进制数以及文件名。  
       
    例子:  
    chmod(644, "/tmp/test.txt")  
    ''' 
    str_cmd = "/bin/chmod %s %s" % (num, file_name)  
    status, result = getso(str_cmd)  
    wr_log(str_cmd, status, result)  
Пример #15
0
def userdel(user_name):  
    '''''  
    userdel封装,删除用户。  
    ''' 
    os_type = platform.system()  
    if os_type == "Linux":  
        str_cmd = "/usr/bin/id %s" % user_name  
        status, result = getso(str_cmd)  
        if status == 0:  
            str_cmd = "/usr/sbin/userdel -r %s" % user_name  
            status, result = getso(str_cmd)  
            wr_log(str_cmd, status, result)  
    elif os_type == "Windows":  
        try:  
            import win32net,wmi  
            for use in wmi.WMI().Win32_UserAccount():  
                if use.name == user_name:  
                    win32net.NetUserDel(None,user_name)  
                    wr_log("del user %s" % user_name)  
                    return 
            wr_log("user %s not exists" % user_name)  
        except Exception,e:  
            wr_log("del user %s" % user_name, 1, e) 
Пример #16
0
def chown(user, file_name, arg=""):  
    '''''  
    linux适用  
       
    chown封装,修改文件属主属组。  
       
    例子:  
    chown("nobody.nobody", "/tmp", "r")  
    ''' 
    if arg == "r":  
        str_cmd = "/bin/chown -R %s %s" % (user, file_name)  
    else:  
        str_cmd = "/bin/chown %s %s" % (user, file_name)  
    status, result = getso(str_cmd)  
    wr_log(str_cmd, status, result)  
Пример #17
0
def passwd(user_name,newpass):  
    '''''  
    passwd封装,修改用户密码  
    ''' 
    os_type = platform.system()  
    if os_type == "Linux":  
        str_cmd = "echo '%s' | passwd %s --stdin" % (newpass, user_name)  
        status, result = getso(str_cmd)  
        wr_log(str_cmd, status, result)  
    elif os_type == "Windows":  
        try:  
            if os.system('net user %s "%s" ' %(user_name,newpass)) == 0:  
                wr_log("modify passwd for %s  " % user_name)  
            elif os.system('net user %s "%s" ' %(user_name,newpass)) == 2:  
                raise Exception, "user %s isnot exists" % user_name  
        except Exception,e:  
            wr_log("modify passwd for %s " % user_name, 1, e)  
Пример #18
0
def get_pids(name):
    '''get pids of a process'''
    os_type = platform.system()
    if os_type in ('Linux', 'Darwin'):
        s_cmd = "/bin/ps auxww | grep %s | grep -v grep | awk '{print $2}'" % name
        status, result = getso(s_cmd)
        print(status, result)
        if status == 0 and result:
            return ' '.join(result.split()).split(' ')  # list
        else:
            return []
    elif os_type == 'Windows':
        if type(name) == int:
            str_cmd = "netstat -ano|find \"%s\"" % name
            try:
                result = os.popen(str_cmd, 'r').read()
                result = result.split('\n')[0].strip()
                if result.find('WAIT') != -1:
                    return 0
                pid = int(result[result.rfind(' '):].strip())
                return [pid]
            except Exception, e:
                return 0
        else:
            import win32con
            import win32api
            import win32process
            pids = []
            for pid in win32process.EnumProcesses():
                try:
                    hProcess = win32api.OpenProcess(
                        win32con.PROCESS_ALL_ACCESS, False, pid)
                    hProcessFirstModule = win32process.EnumProcessModules(
                        hProcess)[0]
                    processName = os.path.splitext(
                        os.path.split(
                            win32process.GetModuleFileNameEx(
                                hProcess, hProcessFirstModule))[1])[0]
                    if processName == name:
                        pids.append(pid)
                except Exception, e:
                    pass
            return pids
Пример #19
0
def chkconfig(type, svr_name, switch=""):  
    '''''  
    linux适用  
       
    chkconfig封装,根据传入的type参数执行相应操作,type可以为以下几种:  
    add  添加服务至启动项;  
    del  从启动项删除服务;  
    数字  指定运行级别的服务开启或关闭。  
       
    type及svr_name为必需的参数。  
       
    例子:  
    开启运行级别3的sshd服务:chkconfig("3", "sshd", "on")  
    ''' 
    if type != "add" and type != "del":  
        type = "--level %s" % str(type)  
    str_cmd = "/sbin/chkconfig %s %s %s" % (type, svr_name, switch)  
    status, result = getso(str_cmd)  
    wr_log(str_cmd, status, result)  
Пример #20
0
def do():
    fd = open("app.txt")
    files = fd.readlines()
    fd.close()

    cmd = "rpm -ivh  "

    for p in files:
        p = p.strip()
        if p:
            s=p.split(" ")[0]
            t=p.split(" ")[1]
            f = s+'-'+t
            install = cmd + APP_PACKAGE_DIR + '/'  + f +'.'+SUFFIX         
            print install + '\n'
            s,o = getso(cmd)
#    handle(s,o)
        else:
            continue
Пример #21
0
def kill(arg):  
    '''''  
    linux中,查找进程并杀死返回的pid。  
    windows中,查找进程或端口并杀死返回的pid。  
    ''' 
    pid = get_pid(arg)  
    os_type = platform.system()  
    if os_type == "Linux":  
        if pid:  
            str_cmd = "/bin/kill -9 %s" % pid  
            status, result = getso(str_cmd)  
            wr_log("kill %s" % arg, status, result)  
    elif os_type == "Windows":  
        if pid:  
            try:  
                import ctypes  
                for i in pid:  
                    handle = ctypes.windll.kernel32.OpenProcess(1, False, i)  
                    ctypes.windll.kernel32.TerminateProcess(handle,0)  
                wr_log("kill %s" % arg)  
            except Exception, e:  
                wr_log("kill %s" % arg, 1, e)  
Пример #22
0
def do():
    fd = open("package.txt")
    files = fd.readlines()
    fd.close()

    cmd = "apt-get install -y "

    for p in files:
        p = p.strip()
        if p:
            fd = open(PBASE + '/' + p)
            packages = fd.readlines()
            fd.close()
            for package in packages:
                cmd = cmd + package.strip() + ' '
        else:
            continue

    from commands import getstatusoutput as getso

    s, o = getso(cmd)

    from handle_error import handle
    handle(s, o)
Пример #23
0
def kill_pids(pids):
    '''kill process by pids'''
    if not pids:
        return None
    if isinstance(pids, list):
        pids = ' '.join(pids)  # to string
    os_type = platform.system()
    if os_type in ('Linux', 'Darwin'):
        s_cmd = '/bin/kill -9 %s' % pids
        status, result = getso(s_cmd)
        # print('kill_pids', status, result)
        if status == 0:
            return True
        else:
            return False
    elif os_type == 'Windows':
        try:
            import ctypes
            for i in pids:
                handle = ctypes.windll.kernel32.OpenProcess(1, False, i)
                ctypes.windll.kernel32.TerminateProcess(handle, 0)
            return True
        except Exception, e:
            return False
Пример #24
0
def do():
    fd = open("package.txt")
    files = fd.readlines()
    fd.close()

    cmd = "apt-get install -y "

    for p in files:
        p = p.strip()
        if p:
            fd = open(PBASE + '/' + p)
            packages = fd.readlines()
            fd.close()
            for package in packages:
                    cmd = cmd + package.strip() + ' '
        else:
            continue

    from commands import getstatusoutput as getso

    s,o = getso(cmd)

    from handle_error import handle
    handle(s,o)
Пример #25
0
def reset_tftp():
    print 'reset tftp server...\n'
    s,o = getso("/etc/init.d/xinetd reload")
    handle(s,o)
    s,o = getso("service xinetd restart")
    handle(s,o)
Пример #26
0
def reset_nfs():
    print 'reset nfs server...\n'
    s,o = getso("exportfs -rv")
    handle(s,o)
    s,o = getso("service nfs restart")
    handle(s,o)
Пример #27
0
def clone_config():
    s, o = getso("cp -rf %s/* %s" % (ENV_URL, BASE))
    handle(s, o)
Пример #28
0
def generate_sshkey():
    print 'configure ssh-key...\n'
    s, o = getso("ssh-keygen -t dsa -C %s -f ~/.ssh/%s" % (EMAIL, EMAIL))
    handle(s, o)
Пример #29
0
def clone_config():
    s,o = getso("git clone %s %s" % (ENV_URL, BASE))
    handle(s,o)
Пример #30
0
def generate_sshkey():
    print 'configure ssh-key password...\n'
    s,o = getso("ssh-keygen -t dsa -C %s -f ~/.ssh/%s"  %(EMAIL,EMAIL))
    handle(s,o)
Пример #31
0
def clone_config():
    s,o = getso("cp -rf %s/* %s" % (ENV_URL, BASE))
    handle(s,o)
Пример #32
0
def reset_tftp():
    print 'reset tftp server...\n'
    s, o = getso("/etc/init.d/xinetd reload")
    handle(s, o)
    s, o = getso("service xinetd restart")
    handle(s, o)
Пример #33
0
def reset_nfs():
    print 'reset nfs server...\n'
    s, o = getso("exportfs -rv")
    handle(s, o)
    s, o = getso("service nfs-kernel-server restart")
    handle(s, o)