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实现在matplotlib中两个坐标轴之间画一条直线光标的方法
May 20 Python
Python reduce()函数的用法小结
Nov 15 Python
基于Django用户认证系统详解
Feb 21 Python
快速解决安装python没有scripts文件夹的问题
Apr 03 Python
Python使用itertools模块实现排列组合功能示例
Jul 02 Python
django+mysql的使用示例
Nov 23 Python
python爬虫豆瓣网的模拟登录实现
Aug 21 Python
Python自动化测试笔试面试题精选
Mar 12 Python
Python实现疫情通定时自动填写功能(附代码)
May 27 Python
Python如何操作docker redis过程解析
Aug 10 Python
matplotlib基础绘图命令之imshow的使用
Aug 13 Python
python实现图像高斯金字塔的示例代码
Dec 11 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
PHP初学者头疼问题总结
2006/07/08 PHP
php去除html标记的原生函数详解
2015/01/27 PHP
php实现字符串首字母转换成大写的方法
2015/03/17 PHP
PHP SplObjectStorage使用实例
2015/05/12 PHP
JavaScript 布尔操作符解析  && || !
2012/08/10 Javascript
js弹出模式对话框,并接收回传值的方法
2013/03/12 Javascript
利用百度地图JSAPI生成h7n9禽流感分布图实现代码
2013/04/15 Javascript
使用JS 清空File控件的路径值
2013/07/08 Javascript
javascript中parentNode,childNodes,children的应用详解
2013/12/17 Javascript
JavaScript中的函数的两种定义方式和函数变量赋值
2014/05/12 Javascript
javascript在网页中实现读取剪贴板粘贴截图功能
2014/06/07 Javascript
js怎么覆盖原有方法实现重写
2014/09/04 Javascript
D3.js 从P元素的创建开始(显示可加载数据)
2014/10/30 Javascript
js脚本实现数据去重
2014/11/27 Javascript
jquery实现图片水平滚动效果代码分享
2015/08/26 Javascript
基于JavaScript实现图片点击弹出窗口而不是保存
2016/02/06 Javascript
jquery动态遍历Json对象的属性和值的方法
2016/07/27 Javascript
js实现Tab选项卡切换效果
2020/07/17 Javascript
深入浅析Vue中的slots/scoped slots
2018/04/03 Javascript
从零搭一个自用的前端脚手架的方法步骤
2019/09/23 Javascript
《javascript设计模式》学习笔记七:Javascript面向对象程序设计组合模式详解
2020/04/08 Javascript
Javascript摸拟自由落体与上抛运动原理与实现方法详解
2020/04/08 Javascript
全面解析Vue中的$nextTick
2020/12/24 Vue.js
[07:06]2018DOTA2国际邀请赛寻真——卫冕冠军Team Liquid
2018/08/10 DOTA
python读取txt文件并取其某一列数据的示例
2019/02/19 Python
Python中查看变量的类型内存地址所占字节的大小
2019/06/26 Python
python设置环境变量的作用和实例
2019/07/09 Python
python生成13位或16位时间戳以及反向解析时间戳的实例
2020/03/03 Python
python datetime时间格式的相互转换问题
2020/06/11 Python
细说CSS3中的选择符
2008/10/17 HTML / CSS
机械电子工程专业求职信
2014/06/22 职场文书
小学庆六一活动总结
2014/08/28 职场文书
个人委托书如何写
2014/09/25 职场文书
个人委托书范本汇总
2014/10/01 职场文书
z-index不起作用
2021/03/31 HTML / CSS
Python使用Beautiful Soup(BS4)库解析HTML和XML
2022/06/05 Python