python 批量修改密码

下午闲来无事,就搞个批量密码修改工具玩玩...

#!/usr/bin/env python
import paramiko
import time

ip_list=('ip1','ip2')
log_file=open('mpwdok.log','w+')
log_file1=open('mpwderr.log','w+')

for ip in ip_list:
    try:
        s = paramiko.Transport((ip, 22))
        s.connect(username='root', password='222222')
        chan = s.open_session()
        chan.get_pty()
        chan.invoke_shell()
        chan.send('passwd root\n')
        time.sleep(2)
        chan.send('111111\n')
        time.sleep(2)
        chan.send('111111\n')
        time.sleep(2)
        log_file.write("\n"+ip+"\n=================================================================\n")
        log_file.write(chan.recv(1024))
        log_file.write("\n=================================================================")
    except Exception,err:
        log_file1.write("ERR:unable to connect %s:%s\n" %(ip,err))

 

以下是另一种方式自动登录并且修改密码的脚本,仅供参考:

#!/usr/bin/env python
#CreateBy:Badboy 2010-11-02
#Auto modify user passwd

import paramiko
import time
import ConfigParser

cf=ConfigParser.ConfigParser()
cf.read("userinfo.conf")
userName=cf.get("user_info","user_name")
userPass=cf.get("user_info","user_pwd")
serverPort=cf.getint("user_info","server_port")
keyFile=cf.get("user_info","key_path")
ip_list=cf.get("user_info","ip_list").split(",")

channel = paramiko.SSHClient();
channel.set_missing_host_key_policy(paramiko.AutoAddPolicy())
log_file=open('mpwdok.log','w+')
log_file1=open('mpwderr.log','w+')

for serverHost in ip_list:
    try:
        mpwdssh=paramiko.Transport((serverHost,serverPort))
        mykey = paramiko.DSSKey.from_private_key_file(keyFile,password=userPass)
        mpwdssh.connect(username=userName,pkey=mykey)
        chan = mpwdssh.open_session()
        chan.get_pty()
        chan.invoke_shell()
        chan.send('passwd root\n')
        time.sleep(2)
        chan.send('222222\n')
        time.sleep(2)
        chan.send('222222\n')
        time.sleep(2)
        log_file.write("\nLinuxServer_IP:"+serverHost+"\n=================================================================\n")
        log_file.write(chan.recv(1024))
        log_file.write("\n=================================================================")
    except Exception,err:
        log_file1.write("ERR:unable to connect %s:%s\n" %(serverHost,err))
 

userinfo.conf

[user_info]
user_name=root
user_pwd=111111
server_port=22
key_path=/root/.ssh/badboy
ip_list=ip1,ip2



本文转自hahazhu0634 51CTO博客,原文链接:http://blog.51cto.com/5ydycm/407163,如需转载请自行联系原作者

上一篇:八年磨一剑,阿里云HBase做到性能和稳定性提升10倍!


下一篇:低代码平台未来发展和价值