0

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()
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by zachabesh
0

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. ;)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.