zoukankan      html  css  js  c++  java
  • 利用expect交互批量修改普通用户密码

    利用expect交互批量修改普通用户密码

    #!/bin/bash
    #Author:shsnc-chenhaijun
    #Time:2020-10-23
    #Name:for_user.sh
    #Version:V1.0
    #Description:This is a password change script.
    
    user=$1
    old_passwd=$2
    new_passwd=$3
    d_time=`date +"%F-%H-%M"`
    #ios_name=`lsb_release -a| awk -F '[: ]+' 'NR==2{print $3}' |sed 's/^[ 	]*//g'`
    #iod_name="bash ios.sh"
    #Define modification function
    function change_passwd(){
    for ip in `cat ip_list.txt`
       do
        if [ $# -ne 3 ]
         then
             echo "Please check whether the transmission is correct"
             echo "$0 Usage:username|the old passwd|The new password"
             exit 0
        fi
        
        timeout 3 sshpass -p ${old_passwd}  ssh  -o StrictHostKeyChecking=no ${user}@${ip} hostname >>./change_passwd_${d_time}.log 2>&1  
        if [ $? -ne 0 ]
         then
            echo "username or passwd error Please check"
            echo "${ip}:${user}:${old_passwd}" >>./old_passwd_${d_time}.txt
            continue
        elif [ '${ios_name}'=='CentOS' ] || [ '${ios_name}'=='RedHat' ] || [ '${ios_name}'=='openSUSE' ]
         then
           /usr/bin/expect << EOF
           set timeout 1   
           spawn /usr/bin/ssh ${user}@$ip
           expect {
               "(yes/no)?" {send "yes
    ";exp_continue}
               "*assword" {send "${old_passwd}
    "}
           }
           expect "*]#"
           send "passwd
    "
           expect {
               "*assword:" { send "${old_passwd}
    " }
           }
           
           expect {
               "New password:" { send "${new_passwd}
    " }
           }
           
           expect {
               "Retype new password:" { send "${new_passwd}
    " }
           }
           
           expect "*]#"
           send "exit
    "
           expect eof
    EOF
      echo "${ip}:${user}:${new_passwd}">>./new_passwd_${d_time}.txt
    else
        echo "${ip}:${user}:${old_passwd}">>./old_passwd_${d_time}.txt
    fi
    done
    }
    
    change_passwd $1 $2 $3
    
  • 相关阅读:
    浅谈流形学习
    流形(Manifold)初步【转】
    MATLAB中的函数的归总
    LBP特征提取实现
    Nginx 安装
    Linux执行.sh文件,提示No such file or directory的问题
    Ubuntu 安装后的配置及美化(二)
    Ubuntu 安装后的配置及美化(一)
    关于windows上 web 和 ftp 站点的创建及使用
    win10 + Lubuntu 双系统安装
  • 原文地址:https://www.cnblogs.com/woaiyunwei/p/13948851.html
Copyright © 2011-2022 走看看