【问题标题】:Connecting to SFTP through domain instead of IP fails通过域而不是 IP 连接到 SFTP 失败
【发布时间】:2020-02-05 16:29:12
【问题描述】:

我正在使用 Renci SSH.NET 将文件上传到服务器。我可以使用 SFTP 服务器 IP 轻松完成此操作,但是当我尝试设置我的服务器域时,我收到一个异常

由于目标机器主动拒绝,无法建立连接。

我可以在 FTP 和 FTPS 中同时使用 IP 和完整的计算机名称,但使用 SFTP 我只能使用 IP。是否可以通过完整的计算机名称连接到 SFTP?这是我的代码:

public class SftpSender : SenderBase
{
    public ConnectionInfo ConnectionInfo { get; }
    public SftpSender(DestinationInfo destination)
    {
        var authenticationMethod = new PasswordAuthenticationMethod(destination.UserName, destination.Password);
        this.ConnectionInfo = new ConnectionInfo(destination.HostIp, destination.Port, destination.UserName, authenticationMethod);
    }

    public override bool Send(string filePath)
    {
        bool isFileUploaded = false;

        try
        {
            using (var client = new SftpClient(ConnectionInfo))
            {
                client.Connect();
                string uploadedFileName = Path.GetFileName(filePath);

                using (FileStream fileStream = File.OpenRead(filePath))
                {                        
                    client.UploadFile(fileStream, $"{uploadedFileName}", true);
                    isFileUploaded = true;
                    client.Disconnect();
                    isFileUploaded = true;
                }
            }
        }
        catch (Exception exception)
        {
            //...
            isFileUploaded = false;
        }

        return isFileUploaded;
    }
}

我使用 WinScp 连接时的日志:

