python paramiko多线程批量修改主机账号密码
2015-11-04 16:17
567 查看
#!/usr/bin/env python
#-*- coding:utf:8 -*-
import paramiko,time,threading
def reset_passwd(line):
try:
test=paramiko.Transport((line.split()[0],22))
test.connect(username='username',password='password')
chan=test.open_session()
chan.settimeout(5)
chan.get_pty()
chan.invoke_shell()
chan.send('passwd\n')
time.sleep(1)
chan.send('password\n')
time.sleep(1)
chan.send('newpassword\n')
time.sleep(1)
chan.send('newpassword\n')
time.sleep(1)
chan.close()
lock.acquire()
successful_ip_list.append('%s: 连接成功'%(line.split()[0]))
lock.release()
except Exception, e:
lock.acquire()
failure_ip_list.append('%s: 连接失败'%(line.split()[0]))
lock.release()
successful_ip_list = []
failure_ip_list = []
if __name__ == '__main__':
lock = threading.Lock()
threads = []
start = time.time()
# info.txt文件只有IP地址,每行一个IP地址
info = file('info.txt','r+')
for line in info.readlines():
t = threading.Thread(target=reset_passwd,args=(line,))
threads.append(t)
t.start()
for t in threads:t.join()
if not successful_ip_list:print '没有成功连接信息'
else:
for line in successful_ip_list:print line
print '-'*20
if not failure_ip_list:print '没有失败连接信息'
else:
for line in failure_ip_list:print line
successful_number = len(successful_ip_list)
failure_number = len(failure_ip_list)
end = time.time()
print "成功连接 %s 台服务器,失败连接 %s 台服务器。共用时 %s 秒"%(successful_number,failure_number,(end - start))
本文出自 “、矿泉水” 博客,请务必保留此出处http://guwenqiang.blog.51cto.com/5462040/1709668
#-*- coding:utf:8 -*-
import paramiko,time,threading
def reset_passwd(line):
try:
test=paramiko.Transport((line.split()[0],22))
test.connect(username='username',password='password')
chan=test.open_session()
chan.settimeout(5)
chan.get_pty()
chan.invoke_shell()
chan.send('passwd\n')
time.sleep(1)
chan.send('password\n')
time.sleep(1)
chan.send('newpassword\n')
time.sleep(1)
chan.send('newpassword\n')
time.sleep(1)
chan.close()
lock.acquire()
successful_ip_list.append('%s: 连接成功'%(line.split()[0]))
lock.release()
except Exception, e:
lock.acquire()
failure_ip_list.append('%s: 连接失败'%(line.split()[0]))
lock.release()
successful_ip_list = []
failure_ip_list = []
if __name__ == '__main__':
lock = threading.Lock()
threads = []
start = time.time()
# info.txt文件只有IP地址,每行一个IP地址
info = file('info.txt','r+')
for line in info.readlines():
t = threading.Thread(target=reset_passwd,args=(line,))
threads.append(t)
t.start()
for t in threads:t.join()
if not successful_ip_list:print '没有成功连接信息'
else:
for line in successful_ip_list:print line
print '-'*20
if not failure_ip_list:print '没有失败连接信息'
else:
for line in failure_ip_list:print line
successful_number = len(successful_ip_list)
failure_number = len(failure_ip_list)
end = time.time()
print "成功连接 %s 台服务器,失败连接 %s 台服务器。共用时 %s 秒"%(successful_number,failure_number,(end - start))
本文出自 “、矿泉水” 博客,请务必保留此出处http://guwenqiang.blog.51cto.com/5462040/1709668
相关文章推荐
- 老鸟的Python入门教程
- Python 多进程 fork()详解
- Python IDE PyCharm的基本快捷键和配置简介
- python arguments *args and **args ** is for dictionaries, * is for lists or tuples.
- Python函数
- 用python写简单的url返回码判断(urllib)
- Windows平台python验证码识别
- Python安装、配置
- Python实现周期性抓取网页内容的方法
- Python遍历文件夹中文件
- Python变量类型
- Windows下对Sublime text2进行Python编译环境配置
- Python中 L 的意思
- Python判断IP是否属于某子网
- 如何面试Python后端工程师?
- python os.popen os.system
- python logging 模块
- 建议10:numpy使用基础
- 老李分享:Python开发性能测试脚本
- 2015/11/4用Python写游戏,pygame入门(4):获取鼠标的位置及运动