Hurricane Electric's IPv6 Tunnel Broker Forums

Advanced search  

News:

Welcome to Hurricane Electric's Tunnelbroker.net forums!

Author Topic: Here's a python script for updating your tunnel. Please test it out.  (Read 2473 times)

PatrickDickey

  • Newbie
  • *
  • Posts: 40

Hi everyone,

I wrote a python script that can be run as either a cronjob (or Scheduled Task on Windows) or manually to update your tunnel. The way it's set up right now, you need to have a file in the same directory called "UserInfo.txt" which should have

UserID
Password (MD 5 Hash)
Tunnel ID
an IP Address to check against

It will write the results to a file called results.log in the same directory as well.  You can edit the script and enter your user information in the variables (and then remove the call to the ReadInformation() method), or create the file. I've tested it on Ubuntu 12.10 with Python 2.7.3, so if you'll let me know what your experiences are, I'll greatly appreciate it. Eventually I want to make a GUI version of the script (and a Python 3 version).

Code: [Select]
#!/usr/bin/env python

#Version: 0.2.2012-10-23
#Purpose: Intended to run as a cronjob or manually. It gets the information from the UserInfo.txt file, and updates the tunnel
#         if the IP Address has changed.
#
#       TunnelBrokerUpdater: Takes User Information and IP Address, and updates Tunnelbroker IPv6 Tunnel if necessary.
#       Copyright (C) 2012 Patrick C. Dickey <pdickeybeta@gmail.com or support@patscomputerservices.com>
#
#       This program is free software: you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation, either version 3 of the License, or
#       any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program.  If not, see <http://www.gnu.org/licenses/>.
#       Also a copy of this license is included as part of the project files in license.txt
#       If you wish to use this code in a proprietary application, please contact me about
#       relicensing conditions.


import urllib2
import sys
import os
import datetime

strOldIPAddress = ""
strUserName = ""
strPassword = ""
strTunnelID = ""
strBuffer = ""
now = datetime.datetime.now()

# Update the tunnel with the user name, password, tunnel ID, and new IP Address
def UpdateTunnel(strUserName, strPassword, strTunnelID, strIPAddress):
    url="https://ipv4.tunnelbroker.net/ipv4_end.php?ip="+strIPAddress+"&pass="+strPassword+"&apikey="+strUserName+"&tid="+strTunnelID
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    return response.read()

# Get the current IP Address for the computer/network from a public website (this gets the "Public IP Address", which may not be the same as your IP Address)
def getIPAddress():
    url="http://bot.whatismyipaddress.com"
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    return response.read()

#Checks to see if the IP Address has changed. If so, it runs the Update Tunnel script and returns the response. If not, it returns "No update needed."
def IPCheck():
    global strOldIPAddress
    strIPAddress = ""
    strUpdateResponse = ""
    global strUserName
    global strPassword
    global strTunnelID
   
    strIPAddress = str(getIPAddress())
    if strIPAddress != strOldIPAddress:
            strUpdateResponse = UpdateTunnel(strUserName, strPassword, strTunnelID, strIPAddress) + "\n"
            strOldIPAddress = strIPAddress
    else:
            strUpdateResponse = "No update needed.\n"
    return strUpdateResponse

#Used for writing log file and for writing the UserInfo.txt file (with the new IP Address)
def WriteInformation(fileNameToOpen, strWrtOApnd, strngBuffer):
        # Write the user information to the file for the next time the app is open. Include IP Address.
       
        global strUserName
        global strPassword
        global strTunnelID
        global strOldIPAddress
        fileOut = open(os.path.join(os.path.dirname(__file__), fileNameToOpen), strWrtOApnd)
        fileOut.write(strngBuffer)
        # Close the file after writing. Hopefully it will write valid data.               
        fileOut.close()

# Reads the UserInfo.txt file. If it's valid, it supplies the information for the Username, Password, and Tunnel ID. If not, logs the error and quits.
def readInformation():
    # Read in the UserInfo.txt file, if it exists. If not, then put blank values in the text boxes.
    try:
            fileInfo = open(os.path.join(os.path.dirname(__file__), "UserInfo.txt"), "r")
            fileList = []
            fileList = fileInfo.readlines()
            global strOldIPAddress
            global strUserName
            global strPassword
            global strTunnelID
            #Assign the data from the file to their respective textboxes.
             
            strUserName = fileList[0].strip()
            strPassword = fileList[1].strip()
            strTunnelID = fileList[2].strip()
            strOldIPAddress = str(fileList[3]).strip()
             
    except IOError:
           
            strngBuffer = now.strftime("%Y-%m-%d %H:%M") + ":  " + "Unable to Open the UserInfo.txt file\n"
            WriteInformation("results.log", "a", strngBuffer)
            sys.exit()
    except IndexError:
            strngBuffer = now.strftime("%Y-%m-%d %H:%M") + ":  " + "Index out of Bounds error. Do you have four values in UserInfo.txt?\n"
            WriteInformation("results.log", "a", strngBuffer)
            sys.exit()
   
    else:
            #Close the file, so we can write to it later.
            fileInfo.close()

#Main method that controls the whole script.

readInformation()
strBuffer = now.strftime("%Y-%m-%d %H:%M") + ":  " + IPCheck()
WriteInformation("results.log", "a", strBuffer)
strBuffer = strUserName + "\n" + strPassword + "\n" + strTunnelID + "\n" + strOldIPAddress
WriteInformation("UserInfo.txt", "w", strBuffer)

Also, if you need to hash your password, I have a script for that:

Code: [Select]
#!/usr/bin/env python

#       TunnelBrokerUpdater: Takes User Information and IP Address, and updates Tunnelbroker IPv6 Tunnel if necessary.
#       Copyright (C) 2012 Patrick C. Dickey <pdickeybeta@gmail.com or support@patscomputerservices.com>
#
#       This program is free software: you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation, either version 3 of the License, or
#       any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program.  If not, see <http://www.gnu.org/licenses/>.
#       Also a copy of this license is included as part of the project files in license.txt
#       If you wish to use this code in a proprietary application, please contact me about
#       relicensing conditions.
#
#       I combined two scripts from http://wiki.python.org/moin/Md5Passwords and
#       http://www.geekademy.com/2010/10/creating-hashed-password-for-vnc.html The geekacademy code is not offered under any license,
#       so I may have to change the licensing terms if pressed to. However, I believe that the combination of code here is different
#       enough from both snippets, that there shouldn't be any issues.

if __name__ == '__main__':
    import sys
    import hashlib
   
    #If the script was called with a password, then it will hash that. Otherwise, the user will be prompted for their password.
   
    if len(sys.argv) > 1:
        print hashlib.md5(sys.argv[1]).hexdigest()
    else:
        unhashpass = raw_input( "Enter your password here. " )
        print  hashlib.md5(unhashpass).hexdigest()

Once I'm sure it works, I'll upload a tar.gz file that contains the license, a README file, my contact information, and a blank UserInfo.txt file (along with the script(s)) to my website (and can upload them here as well, if requested).

Thanks, and have a great day:)
Patrick.
Logged