Last active
December 2, 2023 03:09
-
-
Save pabluk/a47aa7c91ea4802314f7 to your computer and use it in GitHub Desktop.
Livebox IP
This file contains hidden or 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 python | |
""" | |
This file provides a class to connect to the Livebox router | |
and get his public IP address. | |
The Livebox router is provided by Orange, a French ISP. | |
Usage | |
----- | |
$ python livebox-ip.py | |
or with the `dyndnsc` client (https://nsupdate.info/) | |
$ dyndnsc --hostname test.dyndns.com --userid bob --method=command,command:"python livebox-ip.py" | |
This script was tested with Python 2.7 and 3.4 on Linux | |
using a Livebox 2 firmware: FAST3XXX_6814BC | |
Author: Pablo SEMINARIO <[email protected]> | |
URL: https://gist.github.com/pabluk/a47aa7c91ea4802314f7 | |
""" | |
import re | |
import socket | |
import struct | |
try: | |
from urllib.request import urlopen | |
except ImportError: | |
from urllib2 import urlopen | |
class Livebox: | |
""" | |
A simple class to connect to the Livebox router and extract his | |
public IP address. | |
""" | |
def _get_default_gateway(self): | |
""" | |
Read the default gateway directly from /proc. Only for Linux systems. | |
https://gist.github.com/ssokolow/1059982 | |
""" | |
with open("/proc/net/route") as fh: | |
for line in fh: | |
fields = line.strip().split() | |
if fields[1] != '00000000' or not int(fields[3], 16) & 2: | |
continue | |
return socket.inet_ntoa(struct.pack("<L", int(fields[2], 16))) | |
def get_public_ip(self): | |
""" | |
Parse Livebox's homepage and extract public IP address | |
""" | |
livebox_homepage = "http://%s/" % self._get_default_gateway() | |
response = urlopen(livebox_homepage) | |
response_text = response.read().decode('utf-8') | |
ip_re = r'<td class="libelle">Adresse IP WAN :</td>\n\t\t\t\t\t' \ | |
r'<td class="value">(?P<ipaddr>[\d\.]+)</td>' | |
re_match = re.search(ip_re, response_text) | |
return re_match.group('ipaddr') | |
if __name__ == "__main__": | |
livebox = Livebox() | |
print(livebox.get_public_ip()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment