[Gambas-user] Wierd question

Pablo Vera pvera at ...729...
Fri May 20 15:48:46 CEST 2005


You don't need a browser, simply use a socket object to connect to the 
router.  Something like this:

-----------------------------------------------------------------------

PRIVATE sckComm AS Socket
PRIVATE Buffer AS String


' Startup, create the socket
PUBLIC SUB Main()
   sckComm = NEW Socket AS "sckComm"
END


' The Start SUB should be called by a timer or click event,
' this will fire a series of events, beginning by the connection
' to the router:

PUBLIC SUB Start()
   sckComm.Connect("192.168.1.1",80)
END


' When the connection is established, send your request:

PUBLIC SUB sckComm_Ready()
DIM S AS String
   Buffer = ""
   IF sckComm.Status = Net.Connected THEN
     S =     "GET http://192.168.1.1/Status.htm HTTP/1.1" & vbCRLF
     S = S & "Host: 192.168.1.1" & vbCrLf
     S = S & "Authorization: Basic " & ToBase64(":xxxxx") & vbCrLf
     S = S & vbCrLf
     WRITE #sckComm, S, Len(S)
   END IF
END
' *** You should replace the xxxxx above, with your router password,
' the : before the password is required.


' Read the response and save it in a buffer:

PUBLIC SUB sckComm_Read()
DIM S AS String
   IF sckComm.Status = Net.Connected THEN
     READ #sckComm, S, Lof(sckComm)
     Buffer = Buffer & S
   END IF
END


' When the connection closes, it means that the response is complete,
' now you need to parse it and extract the IP address:

PUBLIC SUB sckComm_Closed()
DIM S AS String
   IF Buffer<>"" THEN
     S = UCase$(Buffer)

' *** In here you would parse the html response and get the IP address.

   END IF
END


' This function is used to convert the "username:password" string to
' Base64, which is required by the authentication:

PUBLIC FUNCTION ToBase64(S AS String) AS String
DIM L AS Integer
DIM I AS Integer
DIM P AS Integer
DIM B AS Integer
DIM SS AS String
DIM SSS AS String
DIM TB64 AS String
DIM Table AS String
DIM Buf AS Byte
DIM Lim AS Integer
   TB64 = ""
   Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" &
           "abcdefghijklmnopqrstuvwxyz" &
           "0123456789+/"                 ' "=": Pad
   P = 1
   DO
     SS = Mid$(S, P, 3)
     L = Len(SS)
     SELECT CASE L
     CASE 0
       SSS = ""
       Lim = 0
       B = 0
     CASE 1
       SSS = "=="
       Lim = 2
       B = Shl(Asc(Mid$(SS, 1, 1)), 4)
     CASE 2
       SSS = "="
       Lim = 3
       B = Shl(Asc(Mid$(SS, 1, 1)), 12) + Shl(Asc(Mid$(SS, 2, 1)), 2)
     CASE 3
       SSS = ""
       Lim = 4
       B = Shl(Asc(Mid$(SS, 1, 1)), 16) + Shl(Asc(Mid$(SS, 2, 1)), 8) +
         Asc(Mid$(SS, 3, 1))
     END SELECT
     FOR I = 1 TO Lim
       Buf = B AND 63 ' 111111
       SSS = Mid$(Table, Buf+1, 1) & SSS
       B = B \ 64
     NEXT
     TB64 = TB64 & SSS
     P = P + 3
   LOOP UNTIL L < 3

   RETURN TB64
END FUNCTION

----------------------------------------------------------------------

I used the Networking / ClientSocket example as a startup point to write 
this.  I hope it helps a little.

Saludos,
Pablo Vera
______________________________________________________________________

Eric Damron wrote:
> I need to sense when the IP address on my cable modem changes.  To
> complicate the matter I have a Linksys router setting between my
> internal network (using NAT) and my cable modem.
> 
> I'm thinking that the only way to do this is to send a http request for
> the status page and then scrape the current IP address off of the html
> returned.  But since I want this to run unattended on my home server
> that runs at run level 3 I don't want a gui. 
> 
> Can I run the browser as a nonvisual object?  Thanks.




More information about the User mailing list