shell脚本:
- #!/bin/bash
- time=`date +%Y%m%d%H%M`
- cat ip.list | while read line
- do
- a=(0 1 2 3 4 5 6 7 8 9 a b c d e f g h q m o p A B C D E F G H Q M O P @ $ % ^)
- newpass=`for ((i=0;i<64;i++));do echo -n ${a[$RANDOM % ${#a[*]}]};done;echo`
- ip=`echo $line |awk '{print $1}'`
- user=`echo $line |awk '{print $2}'`
- pass=`echo $line |awk '{print $3}'`
- echo "$ip $user $newpass" >> $time-ip.list
- expect changp.exp $ip $user $pass $newpass >> /tmp/changp-$time.log
- done
expect脚本:
- #!/usr/local/bin/expect
- set timeout 8
- set ip [lindex $argv 0]
- set user [lindex $argv 1]
- set pass [lindex $argv 2]
- set newpass [lindex $argv 3]
- if {$argc!=4}{
- puts stderr "Usage:your arguments is error!"
- exit 1
- }
- spawn ssh $user@$ip
- expect {
- "(yes/no)?" {send "yes\r";exp_continue}
- "password:" {send "$pass\r"}
- }
- expect "#"
- send "passwd $user"
- expect "password:"
- send "$newpass"
- send "exit \r"
- expect eof
ip.list格式:
192.169.1.188 root password