Class: SSHClient
- Inherits:
-
Object
- Object
- SSHClient
- Defined in:
- lib/reparto.rb
Instance Method Summary collapse
- #<<(action) ⇒ Object
-
#cmd(cmd) ⇒ Object
- Ejecuta comando en cliente ::cmd
-
comando en cliente.
-
#cp(local, remote) ⇒ Object
- Copia archivo local a remote ::local
- archivo local ::remote
-
archivo remote.
-
#cp_dir(local, remote) ⇒ Object
- Copia directoria local a remote ::local
- directorio local ::remote
-
directorio remote.
-
#do ⇒ Object
ejecuta acciones.
-
#initialize(ip, port, username, password) ⇒ SSHClient
constructor
A new instance of SSHClient.
-
#update_dir(local, remote) ⇒ Object
- Actualiza directorio remote en caso de cambio ::dir
-
directorio local/remote.
Constructor Details
#initialize(ip, port, username, password) ⇒ SSHClient
Returns a new instance of SSHClient.
40 41 42 43 44 45 46 |
# File 'lib/reparto.rb', line 40 def initialize(ip, port, username, password) @ip = ip @port = port @username = username @password = password @actions = [] end |
Instance Method Details
#<<(action) ⇒ Object
167 168 169 |
# File 'lib/reparto.rb', line 167 def <<(action) @actions << action end |
#cmd(cmd) ⇒ Object
Ejecuta comando en cliente
- ::cmd
-
comando en cliente
173 174 175 |
# File 'lib/reparto.rb', line 173 def cmd(cmd) @actions << {:action => :cmd, :cmd => cmd} end |
#cp(local, remote) ⇒ Object
Copia archivo local a remote
- ::local
-
archivo local
- ::remote
-
archivo remote
180 181 182 |
# File 'lib/reparto.rb', line 180 def cp(local, remote) @actions << {:action => :cp, :local =>local, :remote =>remote} end |
#cp_dir(local, remote) ⇒ Object
Copia directoria local a remote
- ::local
-
directorio local
- ::remote
-
directorio remote
187 188 189 |
# File 'lib/reparto.rb', line 187 def cp_dir(local, remote) @actions << {:action => :cpdir, :local => local, :remote => remote} end |
#do ⇒ Object
ejecuta acciones
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/reparto.rb', line 49 def do $logs.debug($t.reparto.conecting(@ip, @port)) Net::SSH.start(@ip, @username, :password => @password, :port => @port) do |ssh| @actions.each do |action| name = action[:action] case name when :cmd cmd = action[:cmd] $logs.debug("%s command execute: %s" % [@ip,cmd]) r = ssh.exec! cmd $logs.debug("%s command return: %s" % [@ip,r]) when :cp local = action[:local] remote = action[:remote] unless File.exist? local $logs.error("%s Not found %s to copy on local machine" % [@ip,local]) next end begin ssh.sftp.connect do |sftp| begin remote = File.join(remote, File.basename(local)) if ssh.sftp.file.directory? remote rescue end sftp.upload!(local, remote) $logs.debug("%s cp: %s %s" % [@ip, local, remote]) end rescue Net::SFTP::StatusException => e $logs.error("%s error[%s] cp: %s %s" % [@ip, e.description, local, remote]) end #Copia directorio local con remoto solo si no existe when :cpdir local = action[:local] remote = action[:remote] unless File.directory? local $logs.error("%s Only dir allow for cpdir not %s" % [@ip, local]) end begin ssh.sftp.upload!(local, remote) $logs.debug("%s cpdir: %s %s" % [@ip, local, remote]) rescue Net::SFTP::StatusException => e case e.code when 4 $logs.error("%s error[Can't copy directory already exist, use updatedir] cpdir: %s %s" % [@ip, local, remote]) else $logs.error("%s error[%s] cpdir: %s %s" % [@ip, e.description, local, remote]) end rescue ArgumentError $logs.error("%s error[Need directory to upload] cpdir: %s %s" % [@ip, local, remote]) end #Actualiza directory local con remoto when :updatedir ssh.sftp.connect do |sftp| local_dir = action[:local] remote_dir = action[:remote] $logs.debug("Checking for files which need updating %s" % @ip) Find.find(local_dir) do |file| local_file = file remote_file = File.join(remote_dir, local_file.sub(local_dir, '')) #actualiza directorio no existene en el remoto if File.directory? file begin ssh.sftp.file.directory? remote_file rescue Net::SFTP::StatusException => e if e.code == 2 sftp.upload!(file, remote_file) $logs.debug("%s Updating, new dir %s" % [@ip, remote_file]) else $logs.error("%s error[%s] updating new dir %s" % [@ip, e.description, remote_file]) end end next end #actualiza comparando mtime de stat #o fechas de modificacion begin lstat = File.stat(local_file) rstat = sftp.stat!(remote_file) if lstat.mtime > Time.at(rstat.mtime) $logs.debug("%s Updating %s" % [@ip, remote_file]) sftp.upload!(local_file, remote_file) sftp.setstat!(remote_file, :permissions => lstat.mode) end rescue Net::SFTP::StatusException => e if e.code == 2 sftp.upload!(local_file, remote_file) $logs.debug("%s Updating new file %s" % [@ip, remote_file]) else $logs.error("%s error[%s] updating new file %s" % [@ip, e.description, local_file]) end rescue Errno::ENOENT $logs.error("%s error[No such directory] updating: %s %s" % [@ip, local_file, remote_file]) end end end end end end end |
#update_dir(local, remote) ⇒ Object
Actualiza directorio remote en caso de cambio
- ::dir
-
directorio local/remote
193 194 195 |
# File 'lib/reparto.rb', line 193 def update_dir(local, remote) @actions << {:action => :updatedir, :local => local, :remote => remote} end |