Net :: SCP ::错误(SCP没有成功完成()):

时间:2010-12-16 21:48:45

标签: ruby-on-rails

我有一个脚本可以将文件从一个服务器传输到另一个服务器但是它会出错:

Net::SCP::Error (SCP did not finish successfully ()):

任何人都可以帮助我吗?这是我的代码。

Net::SCP.start( 's.com', 'username', :password => 'password' ) do|scp|
  scp.upload!( source, destination )
end

8 个答案:

答案 0 :(得分:10)

我今天遇到了这个问题。结果是我的本地文件(在你的例子中,源代码)指向一个不存在的文件。祝你好运。

答案 1 :(得分:9)

如果要将文件上载到远程服务器上尚不存在的文件夹,也会发生此错误。 SCP中没有隐含文件夹创建

答案 2 :(得分:7)

我有一个稍微不同的错误,包括parens中的退出代码:

Net::SCP::Error Exception: SCP did not finish successfully (1)

我首先想到这可能是因为源文件不存在或目标目录不存在,正如其他人所提到的那样,但结果却是因为我传递的是源文件的路径名对象而不是字符串

my_file = Rails.root.join('config/my_file') # my_file.class => Pathname
scp.upload!(my_file,  "/var/tmp/dev.pub")

<Net::SCP::Error: SCP did not finish successfully (1)>
"gems/net-scp-1.0.4/lib/net/scp.rb:352:in `start_command'",                                                                                                                                                                                                   "gems/net-ssh-2.6.7/lib/net/ssh/connection/channel.rb:590:in `call'",
"gems/net-ssh-2.6.7/lib/net/ssh/connection/channel.rb:590:in `do_close'",                                                                                                                                                                                     "gems/net-ssh-2.6.7/lib/net/ssh/connection/session.rb:580:in `channel_close'",                                                                                                                                                                                "gems/net-ssh-2.6.7/lib/net/ssh/connection/session.rb:459:in `send'",
"gems/net-ssh-2.6.7/lib/net/ssh/connection/session.rb:459:in `dispatch_incoming_packets'",
"gems/net-ssh-2.6.7/lib/net/ssh/connection/session.rb:216:in `preprocess'",                                                                                                                                                                                   "gems/net-ssh-2.6.7/lib/net/ssh/connection/session.rb:200:in `process'",
"gems/net-ssh-2.6.7/lib/net/ssh/connection/session.rb:164:in `loop'",                                                                                                                                                                                         "gems/net-ssh-2.6.7/lib/net/ssh/connection/session.rb:164:in `loop_forever'",
"gems/net-ssh-2.6.7/lib/net/ssh/connection/session.rb:164:in `loop'",                                                                                                                                                                                         "gems/net-ssh-2.6.7/lib/net/ssh/connection/channel.rb:269:in `wait'",
"gems/net-scp-1.0.4/lib/net/scp.rb:279:in `upload!'",

该文件被复制到正确的远程位置,但net-ssh中的某些内容退出1而不是0,我没有费心去查找该调用在堆栈跟踪中的位置

# gems/net-scp-1.0.4/lib/net/scp:352
channel.on_close { |ch| raise Net::SCP::Error, "SCP did not finish successfully (#{ch[:exit]})" if ch[:exit] != 0 }

只需更改路径名对象,字符串就能使一切正常工作

my_file = Rails.root.join('config/my_file').to_s
scp.upload!(my_file,  "/var/tmp/dev.pub")

答案 3 :(得分:5)

为了完整起见,如果您没有适当的权限写入目的地,也会发生这种情况。

答案 4 :(得分:3)

我遇到了这个问题,并修复了它。 本地和远程路径的原因应该是'/ home / root / folder'这样的完整路径,而不应该是'〜/ folder'。

答案 5 :(得分:0)

我有一个rakefile.rb,用于定义使用net / scp和基本密码身份验证将文件上传到远程服务器的任务,即使我检查了一个“Net :: SSH :: AuthenticationFailed”异常主机,用户,密码和文件字符串。我启用了

LogLevel INFO

LogLevel DEBUG

然后在服务器的日志中看到以下输出(例如,/var/log/auth.log)并发现客户端由于某种原因尝试使用公钥认证!

Nov 13 22:38:26 linux sshd[12514]: debug1: Forked child 12532.
Nov 13 22:38:26 linux sshd[12532]: Set /proc/self/oom_score_adj to 0
Nov 13 22:38:26 linux sshd[12532]: debug1: rexec start in 5 out 5 newsock 5 pipe 7 sock 8
Nov 13 22:38:26 linux sshd[12532]: debug1: inetd sockets after dupping: 3, 3
Nov 13 22:38:26 linux sshd[12532]: Connection from 192.168.112.1 port 38391
Nov 13 22:38:26 linux sshd[12532]: debug1: Client protocol version 2.0; client software version Ruby/Net::SSH_2.6.1 i386-mingw32
Nov 13 22:38:26 linux sshd[12532]: debug1: no match: Ruby/Net::SSH_2.6.1 i386-mingw32
Nov 13 22:38:26 linux sshd[12532]: debug1: Enabling compatibility mode for protocol 2.0
Nov 13 22:38:26 linux sshd[12532]: debug1: Local version string SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1
Nov 13 22:38:26 linux sshd[12532]: debug1: permanently_set_uid: 102/65534 [preauth]
Nov 13 22:38:26 linux sshd[12532]: debug1: list_hostkey_types: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256 [preauth]
Nov 13 22:38:26 linux sshd[12532]: debug1: SSH2_MSG_KEXINIT sent [preauth]
Nov 13 22:38:26 linux sshd[12532]: debug1: SSH2_MSG_KEXINIT received [preauth]
Nov 13 22:38:26 linux sshd[12532]: debug1: kex: client->server aes128-cbc hmac-sha1 none [preauth]
Nov 13 22:38:26 linux sshd[12532]: debug1: kex: server->client aes128-cbc hmac-sha1 none [preauth]
Nov 13 22:38:26 linux sshd[12532]: debug1: SSH2_MSG_KEX_DH_GEX_REQUEST received [preauth]
Nov 13 22:38:26 linux sshd[12532]: debug1: SSH2_MSG_KEX_DH_GEX_GROUP sent [preauth]
Nov 13 22:38:26 linux sshd[12532]: debug1: expecting SSH2_MSG_KEX_DH_GEX_INIT [preauth]
Nov 13 22:38:27 linux sshd[12532]: debug1: SSH2_MSG_KEX_DH_GEX_REPLY sent [preauth]
Nov 13 22:38:27 linux sshd[12532]: debug1: SSH2_MSG_NEWKEYS sent [preauth]
Nov 13 22:38:27 linux sshd[12532]: debug1: expecting SSH2_MSG_NEWKEYS [preauth]
Nov 13 22:38:27 linux sshd[12532]: debug1: SSH2_MSG_NEWKEYS received [preauth]
Nov 13 22:38:27 linux sshd[12532]: debug1: KEX done [preauth]
Nov 13 22:38:27 linux sshd[12532]: debug1: userauth-request for user root service ssh-connection method publickey [preauth]
Nov 13 22:38:27 linux sshd[12532]: debug1: attempt 0 failures 0 [preauth]

    (pause in time)

Nov 13 22:38:38 linux sshd[12532]: debug1: test whether pkalg/pkblob are acceptable [preauth]
Nov 13 22:38:38 linux sshd[12532]: debug1: PAM: initializing for "root"
Nov 13 22:38:38 linux sshd[12532]: debug1: PAM: setting PAM_RHOST to "np-complete.local"
Nov 13 22:38:38 linux sshd[12532]: debug1: PAM: setting PAM_TTY to "ssh"
Nov 13 22:38:38 linux sshd[12532]: debug1: Checking blacklist file /usr/share/ssh/blacklist.RSA-2048
Nov 13 22:38:38 linux sshd[12532]: debug1: Checking blacklist file /etc/ssh/blacklist.RSA-2048
Nov 13 22:38:38 linux sshd[12532]: debug1: temporarily_use_uid: 0/0 (e=0/0)
Nov 13 22:38:38 linux sshd[12532]: debug1: trying public key file /root/.ssh/authorized_keys
Nov 13 22:38:38 linux sshd[12532]: debug1: Could not open authorized keys '/root/.ssh/authorized_keys': No such file or directory
Nov 13 22:38:38 linux sshd[12532]: debug1: restore_uid: 0/0
Nov 13 22:38:38 linux sshd[12532]: debug1: temporarily_use_uid: 0/0 (e=0/0)
Nov 13 22:38:38 linux sshd[12532]: debug1: trying public key file /root/.ssh/authorized_keys2
Nov 13 22:38:38 linux sshd[12532]: debug1: Could not open authorized keys '/root/.ssh/authorized_keys2': No such file or directory
Nov 13 22:38:38 linux sshd[12532]: debug1: restore_uid: 0/0
Nov 13 22:38:38 linux sshd[12532]: Failed publickey for root from 192.168.112.1 port 38391 ssh2
Nov 13 22:38:38 linux sshd[12532]: Connection closed by 192.168.112.1 [preauth]
Nov 13 22:38:38 linux sshd[12532]: debug1: do_cleanup [preauth]
Nov 13 22:38:38 linux sshd[12532]: debug1: monitor_read_log: child log fd closed
Nov 13 22:38:38 linux sshd[12532]: debug1: do_cleanup
Nov 13 22:38:38 linux sshd[12532]: debug1: PAM: cleanup

