Reputation: 1160
I have a script which will do the file transfer from one server to another but it gives an error:
Net::SCP::Error (SCP did not finish successfully ()):
Can any body help me? Here is my code.
Net::SCP.start( 's.com', 'username', :password => 'password' ) do|scp|
scp.upload!( source, destination )
end
Upvotes: 16
Views: 12080
Reputation: 15
The issue with me was that the target file was size zero. Swapping out that test file for one with data in it resolved my issue without changing any other code.
Upvotes: 0
Reputation: 1209
In my case, I needed to install openssh-clients package into server-side also as follows (in CentOS7):
# yum install openssh-clients
Upvotes: 0
Reputation: 3480
OK, so found another way to make this happen. By making changes to the file that your uploading threw some well-created crafted spaghetti code. The easy way to tell if this is the case you can take a look at the total bits uploaded vs what it expected it to upload.
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
If you see the percentage no stop at 100.00% that might be the reason you're seeing the Net::SCP::Error Exception: SCP did not finish successfully (1)
. However, like it has been said above it could be something totally unrelated to this as well.
Upvotes: 0
Reputation: 117615
Just for completeness' sake, this can also happen if you don't have proper permissions to write to the destination.
Upvotes: 6
Reputation: 13795
I had a slightly different error that included the exit code in parens:
Net::SCP::Error Exception: SCP did not finish successfully (1)
I figured at first this would have been caused by the source file not existing or the destination dir not existing as others have mentioned, but it turned out to be because I was passing a pathname object for the source file instead of a string.
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!'",
The file was being copied to the correct remote location, but something in net-ssh was exiting 1 instead of 0, I haven't bothered to go find where that call is in the stack trace
# 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 }
Just changing the pathname object a string made everything work
my_file = Rails.root.join('config/my_file').to_s
scp.upload!(my_file, "/var/tmp/dev.pub")
Upvotes: 10
Reputation: 11587
I have a rakefile.rb that defines a task for uploading a file to a remote server using net/scp and basic password authentication, and I was getting a "Net::SSH::AuthenticationFailed" exception even though I checked the host, user, password, and file strings. I enabled
LogLevel INFO
to
LogLevel DEBUG
then saw the following output in the server's log (e.g.,/var/log/auth.log
) and discovered that the client was trying to use public key authentication for some reason!
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
So to see what was going on I changed my code to include an :ssh key in the options for SCP to specify that I only support password authentication:
Net::SCP.upload!(:remote_server, :remote_user, local_file, remote_file,
{
:password => "(my password)",
:ssh => {:auth_methods => ["password"]}
}
)
and this resulted in the following messages on the server:
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
Apparently they couldn't agree on how to do authentication.... Hopefully this is helpful for people coming across it. If/When I find out why this is happening I will update this.
Upvotes: 0
Reputation: 1857
I have this issue and just fixed it. The reason that both local and remote path, should be full path like '/home/root/folder', and should not be '~/folder'.
Upvotes: 5
Reputation: 3316
This error also happens if you are uploading a file to a folder that does not yet exist on the remote server. Folder creation is not implicit in SCP
Upvotes: 10
Reputation: 196
I had this issue today. Turns out my local file (in your example, source) pointed at a non-existent file. Good luck.
Upvotes: 12