socket.AF_INET

Matt Herzog msh at blisses.org
Sat Apr 19 11:11:07 EDT 2008


Hi All.

I'm trying to write a script that will send me an email message when my IP address changes on a specific NIC. On Linux, the script works. On FreeBSD, it fails with: 

Traceback (most recent call last):
File "./pyifcheck.py", line 22, in <module>
if get_ip_address('xl0') == IPADDY:
File "./pyifcheck.py", line 18, in get_ip_address
struct.pack('256s', ifname[:15]) )[20:24])
IOError: [Errno 25] Inappropriate ioctl for device

The script is below.
###################################################################
SENDMAIL = "/usr/sbin/sendmail"
IPADDY = "85.126.250.328"
#IFCONFIG = "/sbin/ifconfig
import os
import smtplib
import socket
import fcntl
import struct

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15]) )[20:24])

#get_ip_address('xl0')

if get_ip_address('xl0') == IPADDY:
        print "nevermind"

else:   

        p = os.popen("%s -t" % SENDMAIL, "w")
        p.write("To: matthew.herzog at gmail.com\n")
        p.write("Subject: YOUR IP ADDRESS HAS CHANGED\n")
        p.write("\n") # blank line separating headers from body
        p.write("Your IP addy has changed to $getipaddy\n")
        p.write("some more text\n")
        sts = p.close()
        if sts != 0:

                print "Sendmail exit status", sts


##############################################################################

Thanks for any suggestions.

-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too dark to read."   
--  Groucho Marx




More information about the Python-list mailing list