ansible非root用户批量修改root密码
前言:
由于线上服务器密码长久没有更新,现领导要求批量更换密码。线上的之前部署过salt,但由于各种因素没有正常使用。
使用自动化工具批量修改的计划搁浅了,后来领导给了个python多线程修改密码脚本。但实际测试效果并不理想,会出现卡住情况
#!/usr/bin/python
import paramiko#ssh连接模块
import time,sys,re,os
import socket
import threading,Queue#线程模块 root_cmd = r'''
这里输入你要执行的命令
'''
user_cmd = r''' echo '' '''
issu = 1
root_pwd='你要修改的root密码'
login_user = '普通用户名'
key_file = '/home/.ssh/id_rsa'#普通用户key
sshport = 22#端口
time_out = 60 #超时时间
Numer_Thread = 300#最大线程数(根据主机数量修改) q = Queue.Queue()#线程队列
socket.setdefaulttimeout(time_out)
lock = threading.RLock()#线程锁(同时只允许一个线程执行动作)
onlydir = dir() def sshgo(host,rootuser,rootpwd):
rtn = []
key = paramiko.RSAKey.from_private_key_file(key_file)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_system_host_keys() rtn.append('________________________________%s'%host)
try:
ssh.connect(host,sshport,login_user,pkey=key)
except Exception,e:
rtn.append('%s__ERROR_________________________%s'%(e,host))
return rtn
if 'user_cmd' in onlydir:
stdin, stdout, stderr = ssh.exec_command('LANG=en_US.UTF-8;LANGUAGE=en_US.UTF-8; %s'%user_cmd)
rtn.append(stdout.read() + stdout.read())
#rtn.append(stdout.read() + stderr.read())
if not issu:
#return rtn
return "",(rtn)
shell = ssh.invoke_shell()
while not shell.recv(4096).endswith(']$ '):
time.sleep(0.1) buff =''
shell.send('LANG=en_US.UTF-8;LANGUAGE=en_US.UTF-8;su - %s'%rootuser)
shell.send('\n')
while not buff.endswith('Password: '):
time.sleep(0.1)
resp = shell.recv(4096)
buff += resp
if buff.endswith('exist') or buff.endswith(']$ '):
rtn.append('ERROR_SSH.RECV_____1________________%s'% resp)
return rtn
buff =''
shell.send(root_pwd)
shell.send('\n')
while not buff.endswith(']# '):
time.sleep(0.1)
resp = shell.recv(4096)
buff += resp
if buff.endswith('password') or buff.endswith(']$ '):
rtn.append('ERROR_SSH.RECV_____2________________%s'% resp)
return rtn
shell.send('LANG=en_US.UTF-8;LANGUAGE=en_US.UTF-8; %s '%root_cmd)
shell.send('\n')
buff = ''
while not buff.endswith(']# '):
time.sleep(0.1)
resp = shell.recv(4096)
buff += resp
if buff.endswith(']$ '):
rtn.append('ERROR_SSH.RECV_____3________________%s'% resp)
break
elif buff.endswith('? '):
rtn.append('ERROR_SSH.RECV_____4________________??')
break
#print buff
#rtn= (''.join(rtn)).strip()+" "+host
rtn.append('\n'.join(buff.split('\n')[1:-1]))
ssh.close()
return "",(rtn)
#return rtn def do_echo(host,rootuser,rootpwd):
result = sshgo(host,rootuser,rootpwd)
lock.acquire()
for pp in result:
print pp
sys.stdout.flush()
lock.release() def working():
while 1:
args = q.get()
do_echo(args[0],args[1],args[2])
q.task_done() for i in range (Numer_Thread):
t = threading.Thread(target=working)
t.setDaemon(1)
t.start()
print "Begin......"
fn = open("/var/tmp/169" ,"r")
#fn = open("/tmp/1.log" ,"r")
for i in fn:
if not re.match('#',i) and re.search('.',i):
c = i.split()
q_args = [c[0],'','']
#q_args = [c[0],c[1],c[2]]
q.put(q_args)
fn.close()
q.join()
python多线程修改root密码脚本
1.安装ansible
2.修改主机配置文件

配置得了root密码,及普通用户和相应的key,这样做其实并不理想,1:不安全2:配置复杂点。后续可以使用ansible的playbook实现
2.切换普通用户执行命令
3.playbook版本
ansible非root用户批量修改root密码的更多相关文章
- Ansible playbook 批量修改服务器密码 先普通后root用户
fsckzy Ansible playbook 批量修改服务器密码 客户的需求:修改所有服务器密码,密码规则为Rfv5%+主机名后3位 背景:服务器有CentOS6.7,SuSE9.10.11,r ...
- centos 7 下进入单用户模式修改root密码
centos7进入单用户模式修改root用户密码 在工作中可能会遇到root密码忘记,那么这里就要进入单用户模式下了. 在重启电脑之后 时间:2015-05-13 10:42来源:blog.51cto ...
- 转Centos7.0进入单用户模式修改root密码
Centos7.0进入单用户模式修改root密码 启动Centos7 ,按空格让其停留在如下界面. 按e进行编辑 在UTF-8后面输入init=/bin/sh 根据提示按ctrl+x 得如下图 输 ...
- 忘记root密码怎么办-单用户模式修改root密码
忘记root密码怎么办-单用户模式修改root密码================================= 1,开机3秒内按下向下的方向键,目的是为了不让它进入系统,而是停留在开机界面. 2 ...
- 单用户模式修改root密码
单用户模式修改root密码 1.进入引导菜单界面2.按e进入grub,在linux或linux16那行结尾加上 rw init=/bin/bash,按Ctrl+x或F103.进入bash-4.3# , ...
- 进入单用户模式修改root密码
进入单用户模式修改root密码 1.进入引导菜单界面2.按e进入grub,在linux或linux16那行结尾加上 rw init=/bin/bash,按Ctrl+x或F103.进入bash-4.3# ...
- saltstack+python批量修改服务器密码
saltstack安装:略过 python脚本修改密码: # -*- coding utf-8 -*- import socket import re import os import sys imp ...
- ubuntu首次给root用户设置密码和root用户登录设置
1 ubuntu首次给root用户设置密码 给root用户设置密码输入命令sudo passwd,然后系统会让你输入密码,这时输入的密码就是root用户的密码了,设置完成之后就可以切换root用户登录 ...
- Ansible批量修改root密码
0x01:首先做好免密登录 http://www.cnblogs.com/evlon/p/8094306.html 0x02:批量修改密码 ansible all -m raw -a "ec ...
随机推荐
- Magento Connector: Error: Please check for sufficient write file permissions
请确保下面的文件是可写的: /path/to/your/magento-install/path/to/your/magento-install/downloader/path/to/your/mag ...
- Stripe开发笔记之-ISO Currency Code(ISO 4217)
参考:维基百科和ISO官网 ISO Currency Code:国际标准组织通用货币代号,国际标准化组织(英语:International Organization for Standardizati ...
- html中submit和button的区别/ window.location.href 不跳转 的问题
<input type="button"> <input type="submit"> 这两个的区别 是 button 不会自动提交表 ...
- oracle 解锁
解决方法如下: 1:查V$DB_OBJECT_CACHE SELECT * FROM V$DB_OBJECT_CACHE WHERE name='CRM_LASTCHGINFO_DAY' AND LO ...
- l段子
段子简介 L段子起源.L段子是开发者根据市场调查, 用户可在等候列车,飞机,或在公交车上无 聊之余使用本应用.打发无聊的时间,分为段 子,图片,活动和个人中心,用户可根据自己 喜好进入不同的区域.段子 ...
- 【leetcode】两数之和
https://leetcode.com/problems/two-sum/ Example: Given nums = [2, 7, 11, 15], target = 9, Because num ...
- 与MySQL传统复制相比,GTID有哪些独特的复制姿势?
与MySQL传统复制相比,GTID有哪些独特的复制姿势? http://mp.weixin.qq.com/s/IF1Pld-wGW0q2NiBjMXwfg 陈华军,苏宁云商IT总部资深技术经理,从事数 ...
- iOS - UIButton设置文字标题下划线以及下划线颜色
创建button设置可以折行显示 - (void)viewDidLoad { [super viewDidLoad]; UIButton * button = [[UIButton alloc] in ...
- 手机APP开发:学JAVA转安卓APP开发是不是很容易?
成都亿合云商小编为您分享:Android开发是以Java语言为基础的,Android 虽然使用Java 语言作为开发工具,但是在实际开发中发现,还是与Java SDK 有一些不同的地方.Android ...
- Hadoop中Combiner的使用
注:转载自http://blog.csdn.net/ipolaris/article/details/8723782 在MapReduce中,当map生成的数据过大时,带宽就成了瓶颈,怎样精简压缩传给 ...