Proyecto

General

Perfil

131 francis
' Gambas class file
' 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 2 of the
' License, OR (at your option) any later version.
'
' This program IS distributed IN the hope that it will be useful, but
' IS provided AS IS, WITHOUT ANY WARRANTY; without even the implied
' warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
' NON-INFRINGEMENT. See the GNU General PUBLIC License FOR more details.
'
' You should READ the GNU General PUBLIC License
' IF you have non, WRITE TO the Free Software
' Foundation, INC., 59 Temple Place - Suite 330, Boston,
' # MA 02111-1307, USA.
' ----------------------------------------------------------------------------
' ################################################
' # file name : scaniesltsp.class
' # created in project : scanies-ltsp
' # created by : Francisco Paniagua Sánchez
' # mail : adminies.franciscodeorellana@edu.juntaextremadura.net
' # created at : 10/03/2009
' # last test with : gambas-2.7-1
' ################################################

STATIC clase AS scaniesltsp
PRIVATE proceso AS Process


STATIC PUBLIC SUB Main()

clase = NEW scaniesltsp

END

PUBLIC SUB _New()

proceso = SHELL "avahi-browse _workstation._tcp -frpkl | grep \"eth0;IPv4\"" FOR INPUT

END

PRIVATE FUNCTION obtener_mac(cadena AS String[]) AS String

RETURN Right(cadena[2], 2) & ":" & Right(cadena[3], 2) & ":" & Right(cadena[4], 2) & ":" & Right(cadena[5], 2) & ":" & Right(cadena[6], 2) & ":" & Right(cadena[7], 2)

END

PRIVATE FUNCTION esportatil(servidor AS String, cliente AS String) AS String
DIM aula AS String[]
DIM caracteres AS Byte
DIM resultado AS String

aula = Split(servidor, "-")
caracteres = Len(aula[0])

IF (aula[0] = Left(cliente, caracteres)) THEN
'es cliente ligero mantenemos nombre
resultado = cliente
ELSE 'es portatil anexamos nombre clase
resultado = servidor & "--" & cliente
ENDIF

RETURN resultado

END

PRIVATE SUB publicar(aula AS String, nombre AS String, direccion AS String, mac AS String)
DIM texto AS String
DIM fichero AS String
DIM dato AS String
DIM ip AS String[]
DIM dirip AS String
DIM nombrefinal AS String

fichero = "/tmp/temporalscan"

'esto es porque a veces se meten direcciones ipv6
IF (Left(direccion, 7) = "192.168") THEN
texto = direccion & "#" & mac
ELSE
'Hay que resolverlo a mano
SHELL "avahi-resolve-host-name -4 " & nombre & ".local > " & fichero WAIT

TRY dato = File.Load(fichero)

'si el dato es vacio
IF (dato <> "") THEN
ip = Split(dato, "\t")
dirip = Left(ip[1], Len(ip[1]) - 1)
ELSE
dirip = "no-ip"
ENDIF

texto = dirip & "#" & mac

ENDIF
nombrefinal = esportatil(aula, nombre)
SHELL "avahi-publish-service -f " & nombrefinal & " _scanies._tcp 9013 " & texto & " 2>/dev/null &" WAIT

END


PUBLIC SUB borrar(nombre AS String)
DIM fichero AS String
DIM buscar AS String

fichero = "/tmp/buscar"

buscar = nombre & " _scanies._tcp"

'lo borro de la red
SHELL "ps aux | grep \"" & buscar & "\" | grep -v grep | awk '{print $2}' > " & fichero WAIT
'TRY pid = File.Load(fichero)
SHELL "kill -9 `cat " & fichero & "` 2>/dev/null" WAIT


END



PUBLIC SUB process_read()
DIM cadena AS String
DIM campos AS String[]
DIM mascampos AS String[]
DIM nombre AS String
DIM direccionip AS String
DIM mac AS String
DIM pid AS String
DIM nombreservidor AS String
DIM failedtemp AS String
DIM failedhost AS String
DIM aula AS String[]


nombreservidor = Trim(System.host)
aula = Split(nombreservidor, ".")


TRY LINE INPUT #LAST, cadena

IF NOT (Left(cadena, 6) = "Failed") THEN 'si no hay fallo se analiza

campos = Split(cadena, ";")

IF (campos[0] = "=") THEN 'esta encendido

direccionip = campos[7]
mascampos = Split(campos[3], "\\")
mac = obtener_mac(mascampos)

'publicar
publicar(aula[0], mascampos[0], direccionip, mac)


ELSE IF (campos[0] = "-") THEN 'se apaga

'fichero = "/tmp/buscar"
mascampos = Split(campos[3], "\\")

'componer nombre
nombre = esportatil(aula[0], mascampos[0])
'borrar
borrar(nombre)

ELSE 'descartamos
PRINT "Log1 (+): " & cadena
ENDIF
ELSE
'formato es: Failed to resolve service 'nombre-equipo [00:30:05:42:26:35]' of type ...
campos = Split(cadena, " ")
failedtemp = campos[4]
'obtener solo el nombre
mascampos = Split(failedtemp, " ")
failedhost = Right(mascampos[0], Len(mascampos[0]) - 1)
'y borrar de la lista
nombre = esportatil(aula[0], failedhost)
borrar(nombre)

ENDIF

END


PUBLIC SUB _free()

proceso.Kill

END