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


Posted in Python onApril 22, 2014

批量修改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 相关文章推荐
Python中的__new__与__init__魔术方法理解笔记
Nov 08 Python
python实现文件路径和url相互转换的方法
Jul 06 Python
解析Python中while true的使用
Oct 13 Python
Python3 操作符重载方法示例
Nov 23 Python
在python中安装basemap的教程
Sep 20 Python
python生成多个只含0,1元素的随机数组或列表的实例
Nov 12 Python
利用python提取wav文件的mfcc方法
Jan 09 Python
python爬虫的一个常见简单js反爬详解
Jul 09 Python
Python 画出来六维图
Jul 26 Python
详解python中index()、find()方法
Aug 29 Python
推荐8款常用的Python GUI图形界面开发框架
Feb 23 Python
Selenium基于PIL实现拼接滚动截图
Apr 10 Python
python中精确输出JSON浮点数的方法
Apr 18 #Python
python中使用OpenCV进行人脸检测的例子
Apr 18 #Python
在python的WEB框架Flask中使用多个配置文件的解决方法
Apr 18 #Python
Python操作json数据的一个简单例子
Apr 17 #Python
python使用ctypes模块调用windowsapi获取系统版本示例
Apr 17 #Python
python调用windows api锁定计算机示例
Apr 17 #Python
python实现监控windows服务并自动启动服务示例
Apr 17 #Python
You might like
通过chrome浏览器控制台(Console)进行PHP Debug的方法
2016/10/19 PHP
PHP网站自动化配置的实现方法(必看)
2017/05/27 PHP
Javascript实现的鼠标经过时播放声音
2010/05/18 Javascript
javascript中RegExp保留小数点后几位数的方法分享
2013/08/13 Javascript
javascript实现点击商品列表checkbox实时统计金额的方法
2015/05/15 Javascript
js+HTML5实现视频截图的方法
2015/06/16 Javascript
JavaScript Math.round() 方法
2015/12/18 Javascript
使用jquery获取url及url参数的简单实例
2016/06/14 Javascript
模拟javascript中的sort排序(简单实例)
2016/08/17 Javascript
详解Angular2 关于*ngFor 嵌套循环
2017/05/22 Javascript
webpack之引入图片的实现及问题
2018/10/08 Javascript
发布订阅模式在vue中的实际运用实例详解
2019/06/09 Javascript
JavaScript实现单英文金山打字通
2020/07/24 Javascript
详解利用nodejs对本地json文件进行增删改查
2019/09/20 NodeJs
Vue + Element-ui的下拉框el-select获取额外参数详解
2020/08/14 Javascript
Node.JS如何实现JWT原理
2020/09/18 Javascript
Python中的深拷贝和浅拷贝详解
2015/06/03 Python
Python爬虫框架Scrapy实例代码
2018/03/04 Python
完美解决安装完tensorflow后pip无法使用的问题
2018/06/11 Python
python训练数据时打乱训练数据与标签的两种方法小结
2018/11/08 Python
使用GitHub和Python实现持续部署的方法
2019/05/09 Python
python中加背景音乐如何操作
2020/07/19 Python
html5 input元素新特性_动力节点Java学院整理
2017/07/06 HTML / CSS
同步和异步有何异同,在什么情况下分别使用他们?举例说明
2014/02/27 面试题
广告学专业推荐信范文
2013/11/23 职场文书
毕业生找工作的求职信范文
2013/12/24 职场文书
英文导游欢迎词
2014/01/11 职场文书
消防先进事迹材料
2014/02/10 职场文书
大一学生职业生涯规划
2014/03/11 职场文书
2014年销售部工作总结
2014/12/01 职场文书
奠基仪式致辞
2015/07/30 职场文书
中国文明网2015年“向国旗敬礼”活动网上签名寄语
2015/09/24 职场文书
面试必问:圣杯布局和双飞翼布局的区别
2021/05/13 HTML / CSS
python实现会员信息管理系统(List)
2022/03/18 Python
mysql数据库隔离级别详解
2022/06/16 MySQL
微信小程序纯CSS实现无限弹幕滚动效果
2022/09/23 HTML / CSS