python实现linux服务器批量修改密码并生成execl
发布时间:2022-11-22 17:16:06
批量修改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()
猜你喜欢
- 最近又新上了一部分站点,随着站点的增多,管理复杂性也上来了,俗话说:人多了不好带,我发现站点多了也不好管,因为这些站点里有重要的也有不重要的
- '************************************* '检测是否只包含英文和数
- Why use Sockets?套接字是网络的基础。它们使在两个不同程序或设备之间的信息传输成为可能。例如,当您打开浏览器时,您作为客户端正
- 函数:len()1:作用:返回字符串、列表、字典、元组等长度2:语法:len(str)3:参数:str:要计算的字符串、列表、字典、元组等4
- 本文介绍了Python对于线程的支持,包括“学会”多线程编程需要掌握的基础以及Python两个线程标准库的完整介绍及使用示例。注意:本文基于
- 本文实例讲述了Python基于回溯法子集树模板解决取物搭配问题。分享给大家供大家参考,具体如下:问题有5件不同的上衣,3条不同的裤子,4顶不
- 数据结构:通俗点说,就是储存大量数据的容器。这里主要介绍Python的4种基本数据结构:列表、字典、元组、集合。格式如下:列表:list =
- 一、python-yml文件读写使用库 :import yaml安装:pip install pyyaml示例:文件config2.ymlg
- IronPython是一种在 .NET及 Mono上的 Python实现,由微软的 Jim Hugunin所发起,是一个开源的项目,基于微软
- 简介桥接模式又叫桥梁模式,属于结构型模式。目的是将抽象与实现分离,使它们都可以独立的变化,解耦。继承有很多好处,但是会增加耦合,而桥接模式偏
- 前言PHP5.3之后支持了类似Java的jar包,名为phar。用来将多个PHP文件打包为一个文件。首先需要修改php.ini配置将phar
- 这篇文章主要介绍了python连接字符串过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以
- 终于构建出了第一个神经网络,Keras真的很方便。之前不知道Keras这么方便,在构建神经网络的过程中绕了很多弯路,最开始学的TensorF
- 写 python 文件时,每个文件开头都必须注明版本和编码。每次我 touch 文件之后粘贴这两句话让我不胜其烦。由于我没有安装 pytho
- 关于书写习惯,遵循曾经总结过的风格标准,现在一点都没有变。并且近来翻看高手作品,横向连排似乎在大产品项目中逐渐成为主流,个人认为如此维护效率
- 在编程时你一定碰到过时间触发的事件,在VB中有timer控件,而asp中没有,假如你要不停地查询数据库来等待一个返回结果的话,我想你一定知道
- 一、日志级别1. 级别定义logging模块提供了5种日志级别,分别为:CRITICAL>ERROR>WARNING>IN
- 见以下两个文件:showimage.asp Change the HTTP header
- 安装官网下载http://ffmpeg.org/选择需要的版本在这个网址下载ffmpeg,https://github.com/BtbN/F
- 一、CrawlSpider类介绍1.1 引入使用scrapy框架进行全站数据爬取可以基于Spider类,也可以使用接下来用到的CrawlSp