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 ...
随机推荐
- 源码网站(msdn.itellyou.cn) good
verysource 100万源码http://www.verysource.com/category/delphi-vcl/ MSDN DOWNLOADhttp://msdn.itellyou.cn ...
- HTML第二本书学习后记
1,换行标记<br>,不换行标记<nobr></nobr> 2,添加水平线 <hr> 3,插入特殊符号: 空格   " ...
- Mac mySql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)的解决办法
我的环境:Mac 10.11.6 ,mysql 5.7.14 . mac mySql 报错ERROR 2002 (HY000): Can't connect to local MySQL serv ...
- php+mysql+Apache环境搭建
最近有一个小程序需要用php来跑,记录一下php的环境配置过程. 1.首先在下载集成工具wamp,WAMP是指在Windows服务器上使用Apache.MySQL和PHP的集成安装环境,可以快速安装配 ...
- 任务9user表建表语句
create table user( id int not null auto_increment primary key, username ) not null default '', pwd ) ...
- IIS发布网站
IIS发布网站几个问题 1.部署步骤:http://jingyan.baidu.com/article/3065b3b6e5becdbecff8a4d5.html Win7下IIS报503Servic ...
- Lua屏蔽对象方法和恢复的方法
背景 对于OO思想实现的类, 对于某些场景需要屏蔽某些方法, 不让调用.过了这段场景, 就恢复这些类的方法, 可以调用. 例如: 工厂具有开工方法, 但是在晚上不允许开工, 所有在晚上这段时间, 见开 ...
- 罗永浩专访全文记录(转自好奇心日报-http://www.qdaily.com/)
这篇文章是转的,存档做记录,定期看一看,激励自己遇到到困难时,想想人家比自己难多了,自己那点事算个屁啊.学习别人,不要带有傻逼主观倾向性,这样什么也得不到,我看完后,发现有一句话,说的非常好,自己有自 ...
- call经常用到的地方
看完这几个下例子,会对学习js有所帮助1.小猫和小狗 function food(){} food.prototype={ food:'fish', say:function(){ console.l ...
- EL表达式查询出来的数据,下载成excel表格,很实用的
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...