Created
March 6, 2012 11:06
-
-
Save portablejim/1985696 to your computer and use it in GitHub Desktop.
Network tester that is not working
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/python | |
import netifaces | |
import os | |
import requests | |
from subprocess import Popen, PIPE | |
DEVICE = 'eth0' | |
TEST_URL = 'http://uow.edu.au' | |
TEST_DNS = 'uow.edu.au' # DNS record to test | |
class ContentType: | |
NONE = 0 | |
OTHER = 1 | |
UNI = 2 | |
EI = 3 | |
class NetworkTester: | |
def __init__(self): | |
self.ethernet = False | |
self.address = False | |
self.content = ContentType.NONE | |
def _TestEthernet(self): | |
# Interface may be down and carrier status may be invalid | |
interfaceStatus = open('sys/class/net/eth0/operstate','r').read() | |
interfaceStatus = interfaceStatus.rstrip() | |
if interfaceStatus == "down": | |
os.execl('/sbin/ifconfig', DEVICE, 'up') | |
status = open('/sys/class/net/eth0/carrier', 'r').read(1) | |
connected = bool(status) | |
ethernet = connected | |
return | |
def _TestAddress(): | |
addresses = [] | |
# Get all addresses on interface | |
for addr in netifaces.ifaddresses(DEVICE)[netifaces.AF_INET]: | |
addresses.append(addr['addr']) | |
for addr in netifaces.ifaddresses(DEVICE)[netifaces.AF_INET6]: | |
addresses.append(addr['addr']) | |
# Remove local addresses | |
for addr in addresses: | |
if addr.startswith('fe80'): | |
addresses.remove(addr) | |
address = len(addresses) > 0 | |
return | |
def _TestContent(): | |
response = requests.get(TEST_URL, timeout=5) | |
if response.statuscode != requests.codes.ok: | |
return | |
# Got some content | |
content = ContentType.OTHER | |
# Test for Everywhere Internet login web page | |
if 'my.everywhereinternet.com' in content: | |
content = ContentType.EI | |
return | |
# Will only return NOERROR if inside UNI (UOW) network | |
# Will return 'connection timed out' if on EI | |
p = Popen('dig', '130.130.68.1', TEST_DNS, stdout=PIPE) | |
dns_output = p.communicate() | |
if 'connection timed out' in dns_output: | |
content = ContentType.EI | |
return | |
if 'NOERROR' in dns_output: | |
content = UNI | |
return | |
def TestNetwork(self): | |
_TestEthernet() | |
# Cannot continue since next dependant on success | |
if ethernet == False: | |
return | |
_TestAddress() | |
# Cannot continue since next dependant on success | |
if address == False: | |
return | |
_TestContent() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment