使用python的paramiko模块对多台机器更新密码
2021-06-27 14:07
标签:print file paramiko exce cep command return out [1] 废话不多说,直接上代码 #!/usr/bin/env python 需要有一个hosts文件被读取 文件格式为: 192.168.1.1,root,123456,654321 英文逗号分隔 也可以做成多线程,更快的执行 使用python的paramiko模块对多台机器更新密码 标签:print file paramiko exce cep command return out [1] 原文地址:https://www.cnblogs.com/c-wei/p/9651802.html
# -*- coding: utf-8 -*-
from __future__ import print_function
import paramiko
def connect_modify(ip, username, password, newpass):
paramiko.util.log_to_file(‘syslogin.log‘)
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
try:
client.connect(hostname=ip, username=username, password=password)
except:
print(ip+‘network not connect‘)
return 0
stdin, stdout, stderr = client.exec_command(‘echo %s | passwd --stdin %s ‘ % (newpass, username))
result = stdout.read(), stderr.read()
for line in result:
print(line.decode(‘utf-8‘))
print(ip, ‘OK!‘)
client.close()
return 1
def main():
try:
with open(‘hosts‘) as f:
for item in f:
item = item.strip().split(‘,‘)
ip = item[0]
username = item[1]
password = item[2]
newpass = item[3]
connect_modify(ip, username, password, newpass)
except Exception as e:
print(e, ‘not open password configfile‘)
if __name__ == ‘__main__‘:
main()
上一篇:注解实现的spring事务
文章标题:使用python的paramiko模块对多台机器更新密码
文章链接:http://soscw.com/essay/98459.html