批量改密码


注释:里面的modifyrootpasswd.txt文件为储存IP,用户及密码的文件,格式如下:
##ip            user    port    oldpw                                   newpw


-----------------------------------------------------------------------------------------


#!/bin/bash
File=/root/modifyrootpasswd.txt


for Ip in `awk '/^[^#]/{print $1}' $File`;do
        User=`awk -v Var=$Ip '{if(Var==$1)print $2}' $File`
        Port=`awk -v Var=$Ip '{if(Var==$1)print $3}' $File`
        Oldpw=`awk -v Var=$Ip '{if(Var==$1)print $4}' $File`
        Newpw=`awk -v Var=$Ip '{if(Var==$1)print $5}' $File`
expect -c "
    spawn ssh -p $Port $User@$Ip
    set timeout 30
    expect {
        \"(yes/no)\" {send \"yes\r\";exp_continue}
        \"password:\" {send \"$Oldpw\r\";exp_continue}
        \"$User@*\" {send \"echo \'$Newpw\' |passwd --stdin $User\r exit\r\";exp_continue}
    }"
done
对于批量修改密码,你可以使用 SecureCRT 的脚本功能来实现。以下是一个示例脚本,可以帮助你实现批量修改密码的操作: ```python # 导入必要的模块 import os import sys import traceback # 定义需要修改密码的主机列表 hosts = [ { "hostname": "10.0.0.1", "username": "admin", "password": "old_password", "new_password": "new_password" }, { "hostname": "10.0.0.2", "username": "admin", "password": "old_password", "new_password": "new_password" } ] # 定义执行命令的函数 def execute_command(session, command): session.Send(command + '\r\n') return session.WaitForString(command) try: # 创建 SecureCRT 对象 crt = SecureCRT.Session() # 遍历主机列表 for host in hosts: # 连接主机 crt.Connect(host["hostname"]) # 登录主机 crt.WaitForString("login:") crt.Send(host["username"] + '\r\n') crt.WaitForString("password:") crt.Send(host["password"] + '\r\n') # 修改密码 crt.WaitForString("$") execute_command(crt, "passwd") crt.WaitForString("Old password:") crt.Send(host["password"] + '\r\n') crt.WaitForString("New password:") crt.Send(host["new_password"] + '\r\n') crt.WaitForString("Retype new password:") crt.Send(host["new_password"] + '\r\n') # 断开连接 crt.WaitForString("$") execute_command(crt, "exit") # 关闭 SecureCRT 对象 crt.Quit() except Exception as e: # 发生异常时打印错误信息 traceback.print_exc(file=sys.stdout) ``` 在上述示例脚本中,你可以通过修改 `hosts` 列表来指定需要批量修改密码的主机。每个主机需要指定主机名(hostname)、用户名(username)、旧密码(old_password)和新密码(new_password)。你可以根据实际情况进行修改。 请注意,这只是一个示例脚本,实际使用时可能需要根据你的环境和需求进行适当的修改。另外,确保你已经正确安装和配置了 SecureCRT。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值