Advertisement
Guest User

Untitled

a guest
Oct 4th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. #!/usr/bin/ruby1.8
  2.  
  3. require 'rubygems'
  4. require 'net/ssh'
  5. require 'net/ssh/multi'
  6. require 'net/scp'
  7. require "highline/import"
  8. require 'getoptlong'
  9.  
  10. result = GetoptLong.new(
  11. [ "--user", "-u", GetoptLong::REQUIRED_ARGUMENT ],
  12. [ "--command", "-c", GetoptLong::REQUIRED_ARGUMENT ],
  13. [ "--hostsfile", "-f", GetoptLong::REQUIRED_ARGUMENT ],
  14. [ "--type", "-t", GetoptLong::OPTIONAL_ARGUMENT ],
  15. [ "--remote_path", "-r", GetoptLong::REQUIRED_ARGUMENT ],
  16. [ "--local_path", "-l", GetoptLong::REQUIRED_ARGUMENT ],
  17. [ "--hosts", "-h", GetoptLong::REQUIRED_ARGUMENT ]
  18. )
  19.  
  20. hosts=nil
  21. user=nil
  22. pass=nil
  23. remote_path=nil
  24. local_path=nil
  25. command=nil
  26. type=nil
  27.  
  28. result.each {|opt, arg|
  29. case opt
  30. when "--user"
  31. if arg != ''
  32. user = arg
  33. end
  34. when "--hosts"
  35. hosts = arg.split(',').strip
  36. when "--command"
  37. command = arg
  38. when "--hostsfile"
  39. file=File.open(arg)
  40. hosts = file.readlines
  41. when "--type"
  42. if arg =~ /connect|download|upload/
  43. type = arg
  44. end
  45. when "--remote_path"
  46. remote_path = arg
  47. when "--local_path"
  48. local_path = arg
  49. end
  50. }
  51.  
  52. pass = ask("Enter your password: ") { |q| q.echo = '*' }
  53.  
  54. def connect(hosts, user, pass, command)
  55.  
  56. @hosts = hosts
  57. @user = user
  58. @pass = pass
  59. @command = command
  60.  
  61. Net::SSH::Multi.start do |session|
  62.  
  63. @hosts.each {|host|
  64. session.use host, :user => @user, :password => @pass
  65. }
  66.  
  67. session.exec @command
  68. session.loop
  69.  
  70. end
  71. end
  72.  
  73. def download(hosts, user, pass, remote_path, local_path)
  74.  
  75. @hosts = hosts
  76. @user = user
  77. @pass = pass
  78. @remote_path = remote_path
  79. @local_path = local_path
  80.  
  81. @hosts.each {|host|
  82.  
  83. @folder = @local_path +'/'+ host
  84.  
  85. if File.exists?(@folder) && File.directory?(@folder) then
  86. Dir.chdir(@folder)
  87. Dir.glob("*") do |files|
  88. File.unlink(files)
  89. end
  90. else
  91. Dir.mkdir(@folder)
  92. end
  93.  
  94. Net::SSH.start(host, @user, {:password => @pass,}) do |ssh|
  95. ssh.scp.download! @remote_path, @folder
  96. end
  97.  
  98. }
  99.  
  100. end
  101.  
  102. def upload(hosts, user, pass, remote_path, local_path)
  103.  
  104. @hosts = hosts
  105. @user = user
  106. @pass = pass
  107. @remote_path = remote_path
  108. @local_path = local_path
  109.  
  110. @hosts.each {|host|
  111.  
  112. Net::SSH.start(host, @user, {:password => @pass,}) do |ssh|
  113. ssh.scp.upload! @local_path, @remote_path
  114. end
  115.  
  116. }
  117. end
  118.  
  119. case type
  120. when /connect/
  121. connect(hosts, user, pass, command)
  122. when /download/
  123. download(hosts, user, pass, remote_path, local_path)
  124. when /upload/
  125. upload(hosts, user, pass, remote_path, local_path)
  126. else
  127. connect(hosts, user, pass, command)
  128. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement