linux中expect批量更改用户密码的脚本

  使用expect前,我们是需要先安装了,否则expect是不可用的,下面一起来给各位总结两个expect批量修改用户密码脚本.
 
        补充:expect用法:
 
  1.[#!/usr/bin/expect]
 
  这一行告诉操作系统脚本里的代码使用那一个shell来执行,这里的expect其实和linux下的bash、windows下的cmd是一类东西.
 
  注意:这一行需要在脚本的第一行。
 
  2.[set timeout 30]
 
  基本上认识英文的都知道这是设置超时时间的,现在你只要记住他的计时单位是:秒   ,timeout -1 为永不超时.
 
  3.[spawn ssh -l username 192.168.1.1]
 
  spawn是进入expect环境后才可以执行的expect内部命令,如果没有装expect或者直接在默认的SHELL下执行是找不到spawn命令的,所以不要用 “which spawn“之类的命令去找spawn命令,好比windows里的dir就是一个内部命令,这个命令由shell自带,你无法找到一个dir.com 或 dir.exe 的可执行文件.
 
  它主要的功能是给ssh运行进程加个壳,用来传递交互指令.
 
  4.[expect "password:"]
 
  这里的expect也是expect的一个内部命令,有点晕吧,expect的shell命令和内部命令是一样的,但不是一个功能,习惯就好了,这个命令的意思是判断上次输出结果里是否包含“password:”的字符串,如果有则立即返回,否则就等待一段时间后返回,这里等待时长就是前面设置的30秒.
 
  5.[send "ispass\r"]
 
  这里就是执行交互动作,与手工输入密码的动作等效.
 
  温馨提示:命令字符串结尾别忘记加上“\r”,如果出现异常等待的状态可以核查一下.
 
  6.[interact]
 
  执行完成后保持交互状态,把控制权交给控制台,这个时候就可以手工操作了,如果没有这一句登录完成后会退出,而不是留在远程终端上,如果你只是登录过去执行.
 
  7.$argv 参数数组
 
  expect脚本可以接受从bash传递过来的参数,可以使用[lindex $argv n]获得,n从0开始,分别表示第一个,第二个,第三个....参数.

  1、使用expect前,需要先安装两个rpm包,代码如下:
 
  # rpm -ihv expect-5.43.0-8.el5.i386.rpm
  # rpm -ihv expect-devel-5.43.0-8.el5.i386.rpm
  2、批量修改密码的脚本,代码如下:
 
  #!/usr/bin/expect
  #yemaosheng.com
  if { $argc<2 } {
      send_user "usage: $argv0 <host file> <cmd file> \n"
      exit
  }
  
  # 机器列表数据格式:  IP  端口  旧密码  新密码
  set hostfile    [ open [lindex $argv 0] ]
  # 命令列表数据格式:  一条命令一行
  set cmdfile    [ open [lindex $argv 1] ]
  
  # 数据文件分割符,默认为空格
  set part "\ "
  
  # 过滤关键字
  set key_password "password:\ "
  set key_init "\(yes/no\)\?\ "
  set key_confirm "'yes'\ or\ 'no':\ "
  set key_ps "*]#\ "
  set key_newpassword "UNIX password:\ "
  set timeout 30
  
  log_file ./exprct.log
  match_max 20480
  
  while {[gets $hostfile _hosts_] >= 0} {
      set hosts [string trim $_hosts_]
      set str_index [string first $part $hosts]
      set host [string trim [string range $hosts 0 $str_index]]
      set temp [string trim [string range $hosts [expr $str_index + 1] [string length $hosts]]]
      set str_index [string first $part $temp]
  
      if { $str_index == -1 } {
          set port 22
          set pass $temp
          set newpass $temp
      } else {
          set port [string trim [string range $temp 0 $str_index]]
          set temp_pass [string trim [string range $temp [expr $str_index + 1] [string length $temp]]]
          set str_index [string first $part $temp_pass]
          set pass [string trim [string range $temp_pass 0 $str_index]]
          set newpass [string trim [string range $temp_pass [expr $str_index + 1] [string length $temp_pass]]]
      }
  
      spawn ssh -p $port $host
      while {1} {
          expect {
              "$key_password" {
                  send "$pass\r"
              }
              "$key_init" {
                  send "yes\r"
              }
              "$key_confirm" {
                  send "yes\r"
              }
              "$key_ps" {
                  while {[gets $cmdfile cmd] >= 0} {
                      send "$cmd\r"
                      expect {
                          "$key_ps" {
                              continue
                          }
                          "$key_newpassword" {
                              send "$newpass\r"
                              expect "$key_newpassword" {
                                  send "$newpass\r"
                                  expect "$key_ps"
                                  continue
                              }
                          }
                      }
                  }
                  seek $cmdfile 0 start
                  send_user "\r"
                  break
              }
              timeout {
                  puts "$host timeout\n"
                  break  //phpfensi.com
              }
          }
      }
      send "exit\r"
      close
      wait
  }
  
  close $hostfile
  close $cmdfile
  
  exit
  3、批量修改密码的脚本.,用whereis expect确定expect位置,代码如下:
 
  [root@rac1 ~]# whereis expect
  expect: /usr/bin/expect
  #!/usr/bin/expect
  #设置变量
  set timeout 10
  set USERNAME etnet
  set PASSWORD 123456
  #一个循环,说明对哪些机器进行操作
  foreach host {
  192.168.151.89
  192.168.151.90
  } {
  spawn ssh  
  -l root ${host}
  #ssh首次登陆的验证,exp_continue会继续执行下一循环
  expect {
          "no)?" {send "yes\r";exp_continue}
          "password:" {send "123456\r"}
  }
  #每个expect捕获一个提示符,send发送一条命令,命令以\r结尾。

  

dawei

【声明】:石嘴山站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。