当前位置:首页 > linux教程 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2015-04-25 15:57:11 浏览: 评论:0 

使用expect前,我们是需要先安装了,否则expect是不可用的,下面一起来给各位总结两个expect批量修改用户密码脚本.

1、使用expect前,需要先安装两个rpm包,代码如下:

  1. # rpm -ihv expect-5.43.0-8.el5.i386.rpm 
  2. # rpm -ihv expect-devel-5.43.0-8.el5.i386.rpm 

2、批量修改密码的脚本,代码如下:

  1. #!/usr/bin/expect 
  2. #yemaosheng.com 
  3. if { $argc<2 } { 
  4.     send_user "usage: $argv0 <host file> <cmd file> \n" 
  5.     exit 
  6. } 
  7.  
  8. # 机器列表数据格式:  IP  端口  旧密码  新密码 
  9. set hostfile    [ open [lindex $argv 0] ] 
  10. # 命令列表数据格式:  一条命令一行 
  11. set cmdfile    [ open [lindex $argv 1] ] 
  12.  
  13. # 数据文件分割符,默认为空格 
  14. set part "\ " 
  15.  
  16. # 过滤关键字 
  17. set key_password "password:\ " 
  18. set key_init "\(yes/no\)\?\ " 
  19. set key_confirm "'yes'\ or\ 'no':\ " 
  20. set key_ps "*]#\ " 
  21. set key_newpassword "UNIX password:\ " 
  22. set timeout 30 
  23.  
  24. log_file ./exprct.log 
  25. match_max 20480 
  26.  
  27. while {[gets $hostfile _hosts_] >= 0} { 
  28.     set hosts [string trim $_hosts_] 
  29.     set str_index [string first $part $hosts] 
  30.     set host [string trim [string range $hosts 0 $str_index]] 
  31.     set temp [string trim [string range $hosts [expr $str_index + 1] [string length $hosts]]] 
  32.     set str_index [string first $part $temp] 
  33.  
  34.     if { $str_index == -1 } { 
  35.         set port 22 
  36.         set pass $temp 
  37.         set newpass $temp 
  38.     } else { 
  39.         set port [string trim [string range $temp 0 $str_index]] 
  40.         set temp_pass [string trim [string range $temp [expr $str_index + 1] [string length $temp]]] 
  41.         set str_index [string first $part $temp_pass] 
  42.         set pass [string trim [string range $temp_pass 0 $str_index]] 
  43.         set newpass [string trim [string range $temp_pass [expr $str_index + 1] [string length $temp_pass]]] 
  44.     } 
  45.  
  46.     spawn ssh -p $port $host 
  47.     while {1} { 
  48.         expect { 
  49.             "$key_password" { 
  50.                 send "$pass\r" 
  51.             } 
  52.             "$key_init" { 
  53.                 send "yes\r" 
  54.             } 
  55.             "$key_confirm" { 
  56.                 send "yes\r" 
  57.             } 
  58.             "$key_ps" { 
  59.                 while {[gets $cmdfile cmd] >= 0} { 
  60.                     send "$cmd\r" 
  61.                     expect { 
  62.                         "$key_ps" { 
  63.                             continue 
  64.                         } 
  65.                         "$key_newpassword" { 
  66.                             send "$newpass\r" 
  67.                             expect "$key_newpassword" { 
  68.                                 send "$newpass\r" 
  69.                                 expect "$key_ps" 
  70.                                 continue 
  71.                             } 
  72.                         } 
  73.                     } 
  74.                 } 
  75.                 seek $cmdfile 0 start 
  76.                 send_user "\r" 
  77.                 break 
  78.             } 
  79.             timeout { 
  80.                 puts "$host timeout\n" 
  81.                 break  //phpfensi.com 
  82.             } 
  83.         } 
  84.     } 
  85.     send "exit\r" 
  86.     close 
  87.     wait 
  88. } 
  89.  
  90. close $hostfile 
  91. close $cmdfile 
  92.  
  93. exit 

3、批量修改密码的脚本.,用whereis expect确定expect位置,代码如下:

  1. [root@rac1 ~]# whereis expect 
  2. expect: /usr/bin/expect 
  3. #!/usr/bin/expect 
  4. #设置变量 
  5. set timeout 10 
  6. set USERNAME etnet 
  7. set PASSWORD 123456 
  8. #一个循环,说明对哪些机器进行操作 
  9. foreach host { 
  10. 192.168.151.89 
  11. 192.168.151.90 
  12. } { 
  13. spawn ssh  
  14. -l root ${host} 
  15. #ssh首次登陆的验证,exp_continue会继续执行下一循环 
  16. expect { 
  17.         "no)?" {send "yes\r";exp_continue} 
  18.         "password:" {send "123456\r"} 
  19. } 
  20. #每个expect捕获一个提示符,send发送一条命令,命令以\r结尾。 
  21. expect "]*" 
  22. send "passwd ${USERNAME}\r" 
  23. expect "password:" 
  24. send "${PASSWORD}\r" 
  25. expect "password:" 
  26. send "${PASSWORD}\r" 
  27. expect "]*" 
  28. send "exit\r" 
  29. } 

补充: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开始,分别表示第一个,第二个,第三个....参数.

Tags: linux脚本 expect批量修改

分享到: