Machine identification

Skip Montanaro skip at pobox.com
Wed Sep 8 17:40:09 EDT 2004


    Jeff> If this address is a non-routable address, such as 10.0.2.2, which
    Jeff> will be NATted by some other machine, the problem can't be solved
    Jeff> by writing a few lines of Python to execute on the NATted machine.

When I need my cable modem's address I use the following script (called,
oddly enough, printip):

    #!/usr/bin/env python

    import smtplib, re, socket, os, sys

    s = smtplib.SMTP("suitable.smtp.server.name.here")
    code, msg = s.helo()
    s.quit()
    mat = re.search("([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)", msg)

    if mat is not None:
        print mat.group(1)
    else:
        print "IP address unknown"
        sys.exit(1)

It generates a remote SMTP connection and picks the ip address out of the
helo() response.  I don't know if this is a general solution, but it's
worked for me for several years (modtime on the script file says Dec 7
2001).  The remote SMTP server I connect to runs sendmail.  YMMV.

Skip



More information about the Python-list mailing list