#!/usr/bin/python # -*- coding: utf-8 -*- from fabric.api import * from fabric.colors import * import string from random import choice import socket import paramiko env.user = 'test' env.password = 'fsdfsdfasdafddd' env.hosts = [ '10.10.10.1:522','10.10.10.2:22' ] @task @parallel def passwd(user, passwd=False): with settings(hide('running', 'stdout', 'stderr'), warn_only=True): if isup(env.host,env.port): if not passwd: passwd = genpass() #passwd = "thisiatest" sudo("echo -e '%s\n%s' | passwd %s" % (passwd, passwd, user)) with open("passwd.list","a") as f: f.write("%s-%s-%s-%s\n" %(env.host,user,env.port,passwd)) @task def genpass(length=10): return ''.join(choice(string.ascii_lowercase + string.uppercase + string.digits) for _ in range(length)) @task def isup(host,port): port = int(port) print 'connecting host: %s' % host timeout = socket.getdefaulttimeout() socket.setdefaulttimeout(1) up = True try: paramiko.Transport((host,port)) except Exception, e: up = False print '%s:%s down, %s' % (host,port,e) finally: socket.setdefaulttimeout(timeout) return up @task(default=True) @runs_once def help(): print red("isup(host,port)") print blue(" -Check host ssh is alive") print red("genpass(length=10)") print blue(" -Return random password") print red("passwd(user)") print blue(" -Main process,change user's password") print red("help()") print blue(" -You know!")
Python+Fabric批量修改服务器账户密码
转载文章标签 python;fabric 文章分类 服务器
下一篇:我的友情链接
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Linux 服务器批量修改密码
Linux 服务器批量修改密码
linux 脚本 批量修改密码 -
python批量修改服务器密码,同时保存execl表格
这个替我省了不少时间 嘿嘿
python 批量修改密码