Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby1.8
- require 'rubygems'
- require 'net/ssh'
- require 'net/ssh/multi'
- require 'net/scp'
- require "highline/import"
- require 'getoptlong'
- result = GetoptLong.new(
- [ "--user", "-u", GetoptLong::REQUIRED_ARGUMENT ],
- [ "--command", "-c", GetoptLong::REQUIRED_ARGUMENT ],
- [ "--hostsfile", "-f", GetoptLong::REQUIRED_ARGUMENT ],
- [ "--type", "-t", GetoptLong::OPTIONAL_ARGUMENT ],
- [ "--remote_path", "-r", GetoptLong::REQUIRED_ARGUMENT ],
- [ "--local_path", "-l", GetoptLong::REQUIRED_ARGUMENT ],
- [ "--hosts", "-h", GetoptLong::REQUIRED_ARGUMENT ]
- )
- hosts=nil
- user=nil
- pass=nil
- remote_path=nil
- local_path=nil
- command=nil
- type=nil
- result.each {|opt, arg|
- case opt
- when "--user"
- if arg != ''
- user = arg
- end
- when "--hosts"
- hosts = arg.split(',').strip
- when "--command"
- command = arg
- when "--hostsfile"
- file=File.open(arg)
- hosts = file.readlines
- when "--type"
- if arg =~ /connect|download|upload/
- type = arg
- end
- when "--remote_path"
- remote_path = arg
- when "--local_path"
- local_path = arg
- end
- }
- pass = ask("Enter your password: ") { |q| q.echo = '*' }
- def connect(hosts, user, pass, command)
- @hosts = hosts
- @user = user
- @pass = pass
- @command = command
- Net::SSH::Multi.start do |session|
- @hosts.each {|host|
- session.use host, :user => @user, :password => @pass
- }
- session.exec @command
- session.loop
- end
- end
- def download(hosts, user, pass, remote_path, local_path)
- @hosts = hosts
- @user = user
- @pass = pass
- @remote_path = remote_path
- @local_path = local_path
- @hosts.each {|host|
- @folder = @local_path +'/'+ host
- if File.exists?(@folder) && File.directory?(@folder) then
- Dir.chdir(@folder)
- Dir.glob("*") do |files|
- File.unlink(files)
- end
- else
- Dir.mkdir(@folder)
- end
- Net::SSH.start(host, @user, {:password => @pass,}) do |ssh|
- ssh.scp.download! @remote_path, @folder
- end
- }
- end
- def upload(hosts, user, pass, remote_path, local_path)
- @hosts = hosts
- @user = user
- @pass = pass
- @remote_path = remote_path
- @local_path = local_path
- @hosts.each {|host|
- Net::SSH.start(host, @user, {:password => @pass,}) do |ssh|
- ssh.scp.upload! @local_path, @remote_path
- end
- }
- end
- case type
- when /connect/
- connect(hosts, user, pass, command)
- when /download/
- download(hosts, user, pass, remote_path, local_path)
- when /upload/
- upload(hosts, user, pass, remote_path, local_path)
- else
- connect(hosts, user, pass, command)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement