【问题标题】:SyntaxError: net-sftp.rb:48: syntax error, unexpected tCONSTANT puts 'File transfer complete' in Ruby from Filezilla Client to localhostSyntaxError:net-sftp.rb:48:语法错误,意外的 tCONSTANT 将 Ruby 中的“文件传输完成”从 Filezilla 客户端放到本地主机
【发布时间】:2023-07-15 17:01:01
【问题描述】:

我正在使用 ConEmu 运行 JRuby 进行自动化测试,以将文件从我的 Filezila 客户端放到我的本地主机。它不应该是远程服务器。当我尝试在 Ruby 中运行以下代码以建立连接时出现错误。在此处传输文件并不那么重要。

道歉:很快就会解决。

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: syntax error, unexpected tCONSTANT puts 'File transfer complete'


* 编辑 *

***现在您将代码放入注释中,它会出现如下错误:

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

【问题讨论】:

  • 请先修正缩进。很难检查块在哪里。请注意您的代码中的这一行:puts ‘Disconnecting from remote server' Opening qoute is different to your end qoute。
  • 感谢您纠正我的开头和结尾报价 - 我没有注意到这一点。我不确定我是否可以缩进它,因为它实际上是一个代码的一部分?
  • 我不认为你 can't 缩进它。只需编辑您的问题并在您的问题中的代码中执行适当的意图。缩进是一种最佳实践,如果没有适当的缩进,很难阅读您的代码。
  • 好的,谢谢 - 目前正在调查。现在当我运行 jruby net-sftp.rb 时会出现这个 SyntaxError: net-sftp.rb:46: syntax error, unexpected kEND
  • 错误信息如下所示:LoadError: no such file to load -- net-ssh require at org/jruby/RubyKernel.java:939 require at C:/jruby-9.0.4.0/lib/ruby /stdlib/rubygems/core_ext/kernel_require.rb:54 at net-sftp.rb:1

标签: ruby-on-rails localhost syntax-error filezilla


【解决方案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

【讨论】: