Kodi Community Forum
[Script] Default Route Manager - Printable Version

+- Kodi Community Forum (https://forum.kodi.tv)
+-- Forum: Support (https://forum.kodi.tv/forumdisplay.php?fid=33)
+--- Forum: Add-on Support (https://forum.kodi.tv/forumdisplay.php?fid=27)
+---- Forum: Program Add-ons (https://forum.kodi.tv/forumdisplay.php?fid=151)
+---- Thread: [Script] Default Route Manager (/showthread.php?tid=163885)



[Script] Default Route Manager - wellspokenman - 2013-05-02

Not an addon, but i've been doing this for a while now and it's really handy to change the country your xbmc is in for some streaming services. I don't want the XBMC box doing VPN termination, and have separate routers doing that for me. If you do the same then this may be interesting for you. In addition to changing the default gateway the script also scrapes www.whatismyip.com to verify what your new IP is. The regex will ferret out the plain text reference to the country it has detected so you only need to change the country fields below to adjust it to your needs.

Let's assume you have three routers: your default gateway, your first VPN router and your second VPN router. You'd then have three copies of the python vpn script in the code below. All that would be different in each of them would be the final 'route add' statement, deciding which route will become the new gateway of last resort.

Also, for adding each of them to favourites.xml, use the following code (adding them from the interface does not work as it treats them as media to be played, not scripts to be run)
Code:
<favourite name="VPN - UK" thumb="/home/xbmc/scripts/uk.png">RunScript(&quot;special://profile/scripts/vpn-uk.py&quot;)</favourite>

If your VPN router is in a different network and you've got wifi on the xbmc box you could change the commands to disable or enable the wlan0 interface instead. That's what I did for quite a while until I changed the network setup.

Code:
import os, sys, re, urllib2, fileinput
import xbmcgui, xbmc, xbmcaddon

def replaceAll(file,searchExp,replaceExp):
    for line in fileinput.input(file, inplace=1):
        if searchExp in line:
            line = line.replace(searchExp,replaceExp)
        sys.stdout.write(line)

def whatismyip():
    try:
        url = "http://www.whatismyipaddress.com"
        opener = urllib2.build_opener()
        opener.addheaders = [('User-agent', 'Mozilla/5.0')]
        infile = opener.open(url)
        link=infile.read()
        infile.close()
        myip = re.findall(r'name="LOOKUPADDRESS" value="(.+?)" maxlength="15"', link)
        mycountry = re.findall(r'<tr><th>Country:</th><td>(.+?)</td></tr>', link)
        print link
        return mycountry+myip
    except:
        dialog = xbmcgui.Dialog()
        ok = dialog.ok("ERROR",'', "Unable to verify public IP.")
        
def checkip():
    if (whatismyip()[0]) == "TYPE THE NAME OF THE COUNTRY YOU ARE LOOKING FOR":
        print "THIS TEXT GOES TO YOUR XBMC LOG"
        print "Your public IP is: "+(whatismyip()[1])
        dialog = xbmcgui.Dialog()
        ok = dialog.ok("THIS TEXT SHOWS UP IN XBMC",'', "Your public IP is: "+(whatismyip()[1]))
    elif (whatismyip()[0]) == "United Kingdom":
        print "God save the Queen"
        print "Your public IP is: "+(whatismyip()[1])
        dialog = xbmcgui.Dialog()
        ok = dialog.ok("God save the Queen", '',"Your public IP is: "+(whatismyip()[1]))
    elif (whatismyip()[0]) == "Germany":
        print "Es lebe die heilige Deutschland!"
        print "Your public IP is: "+(whatismyip()[1])
        dialog = xbmcgui.Dialog()
        ok = dialog.ok("Es lebe die heilige Deutschland!", '',"Your public IP is: "+(whatismyip()[1]))
    elif (whatismyip()[0]) == "Anonymous Proxy":
        print "Anonymous"
        print "Your public IP is: "+(whatismyip()[1])
        dialog = xbmcgui.Dialog()
        ok = dialog.ok("We are Anonymous.", '',"Your public IP is: "+(whatismyip()[1]))
    else:
        print "Error checking IP."
        dialog = xbmcgui.Dialog()
        ok = dialog.ok("Unable to Verify  Your Country:",'', "Your public IP is: "+(whatismyip()[1]))

def changeip():
    os.system("echo YOURPASSWORD | sudo -S route del default gw 192.168.X.1")
    os.system("echo YOURPASSWORD | sudo -S route del default gw 192.168.X.2")
    os.system("echo YOURPASSWORD | sudo -S route del default gw 192.168.X.3")
    os.system("echo YOURPASSWORD | sudo -S route add default gw 192.168.X.2 eth0")

progress = xbmcgui.DialogProgress()
progress.create('Please wait', '', 'Changing your public IP')
progress.update(0,'' )
changeip()
progress.update(50,'', 'Changing your public IP......' )
checkip()
progress.update(100,'', 'Changing your public IP.........' )



RE: [Script] Default Route Manager - thematy - 2019-06-25

Hi there,

I am reviving this thread, as looks like exactly what I am looking for.

Can I use the script above with Kodi 18.1, and able to switch between the two gateways I have on my network?

Sorry, but I never used scripting before in XBMC\Kodi and I wouldn;t know where to start.

Thanks.


RE: [Script] Default Route Manager - wellspokenman - 2019-08-09

(2019-06-25, 16:51)thematy Wrote: Hi there,

I am reviving this thread, as looks like exactly what I am looking for.

Can I use the script above with Kodi 18.1, and able to switch between the two gateways I have on my network?

Sorry, but I never used scripting before in XBMC\Kodi and I wouldn;t know where to start.

Thanks.
Well that's a blast from the past - I had forgotten about this one. The same idea should work, but these days we have ip info to check IP address stuff, so the whatismyip function is as simple as:
python:

import ipinfo
def whatismyip():
    try:
        ip = ipinfo.getHandler().getDetails()
        return [ip.country_name, ip.ip]
    except:
        dialog = xbmcgui.Dialog()
        ok = dialog.ok("ERROR",'', "Unable to verify public IP.")

Then adjust your gateway settings accordingly. This network syntax is for linux, but there are equivalent commands in Windows. Only issue I can see is that they require elevation, so you'd have to start Kodi as admin.