为什么即使 DNS 提供了有效的 IP 地址,并且 ftp 到该地址成功,但 ftp 到主机名仍然会失败?

为什么即使 DNS 提供了有效的 IP 地址,并且 ftp 到该地址成功,但 ftp 到主机名仍然会失败?

这怎么可能?

NodeX# ping ftp.company.com
PING ftp.company.com (xx.yy.8.11) 56(84) bytes of data.
64 bytes from ftp.company.com (xx.yy.8.11): icmp_seq=1 ttl=127 time=0.657 ms
...
^C
NodeX# /usr/bin/myftp.py -fftp.company.com -uuser -ppasssword 
Transferring myftp.tgz to ftp.company.com:Incoming/
*** ftp login failed: [Errno -5] No address associated with hostname
Operation failed, exit code=3

如果我这样做,它就会起作用:

NodeX# /usr/bin/myftp.py -fxx.yy.8.11 -uuser -ppassword 
Transferring myftp.tgz to ftp.company.com:Incoming/
Operation completed successfully

此外,如果我在 /etc/hosts 中创建一个条目,那么它可以使用名称。发生了什么?

挖掘显示:

NodeX# dig ftp.company.com ANY

; <<>> DiG 9.5.0-P2 <<>> ftp.company.com ANY
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 21125
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;ftp.company.com. IN      ANY

;; ANSWER SECTION:
ftp.company.com. 28800 IN A       xx.yy.8.11

;; Query time: 1 msec
;; SERVER: xx.zz.8.34#53(xx.zz.8.34)
;; WHEN: Thu Oct 11 14:53:00 2012
;; MSG SIZE  rcvd: 63

resolv.conf 显示 2 个名称服务器,dig @each 返回 IP。

Python 2.6 代码如下:

    try:
        print("Preparing ftplib.FTP for " + ftpHost)
        ftp = ftplib.FTP()
    except Exception, e:
        print("*** ftp prep failed: %s" % e.__str__())
        return 3
    end
    try:
        print("Connecting to '" + ftpHost + "'")
        ftp.connect(ftpHost, ftpPort)
    except Exception, e:
        print("*** ftp connection failed: %s" % e.__str__())
        return 4
    end

输出为:

Preparing ftplib.FTP for ftp.company.com
Connecting to 'ftp.company.com'
*** ftp connection failed: [Errno -5] No address associated with hostname
Operation failed, exit code=4

尽管:

Node2414332# ping ftp.company.com
PING ftp.company.com (xx.yy.8.11) 56(84) bytes of data.
64 bytes from ftp.company.com (xx.yy.8.11): icmp_seq=1 ttl=127 time=0.804 ms

解决了:

尽管我们的 Python 的 createconnection() 在 getaddrinfo() 中在给定主机名时会失败(见下文),但它可以使用地址。方便的是,gethostbyname() 可以工作,因此我们有一个简单的修复:createconnection(gethostbyname(hostname))。Weerd。

相关内容