Proyecto

General

Perfil

128 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 : fprincipal.class
' # created in project : scanies
' # created by : Francisco Paniagua Sánchez
' # mail : adminies.franciscodeorellana@edu.juntaextremadura.net
' # created at : 12/11/2006
' # last test with : gambas-2.7-1
' ################################################

PRIVATE hset AS Settings
PUBLIC servidordns AS String
PUBLIC rango AS String
PUBLIC datosldap AS Boolean
PRIVATE aula AS String
PRIVATE indice AS Integer
PRIVATE nequipos AS Integer


PUBLIC SUB Form_Open()
DIM fichero AS String

fichero = user.Home & "/.scanies4"
ME.Center

IF NOT comprobar_configuracion() THEN
frmconfiguracion.ShowModal

'ELSE
' cargar_datos()
ENDIF
hset = NEW Settings(fichero)
rango = hset["Rango de Red"]
datosldap = hset["ldap"]

IF (datosldap) THEN
cargar_datos()
ELSE
deshabilitar_ldap(FALSE)
ENDIF

iniciar_programa()
END


PUBLIC SUB deshabilitar_ldap(valor AS Boolean)

frame2.enabled = valor
btnprofon.enabled = valor
btnproftodoson.Enabled = valor
cmbaulas.Enabled = valor

END



PUBLIC SUB Form_Close()


END


PUBLIC FUNCTION comprobar_configuracion() AS Boolean
DIM fichero AS String
DIM retorno AS Boolean

fichero = user.Home & "/.scanies4"

IF NOT Exist(fichero) THEN
SHELL "touch " & fichero WAIT
message.Info("La aplicación no está configurada")
retorno = FALSE
ELSE
hset = NEW Settings(fichero)
rango = hset["Rango de Red"]
datosldap = hset["ldap"]
retorno = TRUE
ENDIF
RETURN retorno

END


PUBLIC SUB iconos()

btnrefrescar.Picture = stock["16/refresh"]
btnsalir.Picture = stock["16/quit"]
mnusalir.picture = stock["16/quit"]
mnurefrescar.Picture = stock["16/refresh"]
mnuconfiguracion2.Picture = stock["16/properties"]
mnuayuda2.Picture = stock["16/help"]

END




PUBLIC SUB cargar_datos()
DIM fichero AS file
DIM fauxiliar AS File
DIM temporal AS String
DIM fprofesores AS String
DIM linea AS String
DIM nombre AS String
DIM mac AS String


'realizar un fichero mediante ldapsearch con los nombres de los equipos de profesor
temporal = "/tmp/datos-prof"
TRY SHELL "ldapsearch -xLLL -h ldap -b dc=instituto,dc=extremadura,dc=es \"(&(objectClass=dhcpHost)(cn=a*-pro))\" | egrep \"cn:|ethernet\" | awk '{ print $2 $3 }' > " & temporal WAIT

fprofesores = "/tmp/profesores-ldap"
cmbaulas.add("Elige un equipo ...")

TRY OPEN fprofesores FOR CREATE AS #fauxiliar

TRY OPEN temporal FOR READ AS #fichero
WHILE NOT Eof(fichero)

LINE INPUT #fichero, linea
nombre = linea
LINE INPUT #fichero, linea
mac = Right(linea, 17)

cmbaulas.Add(nombre)
PRINT #fauxiliar, nombre & " " & mac

WEND
CLOSE #fichero

CLOSE #fauxiliar

habilitar_botones(FALSE)


CATCH
message.Error("No se puedieron obtener los datos de los equipos de profesor")

END


PUBLIC SUB iniciar_programa()

'cargamos la lista de equipos encendidos
iconos()
inicia_lista()
btnRefrescar_click()

END


PUBLIC SUB habilitar_botones(valor AS Boolean)

btnprofon.Enabled = valor

END



PUBLIC SUB cmbaulas_Click()

IF (cmbaulas.Text = "Elige un equipo ...") THEN
habilitar_botones(FALSE)
lblaula.text = "No hay ningún equipo seleccionado"
ELSE
aula = cmbaulas.Text
habilitar_botones(TRUE)
lblaula.text = "Esta seleccionado el equipo " & aula
ENDIF

END


PUBLIC SUB habilitar(valor AS Boolean)

