test.expect 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/expect -f
  2. # set Variables
  3. # /home/sven/.docker/machine/machines/sven-test/id_rsa
  4. set sshkey [lrange $argv 0 0]
  5. # [email protected]
  6. set sshurl [lrange $argv 1 1]
  7. set username [lrange $argv 2 2]
  8. set password [lrange $argv 3 3]
  9. set command ""
  10. append command [lrange $argv 4 end]
  11. set timeout -1
  12. proc runcmd { username password cmd } {
  13. send_user "<< username: $username"
  14. send_user "<< password: $password"
  15. send_user "<< cmd: $cmd"
  16. set done 0;
  17. while {$done == 0} {
  18. expect {
  19. "*?login:" {
  20. send -- "$username\r"
  21. }
  22. "*?assword:" {
  23. send -- "$password\r"
  24. #send -- "\r"
  25. }
  26. "*?:~#" {
  27. send -- "$cmd\r"
  28. set done 1
  29. }
  30. "*?Reached target Shutdown." {
  31. set done 1
  32. }
  33. }
  34. }
  35. }
  36. spawn ssh -F /dev/null -o PasswordAuthentication=no -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=quiet -o ConnectionAttempts=3 -o ConnectTimeout=10 -o ControlMaster=no -o ControlPath=none -i $sshkey $sshurl
  37. match_max 100000
  38. send -- "\r"
  39. set running [ runcmd $username $password $command ]
  40. expect {
  41. "*? (yes/no)?" {
  42. send -- "no\r"
  43. expect "# "
  44. }
  45. "# " {
  46. }
  47. "*?Restarting system" {
  48. }
  49. "*?kexec_core: Starting new kernel" {
  50. }
  51. }
  52. send_user "<< DONE expect"
  53. send_user "<<"