1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-27 15:43:04 +00:00
mpw/lib/Sync/SSH.rb

83 lines
1.8 KiB
Ruby
Raw Normal View History

2014-01-29 22:11:01 +00:00
#!/usr/bin/ruby
# author: nishiki
# mail: nishiki@yaegashi.fr
2015-02-06 23:10:39 +00:00
require 'rubygems'
require 'i18n'
require 'net/ssh'
2015-02-08 19:56:39 +00:00
require 'net/sftp'
2015-02-06 23:10:39 +00:00
2014-01-29 22:11:01 +00:00
module MPW
2015-02-06 23:10:39 +00:00
class SyncSSH
attr_accessor :error_msg
attr_accessor :enable
# Constructor
# @args: host -> the server host
# port -> ther connection port
# gpg_key -> the gpg key
# password -> the remote password
def initialize(host, user, password, path, port=nil)
@error_msg = nil
@enable = false
2014-01-29 22:11:01 +00:00
2015-02-06 23:10:39 +00:00
@host = host
@user = user
@password = password
@path = path
@port = port.instance_of?(Integer) ? 22 : port
end
# Connect to server
# @rtrn: false if the connection fail
def connect
2015-02-08 19:56:39 +00:00
Net::SSH.start(@host, @user, password: @password, port: @port) do |ssh|
2015-02-06 23:10:39 +00:00
@enable = true
2014-01-29 22:11:01 +00:00
end
2015-02-06 23:10:39 +00:00
rescue Exception => e
@error_msg = "#{I18n.t('error.sync.connection')}\n#{e}"
@enable = false
else
return @enable
end
# Get data on server
# @args: gpg_password -> the gpg password
# @rtrn: nil if nothing data or error
2015-02-08 19:56:39 +00:00
def get(file_tmp)
return false if not @enable
2014-01-29 22:11:01 +00:00
2015-02-08 19:56:39 +00:00
puts @path
puts file_tmp
Net::SFTP.start(@host, @user, password: @password, port: @port) do |sftp|
sftp.lstat(@path) do |response|
sftp.download!(@path, file_tmp) if response.ok?
end
2014-01-29 22:11:01 +00:00
end
2015-02-08 19:56:39 +00:00
return true
2015-02-06 23:10:39 +00:00
rescue Exception => e
@error_msg = "#{I18n.t('error.sync.download')}\n#{e}"
2015-02-08 19:56:39 +00:00
return false
2015-02-06 23:10:39 +00:00
end
# Update the remote data
2015-02-08 19:56:39 +00:00
# @args: file_gpg -> the data to send on server
2015-02-06 23:10:39 +00:00
# @rtrn: false if there is a problem
2015-02-08 19:56:39 +00:00
def update(file_gpg)
2015-02-06 23:10:39 +00:00
return true if not @enable
2015-02-08 19:56:39 +00:00
Net::SFTP.start(@host, @user, password: @password, port: @port) do |sftp|
sftp.upload!(file_gpg, @path)
2014-01-29 22:11:01 +00:00
end
2014-01-30 22:08:38 +00:00
2015-02-06 23:10:39 +00:00
return true
rescue Exception => e
@error_msg = "#{I18n.t('error.sync.upload')}\n#{e}"
return false
2014-01-29 22:11:01 +00:00
end
end
end