. 2019-10-09 09:48:19.899 --------------------------------------------------------------------------
. 2019-10-09 09:48:19.900 WinSCP Version 5.15.1 (Build 9407) (OS 10.0.18362 - Windows 10 Enterprise)
. 2019-10-09 09:48:19.900 Configuration: HKCU\Software\Martin Prikryl\WinSCP 2\
. 2019-10-09 09:48:19.900 Log level: Normal
. 2019-10-09 09:48:19.900 Local account: myCompany\myName
. 2019-10-09 09:48:19.900 Working directory: C:\Program Files (x86)\WinSCP
. 2019-10-09 09:48:19.900 Process ID: 8868
. 2019-10-09 09:48:19.900 Command-line: "C:\Program Files (x86)\WinSCP\WinSCP.exe" 
. 2019-10-09 09:48:19.900 Time zone: Current: GMT+2, Standard: GMT+1 (Central European Standard Time), DST: GMT+2 (Central European Daylight Time), DST Start: 31.03.2019, DST End: 27.10.2019
. 2019-10-09 09:48:19.901 Login time: środa, 9 październik 2019 09:48:19
. 2019-10-09 09:48:19.901 --------------------------------------------------------------------------
. 2019-10-09 09:48:19.901 Session name: user@test_site.pl (Site)
. 2019-10-09 09:48:19.901 Host name: name-ARD.myCompany.com.pl (Port: 2222)
. 2019-10-09 09:48:19.901 User name: user (Password: No, Key file: No, Passphrase: No)
. 2019-10-09 09:48:19.901 Tunnel: No
. 2019-10-09 09:48:19.901 Transfer Protocol: SFTP (SCP)
. 2019-10-09 09:48:19.901 Ping type: Off, Ping interval: 30 sec; Timeout: 15 sec
. 2019-10-09 09:48:19.901 Disable Nagle: No
. 2019-10-09 09:48:19.901 Proxy: None
. 2019-10-09 09:48:19.901 Send buffer: 262144
. 2019-10-09 09:48:19.901 SSH protocol version: 2; Compression: No
. 2019-10-09 09:48:19.901 Bypass authentication: No
. 2019-10-09 09:48:19.901 Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: Yes
. 2019-10-09 09:48:19.901 GSSAPI: Forwarding: No; Libs: gssapi32,sspi,custom; Custom: 
. 2019-10-09 09:48:19.901 Ciphers: aes,chacha20,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
. 2019-10-09 09:48:19.901 KEX: ecdh,dh-gex-sha1,dh-group14-sha1,rsa,WARN,dh-group1-sha1
. 2019-10-09 09:48:19.901 SSH Bugs: Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto
. 2019-10-09 09:48:19.901 Simple channel: Yes
. 2019-10-09 09:48:19.901 Return code variable: Autodetect; Lookup user groups: Auto
. 2019-10-09 09:48:19.901 Shell: default
. 2019-10-09 09:48:19.901 EOL: LF, UTF: Auto
. 2019-10-09 09:48:19.901 Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes; Follow directory symlinks: No
. 2019-10-09 09:48:19.901 LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No; Exit code 1 is error: No
. 2019-10-09 09:48:19.901 SFTP Bugs: Auto,Auto
. 2019-10-09 09:48:19.901 SFTP Server: default
. 2019-10-09 09:48:19.901 Local directory: C:\Users\myName\Documents, Remote directory: /, Update: Yes, Cache: Yes
. 2019-10-09 09:48:19.901 Cache directory changes: Yes, Permanent: Yes
. 2019-10-09 09:48:19.901 Recycle bin: Delete to: No, Overwritten to: No, Bin path: 
. 2019-10-09 09:48:19.901 DST mode: Unix
. 2019-10-09 09:48:19.901 --------------------------------------------------------------------------
. 2019-10-09 09:48:20.013 Looking up host "name-ARD.myCompany.com.pl" for SSH connection
. 2019-10-09 09:48:20.013 Connecting to xxxxxxxxxxxxxxxxx port 2222
. 2019-10-09 09:48:22.018 Failed to connect to xxxxxxxxxxxxxxxxx: Network error: Connection refused
. 2019-10-09 09:48:22.018 Connecting to 123.123.123.123 port 2222 // ip changed by me
. 2019-10-09 09:48:22.019 We claim version: SSH-2.0-WinSCP_release_5.15.1
. 2019-10-09 09:48:22.074 Server version: SSH-2.0-Syncplify_me_MicroServer
. 2019-10-09 09:48:22.074 Using SSH protocol version 2
. 2019-10-09 09:48:22.075 Have a known host key of type rsa2
. 2019-10-09 09:48:22.076 Doing Diffie-Hellman group exchange
. 2019-10-09 09:48:22.273 Doing Diffie-Hellman key exchange with hash SHA-256
. 2019-10-09 09:48:23.673 Host key fingerprint is:
. 2019-10-09 09:48:23.673 ssh-rsa 1024 e7:88:7e:97:99:25:b2:19:b2:01:ce:0b:25:c9:34:91 mbWatI+p8H8sAmYlczhVYXFo7wMYlJoD4mlMI4z9oFA=
. 2019-10-09 09:48:23.695 Host key matches cached key
. 2019-10-09 09:48:23.695 Initialised AES-256 SDCTR client->server encryption
. 2019-10-09 09:48:23.695 Initialised HMAC-SHA-256 client->server MAC algorithm
. 2019-10-09 09:48:23.696 Initialised AES-256 SDCTR server->client encryption
. 2019-10-09 09:48:23.696 Initialised HMAC-SHA-256 server->client MAC algorithm
! 2019-10-09 09:48:23.696 Using username "user".
. 2019-10-09 09:48:23.708 Server offered these authentication methods: password
. 2019-10-09 09:48:23.708 Prompt (password, "SSH password", <no instructions>, "&Password: ")
. 2019-10-09 09:48:28.473 Sent password
. 2019-10-09 09:48:31.705 Access granted
. 2019-10-09 09:48:31.705 Opening session as main channel
. 2019-10-09 09:48:31.705 Opened main channel
. 2019-10-09 09:48:31.729 Started a shell/command
. 2019-10-09 09:48:31.743 --------------------------------------------------------------------------
. 2019-10-09 09:48:31.743 Using SFTP protocol.
. 2019-10-09 09:48:31.743 Doing startup conversation with host.
> 2019-10-09 09:48:31.763 Type: SSH_FXP_INIT, Size: 5, Number: -1
< 2019-10-09 09:48:31.764 Type: SSH_FXP_VERSION, Size: 344, Number: -1
. 2019-10-09 09:48:31.764 SFTP version 6 negotiated.
. 2019-10-09 09:48:31.764 Server requests EOL sequence "\r\n".
. 2019-10-09 09:48:31.764 Server support information (supported2):
. 2019-10-09 09:48:31.764   Attribute mask: 8000FFFF, Attribute bits: FFF, Open flags: FF9
. 2019-10-09 09:48:31.764   Access mask: 1F01FF, Open block vector: FFFF, Block vector: FFFF, Max read size: 0
. 2019-10-09 09:48:31.764   Attribute extensions (0)
. 2019-10-09 09:48:31.765   Extensions (14)
. 2019-10-09 09:48:31.765     newline
. 2019-10-09 09:48:31.765     newline@vandyke.com
. 2019-10-09 09:48:31.765     version-select
. 2019-10-09 09:48:31.765     filename-translation-control
. 2019-10-09 09:48:31.765     text-seek
. 2019-10-09 09:48:31.765     vendor-id
. 2019-10-09 09:48:31.765     check-file
. 2019-10-09 09:48:31.765     space-available
. 2019-10-09 09:48:31.765     home-directory
. 2019-10-09 09:48:31.765     copy-file
. 2019-10-09 09:48:31.765     copy-data
. 2019-10-09 09:48:31.765     get-temp-folder
. 2019-10-09 09:48:31.766     make-temp-folder
. 2019-10-09 09:48:31.766     statvfs@openssh.com
. 2019-10-09 09:48:31.766 SFTP versions supported by the server: 3,4,5,6
> 2019-10-09 09:48:31.766 Type: SSH_FXP_EXTENDED, Size: 64, Number: 200
< 2019-10-09 09:48:31.784 Type: SSH_FXP_STATUS, Size: 21, Number: 200
. 2019-10-09 09:48:31.784 We will use UTF-8 strings as it is mandatory with SFTP version 4 and newer
. 2019-10-09 09:48:31.785 Changing directory to "/".
. 2019-10-09 09:48:31.785 Getting real path for '/'
> 2019-10-09 09:48:31.785 Type: SSH_FXP_REALPATH, Size: 11, Number: 272
< 2019-10-09 09:48:31.851 Type: SSH_FXP_NAME, Size: 19, Number: 272
. 2019-10-09 09:48:31.852 Real path is '/'
. 2019-10-09 09:48:31.852 Trying to open directory "/".
> 2019-10-09 09:48:31.852 Type: SSH_FXP_LSTAT, Size: 14, Number: 519
< 2019-10-09 09:48:31.889 Type: SSH_FXP_ATTRS, Size: 10, Number: 519
. 2019-10-09 09:48:31.890 Getting current directory name.
. 2019-10-09 09:48:31.977 Listing directory "/".
> 2019-10-09 09:48:31.977 Type: SSH_FXP_OPENDIR, Size: 10, Number: 779
< 2019-10-09 09:48:31.998 Type: SSH_FXP_HANDLE, Size: 13, Number: 779
> 2019-10-09 09:48:31.998 Type: SSH_FXP_READDIR, Size: 13, Number: 1036
< 2019-10-09 09:48:31.999 Type: SSH_FXP_NAME, Size: 462, Number: 1036
> 2019-10-09 09:48:31.999 Type: SSH_FXP_READDIR, Size: 13, Number: 1292
> 2019-10-09 09:48:32.000 Type: SSH_FXP_CLOSE, Size: 13, Number: 1540
. 2019-10-09 09:48:32.000 New Text Document (2).txt;-;0;2019-10-09T06:49:36.000Z;3;"user" [0];"group" [0];rw-rw-rw-;1
. 2019-10-09 09:48:32.000 New Text Document.txt;-;0;2019-10-09T06:49:16.000Z;3;"user" [0];"group" [0];rw-rw-rw-;1
. 2019-10-09 09:48:32.000 test (3).txt;-;0;2019-10-08T10:38:47.000Z;3;"user" [0];"group" [0];rw-rw-rw-;1
. 2019-10-09 09:48:32.000 ..;D;0;2019-10-09T06:49:36.000Z;3;"user" [0];"group" [0];rwx------;0
. 2019-10-09 09:48:32.021 Startup conversation with host finished.
. 2019-10-09 09:52:09.275 Closing connection.
. 2019-10-09 09:52:09.275 Sending special code: 12
. 2019-10-09 09:52:09.275 Sent EOF message

