-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagente-cliente.py
executable file
·88 lines (82 loc) · 3.04 KB
/
agente-cliente.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
# ------------------------------
# importacion
# ------------------------------
import socket, os, time, ConfigParser
def poner_mensaje( tipo , mensaje ):
# -----------------------------
# Colocar mensajes con formato
# y marca de tiempo
# -----------------------------
print time.strftime('%Y-%m-%d-%X') + " " + tipo + ": " + mensaje
def ejecutar_comando( comando ):
# -----------------------------
# Ejecutar el comando en el
# sistema operativo
# -----------------------------
os.system( comando + ' 2> /dev/null > /dev/null &' )
def activar_configuracion():
# ------------------------------
# Variables del servicio desde
# un archivo de configuracion
# ------------------------------
configuracion = "./configuracion/agente-cliente.cfg"
global direccion
global puerto
global clave
try:
cfg = ConfigParser.ConfigParser()
cfg.read([configuracion])
direccion = cfg.get('cliente','ipcliente')
puerto = int(cfg.get('cliente','puerto'))
clave = cfg.get('cliente','clave')
except:
poner_mensaje( 'ERROR' , "No se pudo leer el archivo de configuracion " + configuracion )
poner_mensaje( 'AVISO' , "Se tomaran los valores por omision: 127.0.0.1 6470 root" )
direccion = '127.0.0.1'
puerto = 6470
clave = 'root'
# ------------------------------
# iniciacion del agente cliente
# ------------------------------
if __name__ == "__main__":
activar_configuracion()
agente = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
try:
agente.bind( ( direccion, puerto ) )
agente.listen( 1 )
seguir = True
except:
poner_mensaje( 'ERROR' , "No se pudo iniciar el agente cliente" )
seguir = False
# ------------------------------
# Bucle infinito para atender clientes
# ------------------------------
while seguir:
canal, detalles = agente.accept( )
ipremota = str( detalles )
ipremota = ipremota[ ipremota.find("(") + 2 : ipremota.find(",") - 1 ]
poner_mensaje( 'AVISO' , 'Se ha recibido una conexion ' + ipremota )
canal.send( 'Hola ' + ipremota + ' !' )
peticion = canal.recv(1000)
if ( clave == peticion):
poner_mensaje( 'AVISO' , "El agente servidor se identifico correctamente" )
canal.send( 'Mucho gusto! Que desea?' )
peticion = canal.recv(1000)
if ( "hola" == peticion ):
poner_mensaje( 'AVISO' , "El agente servidor solicito terminar el agente" )
canal.send( 'Agente cliente terminando... Nos vemos!' )
seguir = False
elif ( "estado" == peticion ):
poner_mensaje( 'AVISO' , "El agente servidor solicito corroborar el estado, y le dije que estaba vivo..." )
canal.send( 'Estoy vivo ' + direccion + ' !' )
else:
poner_mensaje( 'AVISO' , "El agente servidor solicito la ejecucion de: " + peticion )
ejecutar_comando( peticion )
canal.send( 'Comando: <' + peticion + '> ejecutado!' )
else:
poner_mensaje( 'ERROR' , "El agente servidor no se identifico correctamente" )
canal.send( 'No se quien es usted...' )
poner_mensaje( 'ERROR' , "Puede ser un intento de ataque o una mala configuracion en el servidor" )
canal.send( '... Adios !' )
canal.close( )