来自 编程应用 2019-09-04 19:47 的文章
当前位置: 六合联盟网 > 编程应用 > 正文

python实现linux服务器批量修改密码并生成execl,

复制代码 代码如下:

批量修改linux服务器密码,同时生成execl表格

#!/usr/bin/env python
import sys,time
from socket import socket
def read_interface(in_file):
    with file(in_file) as f:
        return f.readlines()[2:]
def set_interface(inter_msg):
    dic={}
    for i in xrange(len(inter_msg)):
        dic[inter_msg[i].split(":")[0].strip()]={"in":inter_msg[i].split(":")[1].strip().split()[0],"out":inter_msg[i].split(":")[1].strip().split()[8]}
    return dic
CARBON_SERVER = '127.0.0.1'
CARBON_PORT = 2003
delay = 3
sock = socket()
try:
    sock.connect( (CARBON_SERVER,CARBON_PORT) )
except:
    print "Couldn't connect to %(server)s on port %(port)d, is carbon-agent.py running?" % { 'server':CARBON_SERVER, 'port':CARBON_PORT }
    sys.exit(1)
while True:
    now = int( time.time() )
    lines=[]
    int1=read_interface("/proc/net/dev")
    inter_dic1=set_interface(int1)
    time.sleep(1)
    int2=read_interface("/proc/net/dev")
    inter_dic2=set_interface(int2)
    print int(inter_dic2[inter_dic2.keys()[1]]["in"]),int(inter_dic1[inter_dic1.keys()[1]]["in"])
    for i in xrange(len(inter_dic1.keys())):
        lines.append("interface.%s_in %s %d" % (inter_dic1.keys()[i],int(inter_dic2[inter_dic2.keys()[i]]["in"])-int(inter_dic1[inter_dic1.keys()[i]]["in"]),now))
        lines.append("interface.%s_out %s %d" % (inter_dic1.keys()[i],int(inter_dic2[inter_dic2.keys()[i]]["out"])-int(inter_dic1[inter_dic1.keys()[i]]["out"]),now))
    message = 'n'.join(lines) + 'n'
    print "sending messagen"
    print '-' * 80
    print message
    sock.sendall(message)
    time.sleep(delay)

复制代码 代码如下:

代码如下: #!/usr/bin/env python import sys,time from socket import socket def read_interface(in_file): with file(in_file) as f: return f.readlines()[2:] def set_in...

#!/usr/bin/env python
#coding:utf8
#随机生成自定义长度密码
from random import choice
import string,pickle

def GenPassword(length=8,chars=string.ascii_letters+string.digits):
    return ''.join([choice(chars) for i in range(length)])
def passlist(r_user,c_user,ip_list,web_list):
    dic={}
    for i in ip_list:
        if i.split()[1] in web_list:
            dic[i.split()[1]]=[i.split()[0],{r_user:GenPassword(32),c_user:GenPassword(32)}]
        else:
            dic[i.split()[1]]=[i.split()[0],{r_user:GenPassword(32)}]
    return dic
def ip_list(ip_file):
    with open(ip_file) as file:
        ip_file=file.read().strip().split("n")
        #web_list=[i for i in ip_file if i in web_list]
        return ip_file
def save_dic():
    dic=main()
    with open("../host_message.pickle","w") as fd:
        pickle.dump(dic,fd)
def main():
    file="/root/cui/script/python/ip.txt"
    web_list=[ip1,ip2,ip3,ip4]
    file_list=ip_list(file)
    file_dic_pass=passlist("root","www",file_list,web_list)
    return file_dic_pass

if __name__=="__main__":
    save_dic()

复制代码 代码如下:

#!/usr/bin/env python
#coding:utf8
#批量修改密码主程序
import pickle
from ssh_co.ssh_connect import sshd
from command.ip_passwd import save_dic,main
from execl import set_execl
def read_dic():
    with open("host_message.pickle","rb") as f:
        return pickle.load(f)
def ssh_main():
    pid_host=1
    host_message=read_dic()
    host_list=host_message.keys()
    print host_list
    for host_msg in xrange(len(host_list)):
        host_attribute=(host_list[host_msg],port,user,passwd)
        ssh_conn=sshd(host_attribute,pid_host)
        if "www" in host_message[host_list[host_msg]][1].keys():
            for username in xrange(len(host_message[host_list[host_msg]][1].keys())):
                try:
                    user=host_message[host_list[host_msg]][1].keys()[username]
                    passwd=host_message[host_list[host_msg]][1][host_message[host_list[host_msg]][1].keys()[username]]
                    results=ssh_conn.set_ssh_cmd(u"/bin/echo %s|/usr/bin/passwd --stdin %s" %(passwd,user))
                    print results,host_list[host_msg],user+"-----------"+passwd
                except:
                    print results,host_list[host_msg],user+"-----except------"+passwd
        else:
            user=host_message[host_list[host_msg]][1].keys()[0]
            passwd=host_message[host_list[host_msg]][1][host_message[host_list[host_msg]][1].keys()[0]]
            result=ssh_conn.set_ssh_cmd(u"/bin/echo %s|/usr/bin/passwd --stdin %s" %(passwd,user))
            print result,host_list[host_msg],user+"-----------"+passwd
if __name__ == "__main__":
    dic=main()
    with open("host_message.pickle","w") as fd:
        pickle.dump(dic,fd)
    set_execl()
    ssh_main()

复制代码 代码如下: #!/usr/bin/env python #coding:utf8 #随机生成自定义长度密码 from random import choic...

本文由六合联盟网发布于编程应用,转载请注明出处:python实现linux服务器批量修改密码并生成execl,

关键词: