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

 更新时间:2014年04月22日 09:51:33   作者:  
这篇文章主要介绍了python实现linux服务器批量修改密码并生成execl示例,需要的朋友可以参考下

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

复制代码 代码如下:

#!/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()

相关文章

  • 详解Python判定IP地址合法性的三种方法

    详解Python判定IP地址合法性的三种方法

    这篇文章主要介绍了详解Python判定IP地址合法性的三种方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • Pycharm debug调试教程

    Pycharm debug调试教程

    Debug即程序调试,是发现和解决代码错误的过程,通过设置断点和逐步执行,可以监控程序执行过程和变量状态,帮助开发者定位问题,Debug工具还能评估表达式和监视变量,极大地方便了程序调试,本文就来介绍一下
    2024-11-11
  • Python 字符串操作方法大全

    Python 字符串操作方法大全

    python字符串操作实方法大合集,包括了几乎所有常用的python字符串操作,如字符串的替换、删除、截取、复制、连接、比较、查找、分割等,需要的朋友可以参考下
    2014-03-03
  • python通过opencv实现图片裁剪原理解析

    python通过opencv实现图片裁剪原理解析

    这篇文章主要介绍了python通过opencv实现图片裁剪原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01
  • 详解Python二维数组与三维数组切片的方法

    详解Python二维数组与三维数组切片的方法

    这篇文章主要介绍了详解Python二维数组与三维数组切片的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • 基于Python编写复杂密码图形化生成工具

    基于Python编写复杂密码图形化生成工具

    这篇文章主要为大家详细介绍了如何基于Python编写复杂密码图形化生成工具,支持选择生成10位和12位复杂密码,需要的小伙伴可以参考一下
    2024-04-04
  • python GUI库图形界面开发之PyQt5菜单栏控件QMenuBar的详细使用方法与实例

    python GUI库图形界面开发之PyQt5菜单栏控件QMenuBar的详细使用方法与实例

    这篇文章主要介绍了python GUI库图形界面开发之PyQt5菜单栏控件QMenuBar的详细使用方法与实例,需要的朋友可以参考下
    2020-02-02
  • Python创建系统目录的方法

    Python创建系统目录的方法

    这篇文章主要介绍了Python创建系统目录的方法,实例分析了Python操作目录的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • 详解python中TCP协议中的粘包问题

    详解python中TCP协议中的粘包问题

    这篇文章主要介绍了python中TCP协议中的粘包问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • python 用opencv实现图像修复和图像金字塔

    python 用opencv实现图像修复和图像金字塔

    这篇文章主要介绍了python 如何用opencv实现图像修复和图像金字塔,帮助大家更好的理解和使用python处理图片,感兴趣的朋友可以了解下
    2020-11-11

最新评论