【问题讨论】:

  • 听起来 DNS 的解析方式与您的预期不同。如果它在另一台机器上,您可能连接到不同的 DNS 服务器。如果它在您自己的机器上,那么这确实很奇怪。无论如何,请确保“nslookup”返回您期望的域 IP。
  • @MartinPrikryl 是的,我可以毫无问题地使用 WinScp 登录。
  • @faester 到目前为止它是我自己的机器。
  • 问题。只是您的代码没有按预期工作,还是任何 SFTP 客户端?对我来说,上面的代码会根据是否提供主机名或仅提供 IP 以不同的方式工作,这似乎很不寻常。你能包括你用来测试的实际代码(两种情况)吗?
  • 那么您为什么要问“是否可以通过完整的计算机名连接到 SFTP?”,如果您知道可以吗? + 向我们展示 WinSCP 的日志文件。 + 你能连接到例如shell.sourceforge.net? + 没有需要四个字符串的 ConnectionInfo 构造函数重载。

标签: c# .net ssh.net


【解决方案1】:

您混淆了 WinSCP 日志中最重要的部分。

尽管如此,你可以看出有问题:

. 2019-10-09 09:48:20.013 Looking up host "name-ARD.myCompany.com.pl" for SSH connection  
. 2019-10-09 09:48:20.013 Connecting to xxxxxxxxxxxxxxxxx port 2222  
. 2019-10-09 09:48:22.018 Failed to connect to xxxxxxxxxxxxxxxxx: Network error: Connection refused  
. 2019-10-09 09:48:22.018 Connecting to 123.123.123.123 port 2222 // ip changed by me

主机名/DNS 名称解析为(至少)两个 IP 地址(IPv6 和 IPv4?)。第一个不起作用。 SSH.NET 可能只尝试第一个地址,而 WinSCP 回退到第二个。

DNS 错误。或者更可能的是,服务器只侦听第二个 IP 地址。

SSH.NET 或您的代码没有问题。这是 DNS/服务器端的问题。

【讨论】:

  • 非常感谢您的帮助。很高兴知道这不是我的代码问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-28
  • 2019-09-23
  • 1970-01-01
  • 2017-11-02
  • 1970-01-01
  • 2016-08-08
相关资源
最近更新 更多