-
Notifications
You must be signed in to change notification settings - Fork 21
/
command.py
91 lines (83 loc) · 2.67 KB
/
command.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
#!python3
###demo code provided by Steve Cope at www.steves-internet-guide.com
##email [email protected]
###Free to use for any purpose
import sys, getopt
options=dict()
##EDIT HERE ###############
options["username"]=""
options["password"]=""
options["broker"]="192.168.1.37"
options["port"]=1883
options["verbose"]=True
options["cname"]=""
options["topics"]=[("#",0)]
options["storechangesonly"]=True
options["keepalive"]=60
options["loglevel"]="WARNING"
options["log_dir"]="mlogs"
options["log_records"]=5000
options["number_logs"]=0
options["JSON"]=False
options["CSV"]=False
options["header_file"]="data.csv"
options["fname"]="data.csv"
options["header_flag"]=False
def command_input(options={}):
topics_in=[]
qos_in=[]
valid_options=" --help <help> -h or -b <broker> -p <port>-t <topic> -q QOS -v <verbose> -h <help>\
-d logging debug -n Client ID or Name -u Username -P Password -s <store all data>\
-l <log directory default= mlogs> -r <number of records default=100>\
-f <number of log files default= unlimited> -j <log in json format> -c <log in csvformat>"
print_options_flag=False
try:
opts, args = getopt.getopt(sys.argv[1:],"h:b:cjsdk:p:t:q:l:vn:u:P:l:r:f:")
except getopt.GetoptError:
print (sys.argv[0],valid_options)
sys.exit(2)
qos=0
for opt, arg in opts:
if opt == '-h':
options["broker"] = str(arg)
elif opt == "-b":
options["broker"] = str(arg)
elif opt == "-k":
options["keepalive"] = int(arg)
elif opt =="-p":
options["port"] = int(arg)
elif opt =="-t":
topics_in.append(arg)
elif opt =="-q":
qos_in.append(int(arg))
elif opt =="-n":
options["cname"]=arg
elif opt =="-d":
options["loglevel"]="DEBUG"
elif opt == "-P":
options["password"] = str(arg)
elif opt == "-u":
options["username"] = str(arg)
elif opt =="-v":
options["verbose"]=True
elif opt =="-s":
options["storechangesonly"]=False
elif opt =="-l":
options["log_dir"]=str(arg)
elif opt =="-r":
options["log_records"]=int(arg)
elif opt =="-f":
options["number_logs"]=int(arg)
elif opt =="-j":
options["JSON"]=True
elif opt =="-c":
options["CSV"]=True
lqos=len(qos_in)
for i in range(len(topics_in)):
if lqos >i:
topics_in[i]=(topics_in[i],int(qos_in[i]))
else:
topics_in[i]=(topics_in[i],0)
if topics_in:
options["topics"]=topics_in #array with qos
return options