mscp.exp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/expect --
  2. proc Usage_Exit {self} {
  3. puts ""
  4. puts "Usage: $self ip user passwd port sourcefile destdir direction bwlimit timeout"
  5. puts ""
  6. puts " sourcefile: a file or directory to be transferred"
  7. puts " 需要拷贝目录时目录名后不要带 /, 否则会拷贝该目录下的所有文件"
  8. puts " destdir: the location that the sourcefile to be put into"
  9. puts " direction: pull or push"
  10. puts " pull: remote -> local"
  11. puts " push: local -> remote"
  12. puts " bwlimit: bandwidth limit, kbit/s, 0 means no limit"
  13. puts " timeout: timeout of expect, s, -1 means no timeout"
  14. puts ""
  15. exit 1
  16. }
  17. if { [llength $argv] < 9 } {
  18. Usage_Exit $argv0
  19. }
  20. set ipcode [lindex $argv 0]
  21. set ip [exec dc -e $ipcode]
  22. set user [lindex $argv 1]
  23. set passwduncode [lindex $argv 2]
  24. set passwd [exec dc -e $passwduncode]
  25. set portcode [lindex $argv 3]
  26. set port [exec dc -e $portcode]
  27. set sourcefile [lindex $argv 4]
  28. set destdir [lindex $argv 5]
  29. set direction [lindex $argv 6]
  30. set bwlimit [lindex $argv 7]
  31. set timeoutflag [lindex $argv 8]
  32. set yesnoflag 0
  33. set timeout $timeoutflag
  34. for {} {1} {} {
  35. # for is only used to retry when "Interrupted system call" occured
  36. if { $direction == "pull" } {
  37. if { $bwlimit > 0 } {
  38. spawn rsync -crazP --delete --bwlimit=$bwlimit -e "/usr/bin/ssh -o GSSAPIAuthentication=no -q -l$user -p$port" $ip:$sourcefile $destdir
  39. } elseif { $bwlimit == 0 } {
  40. spawn rsync -crazP --delete -e "/usr/bin/ssh -o GSSAPIAuthentication=no -q -l$user -p$port" $ip:$sourcefile $destdir
  41. } else {
  42. Usage_Exit $argv0
  43. }
  44. } elseif { $direction == "push" } {
  45. if { $bwlimit > 0 } {
  46. spawn rsync -crazP --delete --bwlimit=$bwlimit -e "/usr/bin/ssh -o GSSAPIAuthentication=no -q -l$user -p$port" $sourcefile $ip:$destdir
  47. } elseif { $bwlimit == 0 } {
  48. spawn rsync -crazP --delete -e "/usr/bin/ssh -o GSSAPIAuthentication=no -q -l$user -p$port" $sourcefile $ip:$destdir
  49. } else {
  50. Usage_Exit $argv0
  51. }
  52. } else {
  53. Usage_Exit $argv0
  54. }
  55. expect {
  56. "assword:" {
  57. send "$passwd\r"
  58. break;
  59. }
  60. "yes/no" {
  61. set yesnoflag 1
  62. send "yes\r"
  63. break;
  64. }
  65. "FATAL" {
  66. puts "\nCONNECTERROR: $ip occur FATAL ERROR!!!\n"
  67. exit 1
  68. }
  69. timeout {
  70. puts "\nCONNECTERROR: $ip Logon timeout!!!\n"
  71. exit 1
  72. }
  73. "No route to host" {
  74. puts "\nCONNECTERROR: $ip No route to host!!!\n"
  75. exit 1
  76. }
  77. "Connection Refused" {
  78. puts "\nCONNECTERROR: $ip Connection Refused!!!\n"
  79. exit 1
  80. }
  81. "Connection refused" {
  82. puts "\nCONNECTERROR: $ip Connection Refused!!!\n"
  83. exit 1
  84. }
  85. "Host key verification failed" {
  86. puts "\nCONNECTERROR: $ip Host key verification failed!!!\n"
  87. exit 1
  88. }
  89. "Illegal host key" {
  90. puts "\nCONNECTERROR: $ip Illegal host key!!!\n"
  91. exit 1
  92. }
  93. "Connection Timed Out" {
  94. puts "\nCONNECTERROR: $ip Logon timeout!!!\n"
  95. exit 1
  96. }
  97. "Interrupted system call" {
  98. puts "\n$ip Interrupted system call!!!\n"
  99. }
  100. }
  101. }
  102. if { $yesnoflag == 1 } {
  103. expect {
  104. "assword:" {
  105. send "$passwd\r"
  106. }
  107. "yes/no)?" {
  108. set yesnoflag 2
  109. send "yes\r"
  110. }
  111. }
  112. }
  113. if { $yesnoflag == 2 } {
  114. expect {
  115. "assword:" {
  116. send "$passwd\r"
  117. }
  118. }
  119. }
  120. expect {
  121. "assword:" {
  122. send "$passwd\r"
  123. puts "\nPASSWORDERROR: $ip Password error!!!\n"
  124. exit 1
  125. }
  126. eof {
  127. puts "OK_SCP: $ip\n"
  128. exit 0;
  129. }
  130. }