' frame1.Enabled = valor
frame2.Enabled = valor
listapcs.Enabled = valor
IF (valor) THEN
ME.Mouse = mouse.Default
ELSE
ME.mouse = mouse.Wait
ENDIF

END

PUBLIC SUB inicia_lista()

listapcs.Columns.Count = 4
listapcs.Columns[0].Width = 200
listapcs.Columns[1].Width = 125
listapcs.Columns[2].width = 180
listapcs.Columns[0].Text = "Nombre"
listapcs.Columns[1].Text = "Dirección IP"
listapcs.Columns[2].text = "MAC"
listapcs.Columns[3].text = " "

END


'****************************************************************************************************
'****************************************************************************************************
' opciones menu y botones
'****************************************************************************************************
'****************************************************************************************************


PUBLIC 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


PUBLIC SUB btnrefrescar_Click()
DIM fichero1 AS String
DIM fichero2 AS String
DIM fichero3 AS String
DIM ficherounion AS String
DIM fencendidos AS File
DIM linea AS String
DIM tratalinea01 AS String[]
DIM tratalinea02 AS String[]
DIM nombre AS String
DIM ip AS String
DIM mac AS String
DIM contador AS Integer
DIM tipo AS String


IF (listapcs.Exist(0)) THEN listapcs.Clear

inicia_lista()
contador = 0
habilitar(FALSE)
fichero1 = "/tmp/equipos-workstation"
fichero2 = "/tmp/equipos-scanies"


'Se crea un fichero con los pc's que están encendidos
SHELL "avahi-browse _workstation._tcp -fkprt | grep = | grep IPv4 > " & fichero1 WAIT
SHELL "avahi-browse _scanies._tcp -fkprt | grep = | grep IPv4 > " & fichero2 WAIT

'Uno los dos ficheros y los ordeno por nombre
ficherounion = "/tmp/equipos-union"
SHELL "cat " & fichero1 & " " & fichero2 & " > " & ficherounion WAIT
fichero3 = "/tmp/encendidos"
SHELL "sort -t\";\" -k 4 " & ficherounion & " > " & fichero3 WAIT


'Saco los datos de los pc's encendidos
TRY OPEN fichero3 FOR READ AS #fencendidos
WHILE NOT Eof(fencendidos)
LINE INPUT #fencendidos, linea

tratalinea01 = Split(linea, ";") 'dividimos linea en campos

'diferenciamos si es workstation o scanies
tipo = tratalinea01[4]

'workstation
IF (tipo = "_workstation._tcp") THEN
tratalinea02 = Split(tratalinea01[3], "\\") 'dividimos campo 3 que contiene el nombre y la mac"
nombre = tratalinea02[0]
mac = obtener_mac(tratalinea02)
ip = tratalinea01[7]
ENDIF

'scanies
IF (tipo = "_scanies._tcp") THEN
nombre = tratalinea01[3]
tratalinea02 = Split(tratalinea01[9], "#")
ip = Replace(tratalinea02[0], "\"", "")
mac = Replace(tratalinea02[1], "\"", "")
ENDIF

listapcs.Add(contador, nombre)
listapcs[contador][1] = ip
listapcs[contador][2] = mac

IF (tipo = "_workstation._tcp") THEN
listapcs[contador][3] = "x"
ELSE
listapcs[contador][3] = " "
ENDIF

contador = contador + 1

WEND

CLOSE #fencendidos
lblnequipos.text = "Numero de equipos encendidos: " & contador
habilitar(TRUE)

END


PUBLIC SUB mnuConfiguracion2_Click()

frmconfiguracion.Show

END

PUBLIC SUB btnsalir_Click()

ME.Close

END

PUBLIC SUB mnuRefrescar_Click()

btnrefrescar_click()

END

PUBLIC SUB mnuSalir_Click()

btnsalir_Click()

END

PUBLIC SUB mnuAyuda2_Click()

frmayuda.Show

END

PUBLIC SUB listapcs_Click()

indice = listapcs.Current.Key

END


PUBLIC SUB listapcs_Menu()
'DIM tocado AS Boolean

' TRY tocado = LAST.current.selected
' IF NOT ERROR THEN
mnupopup.Popup
' indice = LAST.item.key
' ENDIF

