Last active
September 3, 2016 00:04
-
-
Save erm3nda/f25439bba66931d3ca9699b2816e796c to your computer and use it in GitHub Desktop.
Some tools for IP and websites with Python
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
# This is one of my regular scripts, i'd like to create my own functions to understand how everything works. | |
# Usually, when it comes to ip checking, people don't fully understand how it works, and tend to do some BAD checks. | |
# The MOST realiable way to check an IP it's let the system to tell you, cuz every language having networking can do that, need to do that. | |
# There's a few functions that uses various Python modules and functions to check and test IP and hostnames in many ways. | |
#!/usr/bin/env python | |
#! -*- coding: utf-8 -*- | |
import sys, re, socket, urllib2, httplib | |
from urlparse import urlparse | |
sys.dont_write_bytecode = True | |
# this is only regexp, consider using is_valid_ip() to allow 127.1 short ip | |
def is_ip(ip): | |
try: | |
ip = re.findall( r'[0-9]+(?:\.[0-9]+){3}', ip) | |
except: | |
pass | |
if ip: | |
pass # let check continue | |
else: | |
#print "ip not in proper format" | |
return False | |
if re.match( r'[0-9]+(?:\.[0-9]+){3}', ip[0]): # make it work for bad formatted text | |
ip = ip[0] | |
octect = ip.split(".") | |
for i in octect: | |
if int(i) > 255: | |
return False | |
return True | |
else: | |
return False | |
# this is the best method, because system has it's builtin mechanism | |
# if the system does accept it it's a valid ip | |
# this does not make any kind of connection to target | |
def is_valid_ip(ip): | |
try: | |
if socket.inet_aton(ip): | |
return True | |
else: | |
return False | |
except Exception, e: | |
print str(e).split(" ")[0], | |
pass | |
# creating a real connection to target | |
def is_working_addr(addr, port=80, timeout=10): | |
sock = socket.socket() | |
sock.settimeout(timeout) | |
try: | |
if sock.connect((addr, int(port))): # forced port int casting. why not? | |
sock.close() | |
return True | |
else: | |
return False | |
except Exception, e: | |
try: | |
print str(e).split("] ")[1], # Clean a bit the result | |
except: | |
print str(e), | |
pass | |
# we do dns search. none web is valid if dns don't work, right? | |
def is_valid_website(www): | |
#print www | |
try: | |
remove_http(www) | |
#print www | |
except: | |
pass | |
if socket.gethostbyname(www): | |
return True | |
else: | |
return False | |
# does accept both website and ip, but with http:// | |
def is_alive_addr(url): | |
try: | |
url = urlparse(url) | |
conn = httplib.HTTPConnection(url.netloc) | |
res = conn.request("HEAD", url.path) | |
return True | |
except: | |
return False | |
# same than is_valid_website plus loading it | |
def is_working_website(www): | |
try: | |
response = urllib2.urlopen(www).read() | |
return True | |
except: | |
return False | |
def is_valid_port(port): | |
if re.findall( r'[0-9]{1,5}', str(port)) and !port >= 65000: | |
return True | |
else: | |
return False | |
## Running autotest ## | |
if __name__ == "__main__": | |
import functions | |
functions.get_functions(__file__) | |
print "" | |
print "Testing each function \n" | |
someip = ["11.1", "54.175.222.246", "http://127.0.0.1"] # note 11.1 will be accepted by inet_aton() while not in is_ip() regexp | |
somebadip = ["0.0.0.256", "11.."] | |
someweb = ["http://google.com", "https://httpbin.org", "google.es"] | |
somebadweb = ["wwww.ww.es", "216.58.211.195", "www.badomain.domain"] | |
demoip = someip + somebadip | |
demoweb = someweb + somebadweb | |
# testing | |
print "Testing is_ip() with regexp\n" | |
for i in demoip: | |
print str(i) + " is " + str(is_ip(i)) | |
print "\nTesting is_valid_ip() with socket\n" | |
for i in demoip: | |
print str(i) + " is " + str(is_valid_ip(i)) | |
print "\nTesting is_working_addr() with socket connection \n" | |
for i in demoip: | |
print str(i) + " is " + str(is_working_addr(i)) | |
print "\nTesting ip's is_alive_addr() with httplib HEAD checks \n" | |
for i in demoip: | |
print str(i) + " is " + str(is_alive_addr(i)) | |
print "\nTesting webs is_working_addr() with httplib HEAD checks \n" | |
for i in demoweb: | |
print str(i) + " is " + str(is_alive_addr(i)) | |
print "\nTesting is_working_website() with urllib2 \n" | |
for i in demoweb: | |
print str(i) + " is " + str(is_working_website(i)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment