Hi!
I am writing a small Python script.It's function is to tell user external IP address log it into the file and when next time if the script is executed and IP is changed it must display message
Here is the code.

I have writed the script it tells users IP address.But I want to display a message to user if IP address is changed

import socket
import urllib2
import sys
import time
def read():
    f = open("log.txt", "r+")
    for line in f:
        print line
tt = time.ctime()
print "Welcome to Python Script"
print "The Function of Program is to Display IP Address"
print "Fetching Host Name"
ht = socket.gethostname()
print "Host Name is ", ht
print "Fetching Host Ip Address"
ip = socket.gethostbyname(ht)
print "IP by Host Name is ", ip
print "Fetching External IP...."
response = urllib2.urlopen("http://darkstar.heliohost.org/ip.php")
html = response.read()
print "External IP Address is ", html
print "Saving IP"
f = open("log.txt", "a")
f.write(html + " " + "on" + " " + tt + "n" )
f.close()
print "Reading Log Files.........."
read()
sys.exit()

Pop your log file open before you get the current ip and parse out the most recent ip logged. Then compare it with the current ip address.

if most_recent_ip != current_ip:
     print "Your IP changed!"
else:
     pass

Also, your last 3 lines don't really do much. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.