服务器返回一个地址以响应PASV命令,该命令与建立FTP连接的地址不同

时间:2022-03-24 03:46:07

System.Net.WebException: The server returned an address in response to the PASV command that is different than the address to which the FTP connection was made.
at System.Net.FtpWebRequest.CheckError()
at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
at System.Net.CommandStream.Abort(Exception e)
at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
at System.Net.FtpWebRequest.GetRequestStream()
at BackupDB.Program.FTPUploadFile(String serverPath, String serverFile, FileInfo LocalFile, NetworkCredential Cred) in D:\PROJEKTI\BackupDB\BackupDB\Program.cs:line 119

System.Net。WebException:服务器响应PASV命令返回一个地址,这与FTP连接的地址不同。在System.Net.FtpWebRequest.CheckError System.Net.FtpWebRequest()。在System.Net.CommandStream SyncRequestCallback(对象obj)。在System.Net.FtpWebRequest中止(异常e)。在BackupDB.Program的System.Net.FtpWebRequest.GetRequestStream()中完成请求阶段(RequestStage)。FTPUploadFile(String serverPath, String serverFile, FileInfo LocalFile, NetworkCredential Cred) in D:\PROJEKTI\BackupDB\BackupDB\程序。cs:119行

code:

代码:

FTPMakeDir(new Uri(serverPath + "/"), Cred);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath+serverFile);
request.UsePassive = true;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = Cred;
byte[] buffer = new byte[10240];    // Read/write 10kb

using (FileStream sourceStream = new FileStream(
    LocalFile.ToString(), FileMode.Open))
{
    using (Stream requestStream = request.GetRequestStream())
    {
        int bytesRead;
        do
        {
            bytesRead = sourceStream.Read(buffer, 0, buffer.Length);
            requestStream.Write(buffer, 0, bytesRead);
        } while (bytesRead > 0);
    }
    response = (FtpWebResponse)request.GetResponse();
    response.Close();
}

5 个解决方案

#1


7  

omg. What's up with all the pandering here for buying their 3rd party solutions instead of informing you to change one line of code?

omg。在这里,你为什么要购买他们的第三方解决方案而不是通知你改变一行代码?

Try toggling the Passive value to see which works:

尝试切换被动值,看看哪一个是有效的:

    request.UsePassive = false;

This may depend on the firewall between the Machines (client and server).

这可能取决于机器(客户端和服务器)之间的防火墙。

I've noticed if I go through our firewall, then I need it left at True, otherwise it will return the Exception:

我注意到如果我通过防火墙,那么我需要它保持为True,否则它将返回异常:

The remote server returned an error: (500) Syntax error, command unrecognized.

远程服务器返回一个错误:(500)语法错误,命令无法识别。

However, if I'm behind the firewall (like two machines connecting directly to each other within a data-center) then I need to set it to False, otherwise it will return the Exception:

但是,如果我在防火墙后面(就像两个机器在数据中心内直接连接),那么我需要将它设置为False,否则它将返回异常:

The server returned an address in response to the PASV command that is different than the address to which the FTP connection was made.

服务器返回一个地址以响应PASV命令,该命令与建立FTP连接的地址不同。

If this works and you want to make your solution more adaptable, you could wrap your request in a try-catch block using the default True value, and if you get the 500 error, then switch UsePassive to False and try it again.

如果这可行,并且您希望使您的解决方案更具有适应性,您可以使用默认的True值将您的请求封装在try-catch块中,如果您得到500个错误,那么将UsePassive转换为False,然后再次尝试。

#2


3  

if anyone have the same problem, this is solution for proftpd

如果有人有同样的问题,这就是proftpd的解决方案

http://www.proftpd.org/docs/howto/NAT.html

http://www.proftpd.org/docs/howto/NAT.html

#3


3  

In passive mode FTP conversation goes as follows:

在被动模式下,FTP对话如下:

client: PASV
(i would like to transfer files. Tell me which port and ip address should I use)

server: 227 Entering Passive Mode (172,16,3,4,204,173)
(ok, use port 52397 on IP address 172.16.3.4.)

client: connects to this IP address/port and starts data transfer.

It looks like the FTP server with two public IP address (e.g. 1.2.3.4) returns a private IP address as a response to PASV command.

看起来具有两个公共IP地址(例如1.2.3.4)的FTP服务器返回一个私有IP地址作为对PASV命令的响应。

Solution

解决方案

Switching to Active mode.

切换到主动模式。

In active mode FTP server connects to FTP client for data transfers. It would solve this issue, but is not firewall friendly. It will not work when incoming connections are blocked (very common).

在活动模式下,FTP服务器连接到FTP客户端进行数据传输。它可以解决这个问题,但是不支持防火墙。当传入连接被阻塞(非常常见)时,它将无法工作。

Ignoring IP address send as response to PASV command

忽略IP地址发送作为对PASV命令的响应

If the public ftp server IP address is a public one, and IP address returned as a response for PASV command is from private range (such as 10., 192.168.). In such case the FTP client should use the public IP address.

如果公共ftp服务器IP地址是公共的,并且作为PASV命令的响应返回的IP地址来自私有范围(如10)。,192.168)。在这种情况下,FTP客户端应该使用公共IP地址。

This is exactly what does our Rebex FTP do in such situation. It works well (this behavior can be switched off). It can be even turned on for servers with multiple public IP addresses.

这正是我们的Rebex FTP在这种情况下所做的。它运行良好(这种行为可以关闭)。它甚至可以为拥有多个公共IP地址的服务器打开。

I don't know whether similar workaround is possible with FtpWebRequest.

我不知道FtpWebRequest是否也有类似的解决方案。

You can download trial and check whether it solves your problem.

你可以下载试用版,看看它是否能解决你的问题。

#4


2  

After a lot of digging around I found the only way to solve this problem was to change the PASV settings on the server.

在进行了大量的挖掘之后,我发现解决这个问题的唯一方法是改变服务器上的PASV设置。

Fortunately I control both the client and server machines so I was able to tell the server (FileZilla in my case) to use the public IP rather than the private IP.

幸运的是,我同时控制了客户机和服务器机器,因此我能够告诉服务器(在我的例子中是FileZilla)使用公共IP而不是私有IP。

#5


1  

Your FTP server is misconfigured.

FTP服务器配置错误。

In the passive mode the server reports an IP address and port to which the client should connect to for a data transfer. Your FTP server reports its IP address within an internal network, although it's behind a firewall/NAT. The client cannot connect to that internal address for obvious reason. You have to configure the FTP server to report its external IP address.

在被动模式下,服务器报告一个IP地址和端口,客户端应该连接到该地址和端口进行数据传输。您的FTP服务器在一个内部网络中报告它的IP地址,尽管它在防火墙/NAT之后。显然,客户端无法连接到该内部地址。您必须配置FTP服务器来报告其外部IP地址。

How that's done is server-specific and you didn't tell us, what your FTP server is.

这是特定于服务器的,你没有告诉我们,你的FTP服务器是什么。


Some answer here suggest using the active mode.

这里的一些回答建议使用主动模式。

request.UsePassive = false;

But that can help only if there's no firewall/NAT between the client and the server, in which case you won't have the problem in the first place (unless the server is really broken and reports a completely wrong IP address, not only the internal one). Or if the firewall/NAT is configured to allow inbound connections, what is not usual.

但只有在客户端和服务器之间没有防火墙/NAT时,这才会有所帮助,在这种情况下,您首先不会遇到问题(除非服务器真的被破坏,报告一个完全错误的IP地址,而不仅仅是内部的IP地址)。或者,如果防火墙/NAT被配置为允许入站连接,这是不常见的。


Another approach is using a different FTP library that can workaround the problem by ignoring the incorrect IP address reported by the server and using the primary/control connection IP address. Or by using the EPSV command instead of PASV command that implicitly uses the primary/control connection IP address.

另一种方法是使用不同的FTP库,通过忽略服务器报告的不正确的IP地址并使用主/控制连接IP地址来解决这个问题。或者使用EPSV命令,而不是使用隐式使用主/控制连接IP地址的PASV命令。

#1


7  

omg. What's up with all the pandering here for buying their 3rd party solutions instead of informing you to change one line of code?

omg。在这里,你为什么要购买他们的第三方解决方案而不是通知你改变一行代码?

Try toggling the Passive value to see which works:

尝试切换被动值,看看哪一个是有效的:

    request.UsePassive = false;

This may depend on the firewall between the Machines (client and server).

这可能取决于机器(客户端和服务器)之间的防火墙。

I've noticed if I go through our firewall, then I need it left at True, otherwise it will return the Exception:

我注意到如果我通过防火墙,那么我需要它保持为True,否则它将返回异常:

The remote server returned an error: (500) Syntax error, command unrecognized.

远程服务器返回一个错误:(500)语法错误,命令无法识别。

However, if I'm behind the firewall (like two machines connecting directly to each other within a data-center) then I need to set it to False, otherwise it will return the Exception:

但是,如果我在防火墙后面(就像两个机器在数据中心内直接连接),那么我需要将它设置为False,否则它将返回异常:

The server returned an address in response to the PASV command that is different than the address to which the FTP connection was made.

服务器返回一个地址以响应PASV命令,该命令与建立FTP连接的地址不同。

If this works and you want to make your solution more adaptable, you could wrap your request in a try-catch block using the default True value, and if you get the 500 error, then switch UsePassive to False and try it again.

