Created
January 30, 2018 21:45
-
-
Save josifoski/bd059d50d8c5595620074d97c939fab3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3.5 | |
# Script for hourly logging own external IP address | |
# By Aleksandar Josifoski https://twitter.com/josifsk | |
# 2017 Octobar 21 | |
import re | |
import html | |
import codecs | |
import datetime | |
import time | |
import requests | |
import os | |
time.sleep(10) | |
url = 'https://whatismyipaddress.com/' | |
dir_in = '/data/Scrape/iplog/' | |
assert os.path.exists(dir_in), "directory have to be created" | |
headers = { | |
'User-Agent': 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17', | |
'From': 'josifoski@gmail.com' # This is another valid field | |
} | |
while True: | |
response = requests.get(url, headers=headers) | |
s = html.unescape(response.content.decode('utf-8')) | |
#with codecs.open('abc.txt', 'w') as f: | |
# f.write(s) | |
try: | |
ip = re.search(r'//whatismyipaddress.com/ip/(.*?)"', s).group(1) | |
log = codecs.open(dir_in + 'ip.log', 'a', 'utf-8') | |
now = str(datetime.datetime.now())[:16] | |
log.write(now + ' ' + ip + os.linesep) | |
log.close() | |
except: | |
pass | |
time.sleep(3600) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment