批零修改服务器某个用户的密码(expect命令的使用)

#!/bin/bash

#脚本用于批量修改用户的密码,职业中没有正式接触自动化工具,但是希望自己的一些自动化运维想法能够得到同行的认可与支持,在接下来的时间,将系统学习python语言,以便能有更大突破

expec_cmd ()

{

expect << EOF

set host [lindex $argv 0]

set user [lindex $argv 1]

set pass [lindex $argv 2]

set dirm [lindex $argv 3]

spawn cat ./mimazidian/$dirm  | ssh $user@host chpasswd -m

expect {

"yes/no"

{

  send "yes\n"

  expect "password:"  {send "$pass\n"}}

  "password:"  {send "$pass\n"}

}

expect eof

EOF

}

cat host.ini |while read line

do

  host=`echo $line |awk '{print $1}'`

  user=`echo $line |awk '{print $2}'`

  dirm=`echo $line |awk '{print $3}'`

  pint -c1 $host >/dev/null

  if  [ $? -eq 0 ]

  then

    expect_cmd $host $user $pass $dirm

  else 

    echo "check your $hsot net"

  fi

done

 

 

 

####################

host.ini文件的格式为

机器ip  用户名  用户原始密码  存放用于chpasswd命令读取的密码文件

 

posted @ 2016-08-23 13:03  lihe-linux  阅读(157)  评论(0编辑  收藏  举报