为了看看发生了什么,我改变了我的代码,在SCP的选项中包含一个:ssh密钥,指定我只支持密码验证:

Net::SCP.upload!(:remote_server, :remote_user, local_file, remote_file, 
                {   
                    :password => "(my password)",
                    :ssh => {:auth_methods => ["password"]}
                }
            )

这导致服务器上出现以下消息:

Nov 13 23:00:36 linux sshd[12514]: debug1: Forked child 13376.
Nov 13 23:00:36 linux sshd[13376]: Set /proc/self/oom_score_adj to 0
Nov 13 23:00:36 linux sshd[13376]: debug1: rexec start in 5 out 5 newsock 5 pipe 7 sock 8
Nov 13 23:00:36 linux sshd[13376]: debug1: inetd sockets after dupping: 3, 3
Nov 13 23:00:36 linux sshd[13376]: Connection from 192.168.112.1 port 38631
Nov 13 23:00:36 linux sshd[13376]: debug1: Client protocol version 2.0; client software version Ruby/Net::SSH_2.6.1 i386-mingw32
Nov 13 23:00:36 linux sshd[13376]: debug1: no match: Ruby/Net::SSH_2.6.1 i386-mingw32
Nov 13 23:00:36 linux sshd[13376]: debug1: Enabling compatibility mode for protocol 2.0
Nov 13 23:00:36 linux sshd[13376]: debug1: Local version string SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1
Nov 13 23:00:36 linux sshd[13376]: debug1: permanently_set_uid: 102/65534 [preauth]
Nov 13 23:00:36 linux sshd[13376]: debug1: list_hostkey_types: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256 [preauth]
Nov 13 23:00:36 linux sshd[13376]: debug1: SSH2_MSG_KEXINIT sent [preauth]
Nov 13 23:00:36 linux sshd[13376]: debug1: SSH2_MSG_KEXINIT received [preauth]
Nov 13 23:00:36 linux sshd[13376]: debug1: kex: client->server aes128-cbc hmac-sha1 none [preauth]
Nov 13 23:00:36 linux sshd[13376]: debug1: kex: server->client aes128-cbc hmac-sha1 none [preauth]
Nov 13 23:00:36 linux sshd[13376]: debug1: SSH2_MSG_KEX_DH_GEX_REQUEST received [preauth]
Nov 13 23:00:36 linux sshd[13376]: debug1: SSH2_MSG_KEX_DH_GEX_GROUP sent [preauth]
Nov 13 23:00:36 linux sshd[13376]: debug1: expecting SSH2_MSG_KEX_DH_GEX_INIT [preauth]
Nov 13 23:00:37 linux sshd[13376]: debug1: SSH2_MSG_KEX_DH_GEX_REPLY sent [preauth]
Nov 13 23:00:37 linux sshd[13376]: debug1: SSH2_MSG_NEWKEYS sent [preauth]
Nov 13 23:00:37 linux sshd[13376]: debug1: expecting SSH2_MSG_NEWKEYS [preauth]
Nov 13 23:00:37 linux sshd[13376]: debug1: SSH2_MSG_NEWKEYS received [preauth]
Nov 13 23:00:37 linux sshd[13376]: debug1: KEX done [preauth]
Nov 13 23:00:37 linux sshd[13376]: Connection closed by 192.168.112.1 [preauth]
Nov 13 23:00:37 linux sshd[13376]: debug1: do_cleanup [preauth]
Nov 13 23:00:37 linux sshd[13376]: debug1: monitor_read_log: child log fd closed
Nov 13 23:00:37 linux sshd[13376]: debug1: do_cleanup

显然他们无法就如何进行身份验证达成一致......希望这对遇到它的人有帮助。如果/当我发现为什么会发生这种情况时,我会更新。

答案 6 :(得分:0)

好的,所以找到另一种方法来实现这一目标。通过对文件进行更改,您上传的文件会创建一些精心设计的意大利面条代码。很简单的方法来判断是否是这种情况,您可以查看上传的总比特数与预期上传的比例数。

scp = Net::SSH.start(host, username, ssh_settings).scp
scp.upload!(local_path, remote_path) do |_ch, name, sent, total|
  percentage = format('%.2f', sent.to_f / total.to_f * 100) + '%'
  puts "#{name} #{percentage}"
end

如果您看到百分比不停止在100.00%,那可能是您看到Net::SCP::Error Exception: SCP did not finish successfully (1)的原因。但是,就像上面所说的那样,它也可能与此完全无关。

答案 7 :(得分:0)

就我而言,我还需要按以下步骤在服务器端安装openssh-clients软件包(在CentOS7中):

# yum install openssh-clients