SCP 失败且没有错误

SCP 失败且没有错误

一段时间以来,我一直在经历 SCP 非常奇怪的行为:每当我尝试复制文件时,SCP 的输出都会包含一堆下划线,并且文件不会被复制。

$ scp test.txt 192.168.0.2:~
[email protected]'s password: 
 ________________________________________

当我使用 Midnight Commander 创建 SSH 连接并复制文件时,它确实有效。

有关我的机器的一些信息:

$ ssh -V
OpenSSH_5.8p1 Debian-1ubuntu3, OpenSSL 0.9.8o 01 Jun 2010

$ uname -a
Linux squatpc 2.6.38-10-generic #46-Ubuntu SMP Tue Jun 28 15:05:41 UTC 2011 i686 i686 i386 GNU/Linux

我正在运行 Kubuntu 11.04。

编辑:根据评论要求提供更多信息:

$ scp -v test.txt 192.168.0.2:~
Executing: program /usr/bin/ssh host 192.168.0.2, user (unspecified), command scp -v -t -- ~
OpenSSH_5.8p1 Debian-1ubuntu3, OpenSSL 0.9.8o 01 Jun 2010
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 192.168.0.2 [192.168.0.2] port 22.
debug1: Connection established.
debug1: identity file /home/job/.ssh/id_rsa type 1
debug1: Checking blacklist file /usr/share/ssh/blacklist.RSA-2048
debug1: Checking blacklist file /etc/ssh/blacklist.RSA-2048
debug1: identity file /home/job/.ssh/id_rsa-cert type -1
debug1: identity file /home/job/.ssh/id_dsa type -1
debug1: identity file /home/job/.ssh/id_dsa-cert type -1
debug1: identity file /home/job/.ssh/id_ecdsa type -1
debug1: identity file /home/job/.ssh/id_ecdsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.8p1 Debian-1ubuntu3
debug1: match: OpenSSH_5.8p1 Debian-1ubuntu3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.8p1 Debian-1ubuntu3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA 28:f3:2b:31:36:43:9b:07:d8:33:ca:43:4f:ca:6c:4c
debug1: Host '192.168.0.2' is known and matches the ECDSA host key.
debug1: Found key in /home/job/.ssh/known_hosts:20
debug1: ssh_ecdsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/job/.ssh/id_rsa
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/job/.ssh/id_dsa
debug1: Trying private key: /home/job/.ssh/id_ecdsa
debug1: Next authentication method: password
[email protected]'s password: 
debug1: Authentication succeeded (password).
Authenticated to 192.168.0.2 ([192.168.0.2]:22).
debug1: channel 0: new [client-session]
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: scp -v -t -- ~
 ________________________________________
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 2120, received 1872 bytes, in 0.3 seconds
Bytes per second: sent 7783.1, received 6872.6
debug1: Exit status 0

$ type scp
scp is hashed (/usr/bin/scp)

答案1

好吧,哈哈,我刚刚弄清楚问题是什么。

由于我非常喜欢奶牛,因此我将其放在文件fortune | cowsay的顶部.bashrc,该文件在启动时会产生如下所示的输出bash

 _______________________________________
< You will lose an important disk file. >
 ---------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

bash当交互运行时,这一切都很好(有时甚至很有趣) 。然而,~/.bashrc当 bash 是交互式的而不是登录 shell 时,它会读取,或者当它是登录 shell 并且其父进程是rshdsshd。当您运行时scp,服务器会启动一个 shell,该 shell 会启动一个远程scp实例。的输出.bashrc令人困惑scp,因为它的发送方式与scp协议数据的发送方式相同。这显然是一个已知的错误,请参阅这里更多细节。

另请注意,我在问题中提到的下划线是文本气球顶行的下划线。

.bashrc所以解决方案很简单:我将以下内容放在远程(目标)计算机的顶部:

# If not running interactively, don't do anything
[[ $- == *i* ]] || return

该行存在于默认值中.bashrc,但由于我的多次(显然是粗心的)编辑而被搁置。

答案2

AFAIK,启用无阻碍的正确方法scp不是关于~/.bashrc脚本中标准输出的条件,而是更多关于简单地将屏幕输出限制到脚本~/.bash_profile。至少对于我的发行版(CentOS)来说是这样的。

为了清楚起见编辑:

  1. 仅将“所有”远程连接所需的行放入 ~/.bashrc 文件中(即设置某些 ENV 变量是可以的,但回显人类可读的文本则不行。)
  2. 青年MMV

答案3

当您的.bashrc文件产生一些输出或您有时,可能会发生此行为MOTD(Message Of The Day)。您可以将该-o选项与scp

-o可以将选项传递给 SSH,在这种情况下,您可以提供类似选项RequestTTY=no,并且它从不请求终端输出。

scp -o RequestTTY=否来源 目标

答案4

我不确定 scp 是否有此错误,那是很久以前我使用它时的事了。我用 rsync 替换了 scp,效果好多了。 Rsync 应该存在于几乎所有的 Linux 设备上。

我在这里写了关于 rsync 的文章,以及关于 scp 的文章: 如何通过 scp 最好地复制大量小文件?

JohnU 的帖子描述了 Rsync。

相关内容