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

本文出自 “坏男孩” 博客,请务必保留此出处http://5ydycm.blog.51cto.com/115934/407163

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值