WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

How To
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

WAN IP Address



;
;=========================================================================
; A program that obtains the WAN (external) IP of a personal router and
; tracks it for changes.  The program runs in the systray and displays
; the current IP as the icon's tooltip.  If the IP changes, a message box
; is displayed, pausing the script. There is no code to handle pushing
; "Cancel" on the message box - that's up to you. If you want to send
; an email, it would be easy to change the code to use POSTIE.
; Change the value of the "password" variable to whatever your router's
; password is.
;
; REQUIREMENTS:
; - xmt Extender - third party extender that can be obtained from: http://winbatch.hpdd.de
; - An icon .It must be in the same directory as the program.
;
;=========================================================================

;=========================================================================
:initialize

  IntControl(12,5,0,0,0)                           ; Allow quiet termination
  IntControl(1002,0,0,0,0)                         ; Hide tray icon


  AddExtender("WWINT34I.DLL")        ; Internet Extender
  AddExtender("xmt34i.dll")          ; Detlev's XMT Tools extender

  TimeDelay(1)
  exepath = FilePath(IntControl(1004,0,0,0,0))
  DirChange(exepath)

  ; Turn on debugging?
  If IsKeyDown(@CTRL) || FileExist("%exepath%debugon.chk")
    Debug(@ON)
  EndIf

  ; Turn on debug tracing?
  If IsKeyDown(@SHIFT) || FileExist("%exepath%debugtraceon.chk")
    DebugTrace(@ON,"%exepath%trace2.txt")
  EndIf

  GoSub definefuncs

  clicked = 0
  AddToTray = 1
  HideIcon = 1
  ModTray = 4                        ; Modify currently-running script in the system tray
  ModTooltip = 1                     ; Modify tooltip
  RemoveIcon = 2
  tooltip = "Checking router IP"
  null = ""

  password = ":password"             ; Put your router password here.  Needs the ":"

  b64pw = mtStrEncode64(password)    ; Base64 code the password

  Iconfile = StrCat(exepath,"daffy.ICO")

  time = TimeYmdHms()
  interval = "0000:00:00:00:01:00"   ; Interval between checks.  One minute
  checktime = TimeAdd(time,interval)
  currentip = GetRouterIP(b64pw)
  priorip = currentip

  stat = IntControl(1007,addtotray,hideicon,currentip,iconfile)
  If !stat
    Message("Error!","Error %stat% returned from 'addtotray'")
    Exit
  EndIf

;=========================================================================
:main

  While 1

    time = TimeYmdHms()                             ; Get current time

    If time >= checktime                            ; If past checktime
      checktime = TimeAdd(time,interval)            ; set next check time
      currentip = GetRouterIP(b64pw)                ; Get current IP
      If currentip != priorip                       ; If they aren't equal
        msg = StrCat("Router IP has changed from ",priorip, " to ",currentip)
        Message("Router IP",msg)                    ; Display a message
        priorip = currentip
      EndIf
    EndIf

    ; Modify tooltip to show time to next run
    IntControl(1007,modtray,ModTooltip,currentip,null)

    TimeDelay(.1)

    If IsKeyDown(@CTRL & @SHIFT) Then Break

  EndWhile

;============================================================
:finish

  IntControl(1007,removeicon,null,null,null)     ; Remove from systray

  Exit

;============================================================
:definefuncs

  #DefineFunction GetRouterIP(b64pw)

    ; Turn on debugging?
    If IsKeyDown(@CTRL) || FileExist("%exepath%debugon.chk")
      Debug(@ON)
    EndIf

    ; Turn on debug tracing?
    If IsKeyDown(@SHIFT) || FileExist("%exepath%debugtraceon.chk")
      DebugTrace(@ON,"%exepath%trace2.txt")
    EndIf

    strProto = "http://"
    strHost = "192.168.1.1"
    strPage = "/Status.htm"
    strUser = ""
    strPass = ""
    strHeaders = StrCat("Authorization: Basic ",b64pw)
    ; http://www.opinionatedgeek.com/DotNet/Tools/Base64Encode/Default.aspx
    size = 10000
    buf = BinaryAlloc(size)
    ;Get address of buffer
    bufaddr = IntControl (42, buf, 0, 0, 0)
    BinaryEodSet(buf, size)

    objHandle = iBegin (0,"","")
    objConnectHandle = iHostConnect (objHandle,strHost,@HTTP,strUser,strPass)
    objDataHandle = iHttpInit (objConnectHandle,"GET",strPage,strHeaders,4)
    strResult = iHttpOpen(objDataHandle,"",0,0)

    ;xx=iReadData(objDataHandle,"c:\temp\abc.htm")
    size = iReadDataBuf(objdatahandle,bufaddr,size)
    htmlfile = BinaryPeekStr(buf,0, size)
    iClose(objDataHandle)
    iClose(objConnectHandle)
    iClose(objHandle)

    wanloc = BinaryIndexEx(buf,0,"WAN:",@FWDSCAN,0)
    iploc1 = BinaryIndexEx(buf,wanloc,"IP Address:",@FWDSCAN,0)
    iploc2 = BinaryIndexEx(buf,iploc1,"size=2",@FWDSCAN,0)
    iploc3 = BinaryIndexEx(buf,iploc2,">",@FWDSCAN,0)
    iploc4 = BinaryIndexEx(buf,iploc3,"<",@FWDSCAN,0)
    len = iploc4-iploc3-1
    ip = BinaryPeekStr(buf,iploc3+1,len)
    BinaryFree(buf)

    Return(ip)

  #EndSubRoutine


  Return

Article ID:   W16485
File Created: 2011:01:12:09:34:02
Last Updated: 2011:01:12:09:34:02