autopasswpCheck.sh
#!/usr/local/bin/expect -f # desc: Auto Check password on the server list. # author: ZHOU Y-Q # powered by www.beijiait.com # version 0.3 written by 2016.05.17 # ###### #set user name to login set user tomcat #set ssh port #set port 22 #open the server list file 'ippw' to f set f [open ./ippw r] #Read the file by line while { [gets $f line]>=0 } { # Get IP address set ip [lindex $line 0] #Get Password set pw1 [lindex $line 1] set pw2 [lindex $line 2] set pw3 [lindex $line 3] ############################## spawn ssh $user@$ip expect { "*yes/no" { send "yes\r"; exp_continue } "*word:" { send -- "$pw2\r" } } expect "*$*" #send "su - \r" send "export LANG=en_US -\r" expect { "*yes/no" { send "yes\r"; exp_continue } "*assword:" { send "$pw2\r" } } send "echo 'password OK'\r" #exit login send "exit\r" send "exit\r" interact spawn ssh weblogic@$ip expect { "*yes/no" { send "yes\r"; exp_continue } "*word:" { send -- "$pw3\r" } } expect "*$*" send "echo 'password OK'\r" #exit login send "exit\r" interact }AutoSet.sh
#!/usr/local/bin/expect -f # desc: Auto Change password for sxlt & root on the server list. # author: ZHOU Y-Q # powered by www.beijiait.com # version 0.93 written by 2018.06.26 # ###### #set user name to login set user tomcat #open the server list file 'ippw' to f set f [open ./ippw r] #Read the file by line while { [gets $f line]>=0 } { # Get IP address set ip [lindex $line 0] #Get Password set pw1 [lindex $line 1] set pw2 [lindex $line 2] set pw3 [lindex $line 3] set pw4 [lindex $line 4] ############################## spawn ssh $user@$ip expect { "*yes/no" { send "yes\r"; exp_continue } "*assword:" { send "$pw1\r" } } expect "*$*" send "export LANG=en_US;su -\r" expect { "*yes/no" { send "yes\r"; exp_continue } "*assword:" { send "$pw2\r" } } send "echo 'password OK'\r" #new passwd # send "chattr -i /etc/passwd /etc/shadow /etc/group /etc/gshadow\r" # send_user "The New PW is: $pw1\n" # send_user "The New PW is: $pw2\n" # send_user "The New PW is: $pw3\n" #send_user "The New PW is: $pw4\n" #send "echo \"$pw3\" | passwd --stdin tomcat\r" ##send "echo \"$pw3\" | passwd --stdin root\r" #send "echo \"$pw3\" | passwd --stdin weblogic\r" #send "chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow\r" #send "pam_tally2 -r\rhistory -c\r" #exit login send "exit\r" send "exit\r" interact }genpw.sh
#!/bin/bash #Genarate RANDOM password from IP address >./ippw gen_pw() { length=14; # 设定可以作为密码的字符串 chars='abcdefghijkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789_+@#%&' # 获取上面的字符串的长度,其实可以直接数一下,这不是练一下 shell 嘛 charsLength=`echo $chars | wc -L` # 初始的为空 pw='' # 根据所需密码长度进行循环 for ((i=0;i<$length;i++));do # 取得一个随机数 # $RANDOM 是获取一个小于 32767 的随机数 # 所以除以 32767 就可以得到一个 0 - 1 之间的随机小数 r=`echo $RANDOM/32767 | bc -l` # 根据随机数*字符长度取得一个数字 num=`echo $r*$charsLength | bc` # 将取得数字向上取整,并在字符串中截取对应位置的字符 w=`echo $chars | cut -c$((${num//.*/+1}))` # 字符追加到 pw 这个变量 [ `echo $pw | wc -L` -lt $length ] && pw=$pw$w done # 输出我们想要的密码 echo $pw } for IP in `cat ./ip` do PW=$(gen_pw) PW_Web=$(gen_pw) IPPW=$(echo $IP | awk -F "." '{print $3$4}') PW1="Tomcat@!" PW2="Root@!" echo -e "$IP\t$PW1$IPPW\t$PW2$IPPW\t$PW\t$PW_Web" >>./ippw #echo -e "$IP\t$PW1$IPPW\t$PW\t$PW_Web" >>./ippw done cp ./ippw /tmp/hoboson.txt执行genpw.sh 根据ip生成密码本 在执行autopasswpCheck.sh检查,执行AutoSet.sh修改密码 注意su - 切换中英文 Passwd: 还是密码:
