首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 批量修改密码

python 批量修改密码

作者头像
py3study
发布2020-01-08 14:33:58
1.8K0
发布2020-01-08 14:33:58
举报
文章被收录于专栏:python3python3python3

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

#!/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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档