END

PUBLIC SUB mnuconectar_Click()
DIM direccion AS String

IF (listapcs[indice][3] = " ") THEN
message.Info("No hay conexión a esa red")
ELSE
direccion = listapcs[indice][1]
SHELL "gnome-terminal -x ssh root@" & direccion
ENDIF

END


PUBLIC SUB mnupdf_Click()
DIM fecha AS String
DIM fichero AS String
DIM ficheropdf AS String
DIM x AS Integer
DIM y AS Integer
DIM yinc1 AS Integer
DIM yinc2 AS Integer
DIM xinc1 AS Integer
DIM xinc2 AS Integer
DIM i AS Integer
DIM snombre AS String
DIM sdireccionip AS String
DIM smac AS String
DIM npagina AS Integer

'generar un fichero con los datos de los equipos

fecha = Now() 'Format(Now, "dd-mm-yyyy")
fichero = "/tmp/scanies-report.ps"
ficheropdf = User.home & "/Desktop/scanies-report.pdf"
npagina = 1
Printer.copies = 1
Printer.name = ""
Printer.size = "A4"
Printer.file = fichero

Draw.Begin(printer)
Draw.Font.name = "FreeSans"

'cabecera *****************************************************************
x = 250
y = 200

Draw.Font.size = 6
Draw.Text("Documento generado: " & fecha & " --- Página " & npagina, x + 3250, y - 100)

Draw.Font.size = 24
Draw.Font.Bold = TRUE
Draw.Text("*** ScanIES ***", x + 1500, y)
Draw.Font.size = 10

yinc1 = 200
xinc1 = 1800
xinc2 = 3700
y = y + yinc1 + yinc1

Draw.Font.size = 16
Draw.Text("Nombre", x, y)
Draw.Text("Dirección IP", x + xinc1, y)
Draw.Text("MAC", x + xinc2, y)


'cuerpo *************************************************************
y = y + yinc1
yinc2 = 100
Draw.Font.size = 10
Draw.Font.bold = FALSE

FOR i = 0 TO listapcs.Count

IF (listapcs.Exist(i)) THEN
snombre = listapcs[i][0]
sdireccionip = listapcs[i][1]
smac = listapcs[i][2]

Draw.Text(snombre, x, y)
Draw.Text(sdireccionip, x + xinc1, y)
Draw.Text(smac, x + xinc2, y)

y = y + yinc2

IF (y > 6400) THEN 'debemos comenzar una nueva página *******************************
'incluimos la cabecera y modificamos el valor de y **********************************
printer.NewPage
npagina = npagina + 1
Draw.Font.size = 6
Draw.Text("Documento generado: " & fecha & " --- Página " & npagina, x + 3250, 100)
y = 400
Draw.Font.size = 16
Draw.Font.Bold = TRUE
Draw.Text("Nombre", x, y)
Draw.Text("Dirección IP", x + xinc1, y)
Draw.Text("MAC", x + xinc2, y)
Draw.Font.size = 10
Draw.Font.bold = FALSE
y = y + yinc1

ENDIF


ENDIF
NEXT

Draw.End

'convertimos el fichero ps a pdf
SHELL "ps2pdf " & fichero & " " & ficheropdf WAIT
Message.Info("Fichero generado en el escritorio")

END


PUBLIC SUB btnnmap_Click()

frmnmap.ShowModal

END

PUBLIC SUB btnproftodoson_Click()
DIM profesores AS String
DIM temporal AS String

temporal = Temp()
profesores = "/tmp/profesores-ldap"
SHELL "awk '{ print $2 }' " & profesores & " > " & temporal WAIT
SHELL "wakeonlan -f " & temporal WAIT
message.Info("Enviada señal de encendido a todos los equipos de profesor")

END


PUBLIC SUB btnprofon_Click()
DIM profesores AS String
DIM temporal AS String
DIM mac AS String

temporal = Temp()
profesores = "/tmp/profesores-ldap"
SHELL "grep " & cmbaulas.text & " " & profesores & " | cut -f2 -d\" \" > " & temporal WAIT

TRY mac = file.Load(temporal)
SHELL "wakeonlan " & mac
message.Info("Enviada señal de encendido al equipo " & cmbaulas.text)

END