TiledViz
Loading...
Searching...
No Matches
TileClient.py
1from __future__ import with_statement
2import select
3import sys,os,re,time
4import threading
5import datetime
6
7import subprocess as subp
8
9sys.path[0]=''
10sys.path.append(os.path.abspath('/home/myuser/CASE'))
11sys.path.append(os.path.abspath('/TiledViz/TVConnections'))
12from connect import sock
13
14PORT = 6501
15if (len(sys.argv) > 2):
16 try:
17 PORT=int(sys.argv[2])
18 except Exception as err:
19 print("Wrong type of argument for PORT in TileClient for ID %d : %s : " % (sys.argv[1],sys.argv[2]))
20
21SELECT_SEC=1
22
23client=sock.client(PORT)
24
25# sys.stdout.flush()
26# search for last string of launch (wait 1s after each test)
27Home=os.environ["HOME"]
28VNCpath=os.path.join(Home,".vnc")
29LogFile=os.path.join(VNCpath,"log")
30OKLaunchStr="Log file is"
31StrPass='Random Password Generated: '
32
33with open(LogFile) as LogFileDesc:
34 for cnt, line in enumerate(LogFileDesc):
35 if ( re.search(r''+StrPass,line) ):
36 # Get vnc password in LogFile and send it to Connection
37 p=re.compile(r''+StrPass+"(\w*)")
38 PasswdStr=p.sub(r'\1',line.rstrip())
39 print("Passwd =",PasswdStr)
40 sys.stdout.flush()
41 PasswdMSG=sys.argv[1]+":"+PasswdStr
42 client.send_server(PasswdMSG)
43
44 CommandIp="/sbin/ip addr show dev eth0"
45 p=subp.Popen(CommandIp, shell=True,stdout=subp.PIPE,stderr=subp.PIPE)
46 output, errors = p.communicate()
47 o=output.decode('utf-8')
48 OutIp=re.sub(r'.*inet ([^/]*)/24 .*',r'\1',o.replace('\n',''))
49
50 elif ( re.search(r''+OKLaunchStr,line.rstrip()) ):
51 # Get OKLaunchStr in LogFile
52 print("Find end of startup.")
53 sys.stdout.flush()
54
55#=> communication to server
56print("Wait for commands.")
57sys.stdout.flush()
58
59while True:
60 data = client.recv()
61 if not data: break
62 CommandRecv=data
63 print("Receive data : "+CommandRecv)
64 if (re.search(r'execute',CommandRecv)):
65 CommandData=CommandRecv.replace("execute ","")
66 print("Execute command : "+CommandData)
67 #Log outputs of commands :
68 # fsocat.append(open("pid_dist","w"))
69 # socatSERV.append(subp.Popen( (strsocat.split(' ')), stdout=fsocat[i]))
70 # time.sleep(2)
71 # fsocat[i].flush()
72
73 # f3141=open("pid_dist","r")
74 # pidsocat.append(f3141.readline().replace('\n',''))
75 # f3141.close()
76 # print "socat : ",strsocat,pidsocat[i]
77
78 try:
79 #DATE=re.sub(r'\..*','',datetime.datetime.isoformat(datetime.datetime.now(),sep='_').replace(":","-"))
80 #process=subp.Popen( CommandData, shell=True, stdout=subp.PIPE,stderr=subp.PIPE )
81 # output, errors = process.communicate()
82 # print(output.decode('utf-8'))
83 # if (len(errors) >0):
84 # print(errors.decode('utf-8'))
85 #outprocess=process.returncode
86
87 process=os.system( CommandData )
88 outprocess=os.WEXITSTATUS(process)
89 print("outprocess = "+str(outprocess))
90
91 client.send_OK(outprocess)
92
93 except Exception as err:
94 print("Error on execution : "+str(err))
95 client.send_OK(-1)
96 else:
97 print("Get unknown command : "+CommandRecv)
98
99client.close()