1 | import json |
---|
2 | import os |
---|
3 | |
---|
4 | |
---|
5 | class LmdClientManager: |
---|
6 | |
---|
7 | |
---|
8 | def __init__(self): |
---|
9 | self.clientpath="/etc/ltsp/bootopts/clients/" |
---|
10 | |
---|
11 | pass |
---|
12 | #def __init__ |
---|
13 | |
---|
14 | def getClientList(self): |
---|
15 | ''' |
---|
16 | Reads the file list of clients from /etc/ltsp/bootopts/clients |
---|
17 | Returna a JSON List. |
---|
18 | ''' |
---|
19 | |
---|
20 | clientlist=[] |
---|
21 | |
---|
22 | for i in os.listdir(self.clientpath): |
---|
23 | if '.json' in i: |
---|
24 | clientlist.append(str(i)) |
---|
25 | |
---|
26 | return json.dumps(clientlist) |
---|
27 | |
---|
28 | |
---|
29 | |
---|
30 | def getClient(self, client): |
---|
31 | ''' |
---|
32 | Returns the metadata from certain client |
---|
33 | ''' |
---|
34 | try: |
---|
35 | json_data=open(self.clientpath+client) |
---|
36 | data = json.load(json_data) |
---|
37 | json_data.close() |
---|
38 | return json.dumps(data) |
---|
39 | #return data; |
---|
40 | except Exception as e: |
---|
41 | return {"status":False}; |
---|
42 | |
---|
43 | |
---|
44 | |
---|
45 | def setClient(self, client, data): |
---|
46 | ''' |
---|
47 | Saves metadata from *data to client |
---|
48 | data is unicoded string |
---|
49 | client is a mac |
---|
50 | ''' |
---|
51 | client=client.replace(":", "") |
---|
52 | |
---|
53 | path_to_write = os.path.join(self.clientpath,client + ".json") |
---|
54 | f = open(path_to_write,'w') |
---|
55 | f.writelines(data) |
---|
56 | f.close() |
---|
57 | |
---|
58 | |
---|
59 | def deleteClient(self, client): |
---|
60 | ''' |
---|
61 | N4d Method to delete a client |
---|
62 | ''' |
---|
63 | import shutil; |
---|
64 | |
---|
65 | try: |
---|
66 | client=client.replace(":", "") |
---|
67 | json_file = os.path.join("/etc/ltsp/bootopts/clients",client + ".json") |
---|
68 | |
---|
69 | # Remove .json file |
---|
70 | if (os.path.isfile(json_file)): |
---|
71 | os.remove(json_file); |
---|
72 | |
---|
73 | return {"status":True, "msg":"Client Removed"} |
---|
74 | except Exception as e: |
---|
75 | return {"status":False, "msg":str(e)} |
---|
76 | |
---|
77 | def getArpTable(self): |
---|
78 | |
---|
79 | f=open("/proc/net/arp") |
---|
80 | lines=f.readlines() |
---|
81 | f.close() |
---|
82 | |
---|
83 | arptable=[]; |
---|
84 | |
---|
85 | iface=objects["VariablesManager"].get_variable("INTERNAL_INTERFACE") |
---|
86 | |
---|
87 | for line in lines: |
---|
88 | macarray=re.sub(' +',' ',line).split(" "); |
---|
89 | ip=macarray[0] |
---|
90 | mac=macarray[3] |
---|
91 | if (macarray[5].replace('\n', '')==iface): |
---|
92 | print "adding" |
---|
93 | arptable.append({"ip":ip, "mac":mac}); |
---|
94 | |
---|
95 | return arptable[0:]; |
---|