/
chat.py
116 lines (77 loc) · 3.64 KB
/
chat.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from socket import *
from subprocess import call
import re
from database import Client as Client
from database import Software as Software
def main():
#Get database session and engine
engine = database.get_engine()
session = database.get_session()
HOST = '192.168.1.33' #The server IP
PORT = 9000 #PORT
s = socket(AF_INET, SOCK_STREAM)# 98% of all socket programming will use AF_INET and SOCK_STREAM
s.bind((HOST, PORT))
s.listen(1) # how many connections it can receive at one time
conn, addr = s.accept() # accept the connection
print (str(conn))
print str(addr)
print 'Connected by', addr # print the address of the person connected
while True:
data = conn.recv(1024) #recives datae using conn and store into data print "Received ",
if (repr(data) == "''"):
conn, addr = s.accept()
else:
print repr(data) # print data; Data is the message the users types `
#Takes a screenshot from the mac.
#Need to change parameters as ip, users and libraries
if (repr(data)=="'mac'"):
call(['sshpass','-p','1qaz','ssh','lsxliron@192.168.1.14','/usr/local/bin/imagesnap','/Users/lsxliron/Desktop/temp.jpeg'])
call(['sshpass','-p','1qaz','scp','lsxliron@192.168.1.14:/Users/lsxliron/Desktop/temp.jpeg','/home/lsxliron/Desktop/temp.jpeg'])
call (['sshpass','-p','1qaz','ssh','lsxliron@192.168.1.14','rm','/Users/lsxliron/Desktop/temp.jpeg'])
call(["python", "/home/lsxliron/Desktop/sendEmail.py"])
#reply = raw_input("Reply: ")
#conn.sendall(reply)
user_msg = repr(data).split(' ')
if (user_msg[0] == 'Matlab'): #Case user uses matlab
#get client ip, username and password
client_ip = ''
client_pass = ''
client_username = ''
client_name = user_msg[1]
for client in session.query(Client).filter(Client.name == client_name):
client_ip = client.IP
client_pass = client.password
client_username = client.username
#case that client doesnt exists
if (client_ip == None or client_pass == None or client_username == None):
conn.sendall ('Client does not exists in the system.')
else:
conn.sendall("SO FAR SO GOOD")
#Matlab Methods
if (repr(data)== "'Matlab'"):
conn.sendall("Please enter the method name and arguments foo(a,b)\n")
data=conn.recv(1024)
func=re.findall(r'\'.*\'',repr(data))
if (len(data) > 0):
func = func[0]
func = func[1:-1]
print func + " PRINTED BY ME"
eval(func)
conn.sendall("Simulation successful, the published document was sent to your email.")
#if (repr(data)=="''"):
# conn, addr = s.accept()
if (repr(data) == "'test'"):
print "!!!"
testMethod(1,2)
if (repr(data) == "'ssh'"):
print "!!!!"
sshtest(1,2)
def sshtest(a, b):
call (['sshpass','-p','1qaz','ssh','lsxliron@192.168.1.32','/Applications/MATLAB_R2012a.app/bin/matlab','-noawt', '-nodesktop','-nosplash','-r', "\"publish('test.m',struct('codeToEvaluate','test("+str(a)+","+str(b)+")','showCode',true,'outputDir','/Users/lsxliron/Desktop','format','pdf')),exit\""])
call (['sshpass','-p','1qaz','scp','lsxliron@192.168.1.32:/Users/lsxliron/Desktop/test.pdf','/home/lsxliron/Desktop/temp.pdf'])
call (['sshpass','-p','1qaz','ssh','lsxliron@192.168.1.32','rm','/Users/lsxliron/Desktop/test.pdf'])
call (['python',"/home/lsxliron/Desktop/1.py"])
def testMethod(a, b):
call (['/Applications/MATLAB_R2012a.app/bin/matlab','-noawt', '-nodesktop','-nosplash','-r', "publish('test.m',struct('codeToEvaluate','test(1,2)','showCode',true,'outputDir','/Users/lsxliron/Desktop','format','pdf')),exit"])
if __name__ == '__main__':
main()