Scp

SCP 沒有錯誤地失敗

  • August 20, 2021

一段時間以來,我一直在體驗 SCP 的非常奇怪的行為:每當我嘗試複製文件時,SCP 的輸出包含一堆下劃線並且文件沒有被複製。

$ scp test.txt 192.168.0.2:~
job@192.168.0.2'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
job@192.168.0.2'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 no-more-sessions@openssh.com
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)

好的,大聲笑,我剛剛弄清楚問題所在。

因為我非常喜歡奶牛,所以我把它放在fortune | cowsay我的文件頂部,.bashrc它在啟動時會產生如下輸出bash

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

bash當互動式執行時,這一切都很好(有時也很有趣) 。~/.bashrc但是,當 bash是互動式的而不是登錄 shell 時,*或者當它是登錄 shell 並且其父程序是rshdorsshd*時,bash 會讀取。當您執行scp時,伺服器會啟動一個 shell,該 shell 會啟動一個遠端scp實例。的輸出令人.bashrc困惑scp,因為它的發送方式與scp協議數據的發送方式相同。這顯然是一個已知的錯誤,請參閱此處了解更多詳細資訊。

另請注意,我在問題中提到的下劃線是文本氣球頂行中的下劃線。

.bashrc所以解決方案很簡單:我將以下內容放在遠端(目標)機器的頂部:

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

該行預設存在,.bashrc但由於我的許多(顯然是粗心的)編輯而被擱置。

引用自:https://unix.stackexchange.com/questions/18231