如果这可行,并且您希望使您的解决方案更具有适应性,您可以使用默认的True值将您的请求封装在try-catch块中,如果您得到500个错误,那么将UsePassive转换为False,然后再次尝试。

#2


3  

if anyone have the same problem, this is solution for proftpd

如果有人有同样的问题,这就是proftpd的解决方案

http://www.proftpd.org/docs/howto/NAT.html

http://www.proftpd.org/docs/howto/NAT.html

#3


3  

In passive mode FTP conversation goes as follows:

在被动模式下,FTP对话如下:

client: PASV
(i would like to transfer files. Tell me which port and ip address should I use)

server: 227 Entering Passive Mode (172,16,3,4,204,173)
(ok, use port 52397 on IP address 172.16.3.4.)

client: connects to this IP address/port and starts data transfer.

It looks like the FTP server with two public IP address (e.g. 1.2.3.4) returns a private IP address as a response to PASV command.

看起来具有两个公共IP地址(例如1.2.3.4)的FTP服务器返回一个私有IP地址作为对PASV命令的响应。

Solution

解决方案

Switching to Active mode.

切换到主动模式。

In active mode FTP server connects to FTP client for data transfers. It would solve this issue, but is not firewall friendly. It will not work when incoming connections are blocked (very common).

在活动模式下,FTP服务器连接到FTP客户端进行数据传输。它可以解决这个问题,但是不支持防火墙。当传入连接被阻塞(非常常见)时,它将无法工作。

Ignoring IP address send as response to PASV command

忽略IP地址发送作为对PASV命令的响应

If the public ftp server IP address is a public one, and IP address returned as a response for PASV command is from private range (such as 10., 192.168.). In such case the FTP client should use the public IP address.

如果公共ftp服务器IP地址是公共的,并且作为PASV命令的响应返回的IP地址来自私有范围(如10)。,192.168)。在这种情况下,FTP客户端应该使用公共IP地址。

This is exactly what does our Rebex FTP do in such situation. It works well (this behavior can be switched off). It can be even turned on for servers with multiple public IP addresses.

这正是我们的Rebex FTP在这种情况下所做的。它运行良好(这种行为可以关闭)。它甚至可以为拥有多个公共IP地址的服务器打开。

I don't know whether similar workaround is possible with FtpWebRequest.

我不知道FtpWebRequest是否也有类似的解决方案。

You can download trial and check whether it solves your problem.

你可以下载试用版,看看它是否能解决你的问题。

#4


2  

After a lot of digging around I found the only way to solve this problem was to change the PASV settings on the server.

在进行了大量的挖掘之后,我发现解决这个问题的唯一方法是改变服务器上的PASV设置。

Fortunately I control both the client and server machines so I was able to tell the server (FileZilla in my case) to use the public IP rather than the private IP.

幸运的是,我同时控制了客户机和服务器机器,因此我能够告诉服务器(在我的例子中是FileZilla)使用公共IP而不是私有IP。

#5


1  

Your FTP server is misconfigured.

FTP服务器配置错误。

In the passive mode the server reports an IP address and port to which the client should connect to for a data transfer. Your FTP server reports its IP address within an internal network, although it's behind a firewall/NAT. The client cannot connect to that internal address for obvious reason. You have to configure the FTP server to report its external IP address.

在被动模式下,服务器报告一个IP地址和端口,客户端应该连接到该地址和端口进行数据传输。您的FTP服务器在一个内部网络中报告它的IP地址,尽管它在防火墙/NAT之后。显然,客户端无法连接到该内部地址。您必须配置FTP服务器来报告其外部IP地址。

How that's done is server-specific and you didn't tell us, what your FTP server is.

这是特定于服务器的,你没有告诉我们,你的FTP服务器是什么。


Some answer here suggest using the active mode.

这里的一些回答建议使用主动模式。

request.UsePassive = false;

But that can help only if there's no firewall/NAT between the client and the server, in which case you won't have the problem in the first place (unless the server is really broken and reports a completely wrong IP address, not only the internal one). Or if the firewall/NAT is configured to allow inbound connections, what is not usual.

但只有在客户端和服务器之间没有防火墙/NAT时,这才会有所帮助,在这种情况下,您首先不会遇到问题(除非服务器真的被破坏,报告一个完全错误的IP地址,而不仅仅是内部的IP地址)。或者,如果防火墙/NAT被配置为允许入站连接,这是不常见的。


Another approach is using a different FTP library that can workaround the problem by ignoring the incorrect IP address reported by the server and using the primary/control connection IP address. Or by using the EPSV command instead of PASV command that implicitly uses the primary/control connection IP address.

另一种方法是使用不同的FTP库,通过忽略服务器报告的不正确的IP地址并使用主/控制连接IP地址来解决这个问题。或者使用EPSV命令,而不是使用隐式使用主/控制连接IP地址的PASV命令。