socket.AF_INET

M

Matt Herzog

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: (e-mail address removed)\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.
 
J

Juergen Perlinger

Matt said:
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:

[snip]

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])

[OT: sorry Matt, hit email instead followup twice... stupid me]

My bets are that the SIOCGIFADDR opcode has a different numerical value for
BSD. Even if some names are portable, the numerical values aren't! I don't
have BSD, but using

find /usr/include -type f -name '*.h' | xargs grep SIOCGIFADDR /dev/null

should give some hints...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top