简体   繁体   English

使用Perls Net :: FTP从Windows主机获取文件

[英]Getting files from Windows host with Perls Net::FTP

The laptops in our company go to the network either about LAN (workplace) or about WLAN (conference room). 我们公司的笔记本电脑通过LAN(工作场所)或WLAN(会议室)进入网络。 Depending on how they go to the net, they get from DHCP different IPs. 根据它们上网的方式,它们从DHCP获得不同的IP。
Certain Perl application on a server, copies files from the client (eg laptop above) with Net::FTP. 服务器上的某些Perl应用程序使用Net :: FTP从客户端(例如上面的笔记本电脑)复制文件。 The piece of code looks like this: 该段代码如下所示:

# don't wait for ftp-timeout if the host is not reachable
my $alive = Net::Ping::External(host => $clnt_host);
if ($alive) {
    $ftp = Net::FTP->new($clnt_host, Debug => 0, Timeout => 200)
    or return "Cannot connect to $clnt_host: $@\n";
    ....
    ....
}
else {
  dbgout(1, "Host $clnt_host unreachable.\n");
  $st = "'FTPGETFAILED'";
  return ($st);
}

Sometimes the code above doesn't work: Net::Ping::External() returns "alive", but Net::FTP->new() gets a "timeout". 有时上面的代码不起作用:Net :: Ping :: External()返回“活动”,但Net :: FTP-> new()获得“超时”。

Obviously "FTP" and "ping" resolve the hostname differently. 显然,“ FTP”和“ ping”以不同的方式解析主机名。
On the OS ping reslove as follows: 在操作系统上ping relove如下:

C:\Users\converter>ping -n 1 lap314-034

Ping wird ausgeführt für lap314-034.domain.de [10.140.12.110] mit 32 Bytes Daten:
Antwort von 10.140.12.110: Bytes=32 Zeit=2ms TTL=127

However, "nslookup" returns 2 possibilities: 但是,“ nslookup”返回2种可能性:

C:\Users\converter>nslookup lap314-034
Server:  domaincontroller.domain.de
Address:  123.123.123.123

Name:    lap314-034.domain.de
Addresses:  10.192.3.145
          10.140.12.110

The not active IP address is delivered from nslookup at the first place back. 无效的IP地址是从nslookup 首先返回的。
I suppose that Net::FTP also uses this address to connect to the client. 我想Net :: FTP也使用此地址连接到客户端。

How can I "convince" FTP to use the active DNS entry for the connection? 如何“说服” FTP使用活动的DNS条目进行连接?

============================================================= ================================================== ===========

Thanks for your answers. 感谢您的回答。 I followed your suggestions. 我听了你的建议。 The solution bases on: http://code.activestate.com/lists/perl-win32-users/32624/ 该解决方案基于: http : //code.activestate.com/lists/perl-win32-users/32624/

#------------------------------------------------------------------
sub getActiveIP {
#------------------------------------------------------------------
  my $hostname     = shift;

  my $host = (gethostbyname ($hostname))[0] or return undef;
  my @addr = gethostbyname ($host);
  # delete the first 4 array elements
  splice @addr, 0, 4;
  foreach (@addr) {
    my $IPstr = sprintf ("%s", &inet_ntoa ($_));
    my $alive = ping(host => $IPstr);
    if ($alive) {
      return $IPstr;
    }
  }
  return undef;
}

Nevertheless, I believe that a widespread Perl-library should not make such surprises to the user. 尽管如此,我相信广泛的Perl库不应为用户带来这种惊喜。

How can I "convince" FTP to use the active DNS entry for the connection? 如何“说服” FTP使用活动的DNS条目进行连接?

There is no good way to let Net::FTP decide it. 没有好的方法让Net :: FTP决定它。 Instead you should determine it outside of Net::FTP and then use the usable IP address instead of the hostname in Net::FTP. 相反,您应该在Net :: FTP之外确定它,然后使用可用IP地址代替Net :: FTP中的主机名。

Maybe you would be able to use only Net::FTP with the new versions (Net::FTP > 3.0) which can use IO::Socket::IP instead of IO::Socket::INET as the underlying module. 也许您只能使用新版本(Net :: FTP> 3.0)的Net :: FTP,它可以使用IO :: Socket :: IP而不是IO :: Socket :: INET作为基础模块。 This module can try all the IP addresses returned for a hostname until it gets a successful connection. 该模块可以尝试为主机名返回的所有IP地址,直到获得成功的连接为止。 But these retries will be done for every connection, that is the control connection and every data transfer. 但是,将对每个连接(即控制连接和每个数据传输)都执行这些重试。 Since the connection to the inactive host only fails after some timeout everything will just be horribly slow. 由于与非活动主机的连接仅在超时后才会失败,因此一切都会变得非常缓慢。

解决方案似乎很明显:获取IP地址,对其进行ping操作,找出其中一个,然后在Net::FTP构造函数中使用IP地址代替主机名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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