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定时器使用示例分享
Feb 16 Python
Django中模型Model添加JSON类型字段的方法
Jun 17 Python
Python实现的寻找前5个默尼森数算法示例
Mar 25 Python
Python中pillow知识点学习
Apr 30 Python
Tensorflow中的placeholder和feed_dict的使用
Jul 09 Python
python cs架构实现简单文件传输
Mar 20 Python
python使用folium库绘制地图点击框
Sep 21 Python
Python3.4 splinter(模拟填写表单)使用方法
Oct 13 Python
Django 开发环境与生产环境的区分详解
Jul 26 Python
如何用python处理excel表格
Jun 09 Python
利用Pycharm + Django搭建一个简单Python Web项目的步骤
Oct 22 Python
Pycharm创建文件时自动生成文件头注释(自定义设置作者日期)
Nov 24 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
PR值查询 | PageRank 查询
2006/12/20 PHP
PHP print类函数使用总结
2010/06/25 PHP
php获取汉字首字母的函数
2013/11/07 PHP
destoon文章模块调用企业会员资料的方法
2014/08/22 PHP
3款值得推荐的微信开发开源框架
2014/10/28 PHP
php 删除cookie方法详解
2014/12/01 PHP
完美解决在ThinkPHP控制器中命名空间的问题
2017/05/05 PHP
php7新特性的理解和比较总结
2019/04/14 PHP
PHP面向对象程序设计内置标准类,普通数据类型转为对象类型示例
2019/06/12 PHP
Javascript中实现String.startsWith和endsWith方法
2015/06/10 Javascript
详解Jquery实现ready和bind事件
2016/04/14 Javascript
AngularJS入门教程之MVC架构实例分析
2016/11/01 Javascript
用nodejs搭建websocket服务器
2017/01/23 NodeJs
React-Native使用Mobx实现购物车功能
2017/09/14 Javascript
JS实现json对象数组按对象属性排序操作示例
2018/05/18 Javascript
详解vue-cli中模拟数据的两种方法
2018/07/03 Javascript
Vuex 在Vue 组件中获得Vuex 状态state的方法
2018/08/27 Javascript
微信小程序设置全局请求URL及封装wx.request请求操作示例
2019/04/02 Javascript
JavaScript数组排序小程序实现解析
2020/01/13 Javascript
vue实现简单加法计算器
2020/10/22 Javascript
vue中defineProperty和Proxy的区别详解
2020/11/30 Vue.js
python 实现一个贴吧图片爬虫的示例
2017/10/12 Python
PYTHON基础-时间日期处理小结
2018/05/05 Python
Python安装Flask环境及简单应用示例
2019/05/03 Python
通过python3实现投票功能代码实例
2019/09/26 Python
keras .h5转移动端的.tflite文件实现方式
2020/05/25 Python
Python smtp邮件发送模块用法教程
2020/06/15 Python
Python图像处理二值化方法实例汇总
2020/07/24 Python
python中pyqtgraph知识点总结
2021/01/26 Python
Canvas获取视频第一帧缩略图的实现
2020/11/11 HTML / CSS
美国网上花店:JustFlowers
2017/02/12 全球购物
雅高酒店中国:Accorhotels.com China
2018/03/26 全球购物
2014年财务工作总结范文
2014/11/11 职场文书
2014年加油站站长工作总结
2014/12/23 职场文书
二手车转让协议书
2015/01/29 职场文书
在Windows Server 2012上安装 .NET Framework 3.5 所遇到的问题
2022/04/29 Servers