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 17 Python
python实现解数独程序代码
Apr 12 Python
Python最火、R极具潜力 2017机器学习调查报告
Dec 11 Python
python实现彩票系统
Jun 28 Python
Python BS4库的安装与使用详解
Aug 08 Python
tensor和numpy的互相转换的实现示例
Aug 02 Python
python自动化测试之异常及日志操作实例分析
Nov 09 Python
Python连接Hadoop数据中遇到的各种坑(汇总)
Apr 14 Python
python 两种方法删除空文件夹
Sep 29 Python
粗暴解决CUDA out of memory的问题
May 22 Python
Python实现拼音转换
Jun 07 Python
Python+Selenium实现读取网易邮箱验证码
Mar 13 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
德生PL990的分析评价
2021/03/02 无线电
php socket方式提交的post详解
2008/07/19 PHP
php数组函数序列之rsort() - 对数组的元素值进行降序排序
2011/11/02 PHP
php输出xml格式字符串(用的这个)
2012/07/12 PHP
PHP中$_SERVER的详细参数与说明介绍
2013/10/26 PHP
php实现的支持断点续传的文件下载类
2014/09/23 PHP
Yii遍历行下每列数据的方法
2016/10/17 PHP
在laravel框架中使用model层的方法
2019/10/08 PHP
个人总结的一些关于String、Function、Array的属性和用法
2007/01/10 Javascript
新浪中用来显示flash的函数
2007/04/02 Javascript
Mootools 1.2教程 类(一)
2009/09/15 Javascript
JavaScript验证电子邮箱的函数
2014/08/22 Javascript
jQuery easyui刷新当前tabs的方法
2016/09/23 Javascript
Boostrap栅格系统与自己额外定义的媒体查询的冲突问题
2017/02/19 Javascript
Bootstrap实现的标签页内容切换显示效果示例
2017/05/25 Javascript
浅析Vue实例以及生命周期
2018/08/14 Javascript
小程序实现分类页
2019/07/12 Javascript
Python中的fileinput模块的简单实用示例
2015/07/09 Python
Python实现Linux的find命令实例分享
2017/06/04 Python
python绘制多个曲线的折线图
2020/03/23 Python
python3 中的字符串(单引号、双引号、三引号)以及字符串与数字的运算
2019/07/18 Python
python递归下载文件夹下所有文件
2019/08/31 Python
python中dict()的高级用法实现
2019/11/13 Python
Python字节单位转换(将字节转换为K M G T)
2021/03/02 Python
GNC健安喜官方海外旗舰店:美国著名保健品牌
2017/01/04 全球购物
加拿大国民体育购物网站:National Sports
2018/11/04 全球购物
体育纪念品、亲笔签名的体育收藏品:Steiner Sports
2020/07/31 全球购物
采购主管岗位职责
2014/02/01 职场文书
教师敬业奉献模范事迹材料
2014/05/18 职场文书
活动宣传策划方案
2014/05/23 职场文书
节约用水的口号
2014/06/20 职场文书
知识就是力量演讲稿
2014/09/13 职场文书
2015年元旦联欢晚会活动总结
2014/11/28 职场文书
死者家属慰问信
2015/03/24 职场文书
2015年施工员工作总结范文
2015/04/20 职场文书
Nginx的rewrite模块详解
2021/03/31 Servers