博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

好想好好爱你!

badb0y.cublog.cn


用expect完成用户密码的批量修改
1、创建用户/密码对文件
# cat user.password
  test1:123456789
  test2:123456789  #=========END=======================================================
2、创建expect.sh脚本文件
# cat expect.sh
  #!/usr/local/bin/expect
 
  if {$argc<2} {
          send_user "usage: $argv0 username password\n"
          exit
          }
 
          set username [lindex $argv 0]
          set password [lindex $argv 1]
          send_user "execute:$argv0 $username $password\n"
 
  spawn -noecho passwd [lindex $argv 0]      #-noecho不回显spawn正在调用的命令
  expect {
        "*口令*"        {send "$password\n"}
        "*word*"        {send "$password\n"}
        }
  expect {
        "*口令*"        {send "$password\n"}
        "*word*"        {send "$password\n"}
        }
  #expect "*word*"
  #send "$password\n"
  expect eof
  exit
  #=========END=======================================================
3、创建autochg.sh文件,在此文件中调用expect.sh 
 
# cat autochg.sh
  #!/usr/bin/bash
 
  for i in `cat $1`
  do
          USERNAME=${i%:*}   #自右起第一个冒号后的字符都不要
          PASSWORD=${i#*:}   #自左起第一个冒号前的字符都不要
          ./expect.sh $USERNAME $PASSWORD
  done
  #=========END=======================================================
 
# chmod u+x expect.sh autochg.sh
# ./autochg.sh user.password

发表于: 2008-05-05 ,修改于: 2008-05-05 09:41,已浏览101次,有评论0条 推荐 投诉


网友评论

发表评论