1
0

mssh.exp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/expect --
  2. if { [llength $argv] < 4 } {
  3. puts "Usage: $argv0 ip user passwd port commands timeout"
  4. exit 1
  5. }
  6. match_max 600000
  7. set ipcode [lindex $argv 0]
  8. set ip [exec dc -e $ipcode]
  9. set user [lindex $argv 1]
  10. set passwdcode [lindex $argv 2]
  11. set passwd [exec dc -e $passwdcode]
  12. set portcode [lindex $argv 3]
  13. set port [exec dc -e $portcode]
  14. set commands [lindex $argv 4]
  15. set timeoutflag [lindex $argv 5]
  16. set yesnoflag 0
  17. set timeout $timeoutflag
  18. for {} {1} {} {
  19. # for is only used to retry when "Interrupted system call" occured
  20. spawn /usr/bin/ssh -o GSSAPIAuthentication=no -q -l$user -p$port $ip
  21. expect {
  22. "assword:" {
  23. send "$passwd\r"
  24. break;
  25. }
  26. "yes/no)?" {
  27. set yesnoflag 1
  28. send "yes\r"
  29. break;
  30. }
  31. "FATAL" {
  32. puts "\nCONNECTERROR: $ip occur FATAL ERROR!!!\n"
  33. exit 1
  34. }
  35. timeout {
  36. puts "\nCONNECTERROR: $ip Logon timeout!!!\n"
  37. exit 1
  38. }
  39. "No route to host" {
  40. puts "\nCONNECTERROR: $ip No route to host!!!\n"
  41. exit 1
  42. }
  43. "Connection Refused" {
  44. puts "\nCONNECTERROR: $ip Connection Refused!!!\n"
  45. exit 1
  46. }
  47. "Connection refused" {
  48. puts "\nCONNECTERROR: $ip Connection Refused!!!\n"
  49. exit 1
  50. }
  51. "Host key verification failed" {
  52. puts "\nCONNECTERROR: $ip Host key verification failed!!!\n"
  53. exit 1
  54. }
  55. "Illegal host key" {
  56. puts "\nCONNECTERROR: $ip Illegal host key!!!\n"
  57. exit 1
  58. }
  59. "Connection Timed Out" {
  60. puts "\nCONNECTERROR: $ip Logon timeout!!!\n"
  61. exit 1
  62. }
  63. "Interrupted system call" {
  64. puts "\n$ip Interrupted system call!!!\n"
  65. }
  66. }
  67. }
  68. if { $yesnoflag == 1 } {
  69. expect {
  70. "assword:" {
  71. send "$passwd\r"
  72. }
  73. "yes/no" {
  74. set yesnoflag 2
  75. send "yes\r"
  76. }
  77. }
  78. }
  79. if { $yesnoflag == 2 } {
  80. expect {
  81. "assword:" {
  82. send "$passwd\r"
  83. }
  84. }
  85. }
  86. expect {
  87. "@" {send "$commands \r"}
  88. "assword:" {
  89. send "$passwd\r"
  90. puts "\nPASSWORDERROR: $ip Password error!!!\n"
  91. exit 1
  92. }
  93. }
  94. expect {
  95. "@" {send "sleep 1\r"}
  96. }
  97. expect {
  98. "@" {send "exit\r"}
  99. }
  100. expect eof {
  101. puts "OK_SSH: $ip\n"
  102. exit 0;
  103. }