+ -
当前位置:首页 → 问答吧 → expect 小脚本一例

expect 小脚本一例

时间:2011-01-27

来源:互联网

  1. #!/usr/bin/expect

  2. if {$argc!=3} {
  3. send_user "Usage: $argv0 {Array IP} {Password} {CMD}\n\n"
  4. exit
  5. }
  6. #log_file ./out
  7. set IP [lindex $argv 0]
  8. set Password [lindex $argv 1]
  9. set CMD [lindex $argv 2]

  10. spawn ssh admin@$IP
  11. #spawn ssh root@$IP
  12. #expect {
  13. #"Are you" { send "yes\n"
  14. #          }
  15. #}
  16. #set timeout 50
  17. expect {
  18.         "Password:" {
  19.                 exec sleep 1
  20.                 send "${Password}\r"
  21.                         }
  22.         "*continue connecting*" {
  23.                          exec sleep 1
  24.                          send "yes\r"
  25.                          expect  "*Password:" {
  26.                                  exec sleep 1
  27.                                   send "${Password}\r"
  28.                                  }         
  29.                         }
  30. }
  31. #exec sleep 1
  32. #send "${Password}\r"
  33. expect {
  34. "*Password*" { send_user "\n1:Password error!\n"
  35.                exit
  36.              }
  37. "*already*" { send_user "\n2:Repeated login!\n"
  38.               exit
  39.             }
  40. "OceanStor: admin>" { send "${CMD}\r"
  41.                     }
  42. }
  43. #send_user "sleep"
  44. #expect "OceanStor: admin>"
  45. #send "${CMD}\r"
  46. #exec sleep 5

  47. expect         "*>"
  48. #send "exit\r"
  49. send "exit\r"
  50. #send "exit\r"
  51. expect "*closed*"
  52. exit
  53. #exec sleep 10
复制代码
:em27:

作者: chinaboywg   发布时间: 2011-01-27

回复 chinaboywg


    可用不?说下环境之类的,大家都在找这个呢

作者: expert1   发布时间: 2011-01-27

  1. et prefix "\033\[1;31m>>>\033\[0m"

  2. proc usage {} {
  3.     regsub ".*/" $::argv0 "" name
  4.     send_user "Usage:\n"
  5.     send_user "    $name \[user@]host password\n"
  6.     send_user "\n"
  7.     send_user "Report bugs to <\033\[1;[email protected]\033\[0m>\n"
  8.     exit 1
  9. }

  10. proc check_id_files {} {
  11.     if {! [file exists $::id_file]} {
  12.   send_user "$::prefix id file not found, try creating ...\n"
  13.   if {[catch { spawn ssh-keygen -t rsa } error]} {
  14.       send_error "$::prefix $error\n"
  15.       exit 1
  16.   }
  17.   expect -nocase -re "\(.*\):"
  18.   send -- "\r"
  19.   expect -nocase -re "passphrase.*:"
  20.   send -- "\r"
  21.   expect -nocase -re "passphrase.*again:"
  22.   send -- "\r"
  23.   expect eof
  24.   send_user "$::prefix id file successfully created\n"
  25.     }
  26. }

  27. proc remove_known_hosts_entry {host} {
  28.     regsub ".*/" $::argv0 "" name
  29.     set tmp_file "/tmp/$name.tmp"
  30.     set known_hosts "$::env(HOME)/.ssh/known_hosts"
  31.     send_user "$::prefix trying to remove '$host' from ~/.ssh/known_hosts ... "
  32.     if {[catch {
  33.   set fd_known_hosts [open $known_hosts r]
  34.   set fdTmp [open $tmp_file w]
  35.   while 1 {
  36.       gets $fd_known_hosts line
  37.       if [eof $fd_known_hosts] {
  38.     break
  39.       }
  40.       if [regexp "(\[^, ]+,)*${host}(,\[^, ]+)* " $line] {
  41.     continue
  42.       }
  43.       puts $fdTmp $line
  44.   }
  45.   close $fd_known_hosts
  46.   close $fdTmp
  47.   file rename -force $tmp_file $known_hosts
  48.   send_user "OK\n"
  49.     } error]} {
  50.   send_user "failed\n"
  51.   send_user "$::prefix $error\n"
  52.   exit 1
  53.     }
  54. }
复制代码

