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中的localtime()方法使用详解
May 22 Python
Python3控制路由器——使用requests重启极路由.py
May 11 Python
简单谈谈python中的Queue与多进程
Aug 25 Python
深入理解Python爬虫代理池服务
Feb 28 Python
Pycharm无法使用已经安装Selenium的解决方法
Oct 13 Python
python爬虫获取小区经纬度以及结构化地址
Dec 30 Python
python requests 库请求带有文件参数的接口实例
Jan 03 Python
python贪吃蛇游戏代码
Apr 18 Python
python 图片去噪的方法示例
Jul 09 Python
使用 Python 读取电子表格中的数据实例详解
Apr 17 Python
Python ADF 单位根检验 如何查看结果的实现
Jun 03 Python
Pandas实现批量拆分与合并Excel的示例代码
May 30 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
Banner程序
2006/10/09 PHP
php调用mysql存储过程
2007/02/14 PHP
利用phpExcel实现Excel数据的导入导出(全步骤详细解析)
2013/11/26 PHP
php实现获取文件mime类型的方法
2015/02/11 PHP
thinkPHP自动验证、自动添加及表单错误问题分析
2016/10/17 PHP
PHP合并两个或多个数组的方法
2019/01/20 PHP
实现51Map地图接口(示例代码)
2013/11/22 Javascript
js的匿名函数使用介绍
2013/12/11 Javascript
angularJS 入门基础
2015/02/09 Javascript
js控制页面的全屏展示和退出全屏显示的方法
2015/03/10 Javascript
jQuery插件Slider Revolution实现响应动画滑动图片切换效果
2015/06/05 Javascript
javascript倒计时效果实现
2015/11/12 Javascript
js实现字符串和数组之间相互转换操作
2016/01/12 Javascript
React Native如何消除启动时白屏的方法
2017/08/08 Javascript
node.js将MongoDB数据同步到MySQL的步骤
2017/12/10 Javascript
详解Nodejs内存治理
2018/05/13 NodeJs
node.js制作一个简单的登录拦截器
2020/02/10 Javascript
[01:20]DOTA2 齐天大圣至宝动态展示
2016/12/13 DOTA
Python中的Classes和Metaclasses详解
2015/04/02 Python
Python环境变量设置方法
2016/08/28 Python
opencv python统计及绘制直方图的方法
2019/01/21 Python
Python实现一个带权无回置随机抽选函数的方法
2019/07/24 Python
pytorch在fintune时将sequential中的层输出方法,以vgg为例
2019/08/20 Python
简单瞅瞅Python vars()内置函数的实现
2019/09/27 Python
Python绘制数码晶体管日期
2021/02/19 Python
AHAVA美国官方网站:死海海泥护肤品牌
2016/10/18 全球购物
佳能英国官方网站:Canon UK
2017/08/08 全球购物
开普敦通行证:Cape Town Pass
2019/07/18 全球购物
台湾屈臣氏网路商店:Watsons台湾
2020/12/29 全球购物
2014领导班子四风剖析对照检查材料思想汇报
2014/09/20 职场文书
工作检讨书500字
2014/10/19 职场文书
道德模范事迹材料
2014/12/20 职场文书
小时代观后感
2015/06/10 职场文书
2016年三八节红领巾广播稿
2015/12/17 职场文书
nginx简单配置多个server的方法
2021/03/31 Servers
oracle数据库去除重复数据
2022/05/20 Oracle