Machine identification

Jeff Epler jepler at unpythonic.net
Wed Sep 8 22:53:02 CEST 2004


I don't have a machine with the particular misconfiguration John does,
but I have suggested a different method which should identify the IP
address of the external interface, not the loopback device.

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("1.1.1.1", 1))
    print s.getsockname()

No packets are actually sent on s, so it doesn't matter that 1.1.1.1
probably doesn't exist, or in any case doesn't intend to receive UDP
packets on port 1.  It's just a routable IPV4 address.

Once the socket is created, its "name" will be filled out, and should
correspond to the address of the interface that would actually be used
to send the packet.

If this address is a non-routable address, such as 10.0.2.2, which will
be NATted by some other machine, the problem can't be solved by writing
a few lines of Python to execute on the NATted machine.  However, this
routine will help when DNS is misconfigured (so that gethostbyname
returns the wrong address) or when gethostname misbehaves (returning the
wrong string, like "localhost.localdomain")

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20040908/f10b7c01/attachment.pgp>


More information about the Python-list mailing list