简体   繁体   中英

Connecting to SFTP through domain instead of IP fails

I'm using Renci SSH.NET to upload files to a server. I can do it easily with SFTP server IP, but when I try to set my server domain I recieve an exception

No connection could be made because the target machine actively refused it.

I am able to use both the IP and the full computer name with FTP and FTPS, but with SFTP I can only do it with IP. Is it possible to connect to SFTP through the full computer name? Here is my code:

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;
    }
}

My log when connecting with 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

You have obfuscated the most important part of WinSCP log.

Though anyway, you can see that there's something wrong:

 . 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

The hostname/DNS name resolves to (at least) two IP addresses (IPv6 and IPv4?). The first does not work. SSH.NET probably tries only the first address, while WinSCP falls back to the second.

Either the DNS is wrong. Or more likely, the server listens on second IP address only.

Nothing wrong with SSH.NET or your code. It's DNS/server-side problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM