我正在使用ConEmu运行JRuby进行自动化测试,将文件从我的Filezila客户端放到我的本地主机上。它不应该是远程服务器。当我尝试在Ruby中运行以下代码来建立连接时出错。在这里传输文件并不重要。
APOLOGIES:很快就会解决。
require 'net-ssh'
require 'net-sftp'
require 'dir'
local_path = 'D:\Rubynetssh'
remote_path = '/cguclaim/virtual/data/logs/gwlogs/ClaimCenter/'
file_perm = 0644
dir_perm = 0755
puts 'Connecting to remote server'
Net::SSH.start('server', 'admin', 'password1') do |ssh|
ssh.sftp.connect do |sftp|
puts 'Checking for files which need updating'
Find.find(local_path) do |file|
next if File.stat(file).directory?
local_file = "#{dir}/#{file}"
remote_file = remote_path + local_file.sub(local_path, '')
begin
remote_dir = File.dirname(remote_file)
sftp.stat(remote_dir)
rescue Net::SFTP::Operations::StatusException => e
raise unless e.code == 2
sftp.mkdir(remote_dir, :permissions => dir_perm)
end
begin
rstat = sftp.stat(remote_file)
rescue Net::SFTP::Operations::StatusException => e
raise unless e.code == 2
sftp.put_file(local_file, remote_file)
sftp.setstat(remote_file, :permissions => file_perm)
next
end
if File.stat(local_file).mtime > Time.at(rstat.mtime)
puts "Copying #{local_file} to #{remote_file}"
sftp.put_file(local_file, remote_file)
end
end
end
puts 'Disconnecting from remote server'
end
puts 'File transfer complete'
当我运行以下命令时
jruby net-sftp.rb
这会导致此错误语法
SyntaxError:net-sftp.rb:48:语法错误,意外tCONSTANT置'文件传输完成'
*编辑*
***现在您在评论中输入了代码,它出现错误,如下所示:
1. LoadError: no such file to load -- net-ssh
2. require at org/jruby/RubyKernel.java:939
3. require at
4. C:/jruby-9.0.4.0/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb:54
5. <top> at net-sftp.rb:1
下面的代码对我来说更好地建立了从Filezilla到ConEmu的连接,但是现在我需要进入ClaimCenter文件夹来查看文件是否存在
require "net/ssh"
require "net/sftp"
@hostname = "server"
@username = "admin"
@password = "password1"
@cmd = "ls -la"
res = ""
ssh = Net::SSH.start(@hostname, @username, password: @password) do |ssh|
res = ssh.exec!(@cmd)
end
puts res