前言:

由于线上服务器密码长久没有更新,现领导要求批量更换密码。线上的之前部署过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
print
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密码脚本

后来想到了不需要安装客户端的自动化管理工具:ansible 好吧那就查下相关资料吧

1.安装ansible

yum -y install ansible

2.修改主机配置文件

vim /etc/ansible/hosts
[web]#主机组
10.18.111.123 ansible_ssh_user=usernam ansible_ssh_private_key_file=/home/maintain/username/.ssh/id_rsa
192.168.1.1 ansible_become_pass='password' ansible_ssh_user=username  ansible_ssh_private_key_file=/home/maintain/username/.ssh/id_rsa

配置得了root密码,及普通用户和相应的key,这样做其实并不理想,1:不安全2:配置复杂点。后续可以使用ansible的playbook实现

2.切换普通用户执行命令

ansible web -S -R root -m raw -a 'echo '需要修改的root密码' | passwd --stdin root'

 

3.playbook版本

 

这脚本参照网上的脚本修改的,但没生效。期待大牛的完善。

参考链接:

http://zylhz.com/?p=107

ansible非root用户批量修改root密码的更多相关文章

  1. Ansible playbook 批量修改服务器密码 先普通后root用户

    fsckzy   Ansible playbook 批量修改服务器密码 客户的需求:修改所有服务器密码,密码规则为Rfv5%+主机名后3位 背景:服务器有CentOS6.7,SuSE9.10.11,r ...

  2. centos 7 下进入单用户模式修改root密码

    centos7进入单用户模式修改root用户密码 在工作中可能会遇到root密码忘记,那么这里就要进入单用户模式下了. 在重启电脑之后 时间:2015-05-13 10:42来源:blog.51cto ...

  3. 转Centos7.0进入单用户模式修改root密码

    Centos7.0进入单用户模式修改root密码   启动Centos7 ,按空格让其停留在如下界面. 按e进行编辑 在UTF-8后面输入init=/bin/sh 根据提示按ctrl+x 得如下图 输 ...

  4. 忘记root密码怎么办-单用户模式修改root密码

    忘记root密码怎么办-单用户模式修改root密码================================= 1,开机3秒内按下向下的方向键,目的是为了不让它进入系统,而是停留在开机界面. 2 ...

  5. 单用户模式修改root密码

    单用户模式修改root密码 1.进入引导菜单界面2.按e进入grub,在linux或linux16那行结尾加上 rw init=/bin/bash,按Ctrl+x或F103.进入bash-4.3# , ...

  6. 进入单用户模式修改root密码

    进入单用户模式修改root密码 1.进入引导菜单界面2.按e进入grub,在linux或linux16那行结尾加上 rw init=/bin/bash,按Ctrl+x或F103.进入bash-4.3# ...

  7. saltstack+python批量修改服务器密码

    saltstack安装:略过 python脚本修改密码: # -*- coding utf-8 -*- import socket import re import os import sys imp ...

  8. ubuntu首次给root用户设置密码和root用户登录设置

    1 ubuntu首次给root用户设置密码 给root用户设置密码输入命令sudo passwd,然后系统会让你输入密码,这时输入的密码就是root用户的密码了,设置完成之后就可以切换root用户登录 ...

  9. Ansible批量修改root密码

    0x01:首先做好免密登录 http://www.cnblogs.com/evlon/p/8094306.html 0x02:批量修改密码 ansible all -m raw -a "ec ...

随机推荐

  1. Spark1.6.2 java实现读取json数据文件插入MySql数据库

    public class Main implements Serializable { /** * */ private static final long serialVersionUID = -8 ...

  2. http获取图片信息

    一.安卓访问网络需要AndroidManifest.xml配置这样一个节点 <manifest> <uses-permission android:name="androi ...

  3. web app 自适应方案总结 关键字 弹性布局之rem

    关于rem,主要参考文档 1.腾讯ISUX (http://isux.tencent.com/web-app-rem.html) 2.http://www.w3cplus.com/css3/defin ...

  4. linux后台查看共享内存和消息队列的命令

    ipcs ipcs -q : 显示所有的消息队列 ipcs -qt : 显示消息队列的创建时间,发送和接收最后一条消息的时间 ipcs -qp: 显示往消息队列中放消息和从消息队列中取消息的进程ID ...

  5. uploadify使用教程

    <div> <input type="file" id="uploadify" name="uploadify" /> ...

  6. DBA数据库:MySQL简述

    一. 数据库介绍 什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库.每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据.我们也可以将数 ...

  7. windows2008吃尽内存的解决办法

    最近才用上windows2008,之前一直用的是windows2003,发现系统运行一段时间后,内存吃紧,赶紧打开资源查看器,发现当前运行的程序占有内存都很小,后经查资料,原来是被windows200 ...

  8. spring 配置bean

    Main(测试方法) public class Main { public static void main(String[] args) { //1.创建Spring 的IOC容器对象: //spr ...

  9. grunt 入门学习

    前端工作流,Grunt上手指南 Posted@2013-04-20 7:15 a.m. CategoriesGrunt ,  javascript 我想先花点时间回忆一下作为一个前端需要做的工作(Lo ...

  10. docker命令小记

    最近工作需要,对docker稍微了解了一下,大部分内容网上都有各种各样的教程.有两个稍微特殊的命令这里记录一下: 1. rename image的命令 从各种私有镜像下载后用docker images ...