作者: chinaboywg   发布时间: 2011-01-27

  1. ## get host and password from command line parameters
  2. #if {[llength $argv] != 3} {
  3. #    usage
  4. #}
  5. set user@host [lindex $argv 0]
  6. set passwd [lindex $argv 1]
  7. set cmd ""
  8. set src ""
  9. set dst ""
  10. set address_pos ""
  11. if {[llength $argv] == 3} {
  12.     set cmd [lindex $argv 2]
  13. }
  14. if {[llength $argv] == 5} {
  15.     set src [lindex $argv 2]
  16.     set dst [lindex $argv 3]
  17.     set address_pos [lindex $argv 4]
  18. }


  19. ## create public key file if not found
  20. set id_file "$env(HOME)/.ssh/id_rsa.pub"
  21. #check_id_files

  22. ## ssh to host
  23. set yes_no 0
  24. set ok_string SUCCESS
  25. set timeout 66
  26. set done 0
  27. while {!$done} {
  28.     spawn ssh ${user@host} echo $ok_string
  29.     expect {
  30.   -nocase -re "yes/no" {
  31.       set yes_no 1
  32.       send -- "yes\r"
  33.       set done 1
  34.   }
  35.   -nocase -re "password: " {
  36.       set done 1
  37.   }
  38.   $ok_string {
  39.       send_user "$prefix ok\n"
  40.       exit 0
  41.   }
  42.   "@@@@@@@@@@@@@@@@@@@@" {
  43.       expect eof
  44.       set indexOfAtSign [string first "@" ${user@host}]
  45.       incr indexOfAtSign
  46.       set hostname [string range ${user@host} $indexOfAtSign end]
  47.       remove_known_hosts_entry $hostname
  48.   }
  49.   eof {
  50.       send_error "$prefix failed\n"
  51.       exit 1
  52.   }
  53.   timeout {
  54.       send_error "$prefix timeout\n"
  55.             #modified by z60006453 for problem AB6D00409 and AB6D00410 start
  56.             exit 16
  57.       #exit 1
  58.             #modified by z60006453 for problem AB6D00409 and AB6D00410 end
  59.   }
  60.     }
  61. }

  62. if {$yes_no} {
  63.     expect {
  64.   $ok_string {
  65.       send_user "$prefix ok\n"
  66.       exit 0
  67.   }
  68.   -nocase -re "password: " {}
  69.     }
  70. }
  71. send -- "$passwd\r"
  72. expect {
  73.     -nocase "try again" {
  74.   send_error "$prefix passwd error\n"

  75.   exit 11

  76.     }
  77.   
  78.     -nocase "password:" {
  79.   send_error "$prefix passwd error\n"
  80.   
  81.   exit 11
  82.   

  83.   
  84.     }

  85.     $ok_string {}
  86. }
  87. expect eof

  88. ##solve the expect&ssh dead process(a little probability) problem.
  89. ##make the frequency of spawn ssh not very high
  90. sleep 1

  91. ## append public key file to remote host's ~/.ssh/authorized_keys
  92. #if {[catch {
  93. #    set IDFILE [open $id_file RDONLY]
  94. #    set pub_key [read $IDFILE]
  95. #    close $IDFILE
  96. #} error]} {
  97. #    send_error "$prefix $error\n"
  98. #    exit 1
  99. #}
  100. #set pub_key [string trimright $pub_key "\r\n"]

  101. ###a little more than max install time
  102. set timeout 10806

  103. if {[llength $argv] == 3} {
  104. ##solve the expect&ssh dead process(a little probability) problem.
  105.   spawn ssh ${user@host} "$cmd"
  106. }
  107. if {[llength $argv] == 5} {
  108.     if { $address_pos == "RIGHT"} {
  109. ##solve the expect&ssh dead process(a little probability) problem.
  110.       spawn /opt/dms/bin/scp_echo -r "$src" "${user@host}:$dst"
  111.     }
  112.     if { $address_pos == "LEFT" } {
  113. ##solve the expect&ssh dead process(a little probability) problem.
  114.       spawn /opt/dms/bin/scp_echo -r "${user@host}:$src" "$dst"
  115.     }
  116. }
  117. expect -nocase -re "password:"
  118. send -- "$passwd\r"

  119. ##solve the expect&ssh dead process(a little probability) problem.
  120. ##replace expect eof to expect "--exit code=.*=";
  121. ##when expect eof, there are no eof come forever with a little probability.  
  122. expect {
  123.    -nocase -re "--exit code=.*=" {   
  124.           exit 0
  125.    }                             
  126.    timeout {
  127.     exit 16
  128.    }
  129. }

  130. #send_user "$prefix gook luck\n"
  131. ## THE END
复制代码

作者: chinaboywg   发布时间: 2011-01-27