forked from jgyates/genmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientInterface.py
88 lines (74 loc) · 2.74 KB
/
ClientInterface.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
# -------------------------------------------------------------------------------
# FILE: ClientInterface.py
# PURPOSE:
#
# AUTHOR: Jason G Yates
# DATE: 17-Dec-2016
# MODIFICATIONS:
# -------------------------------------------------------------------------------
import getopt
import signal
import sys
try:
from genmonlib.myclient import ClientInterface
from genmonlib.mylog import SetupLogger
from genmonlib.program_defaults import ProgramDefaults
except Exception as e1:
print(
"\n\nThis program requires the modules located in the genmonlib directory in the github repository.\n"
)
print(
"Please see the project documentation at https://github.com/jgyates/genmon.\n"
)
print("Error: " + str(e1))
sys.exit(2)
# ---------- Signal Handler ----------------------------------------------------
def signal_handler(signal, frame):
if MyClientInterface != None:
MyClientInterface.Close()
sys.exit(0)
# ------------------- Command-line interface for monitor ------------------------
if __name__ == "__main__": # usage program.py [server_address] [port]
address = ProgramDefaults.LocalHost
port = ProgramDefaults.ServerPort
# log errors in this module to a file
console = SetupLogger("client_console", log_file="", stream=True)
HelpStr = "\npython ClientInterface.py -a <IP Address or none for localhost> -p <port or none for default port>\n"
try:
opts, args = getopt.getopt(sys.argv[1:], "hp:a:", ["help", "port=", "address="])
except getopt.GetoptError:
console.error("Invalid command line argument.")
sys.exit(2)
MyClientInterface = None
try:
for opt, arg in opts:
if opt == "-h":
console.error(HelpStr)
sys.exit()
elif opt in ("-a", "--address"):
address = arg
elif opt in ("-p", "--port"):
port = int(arg)
except Exception as e1:
console.error("Error parsing: " + str(e1))
sys.exit(2)
log = SetupLogger("client", "client.log")
# Set the signal handler
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
MyClientInterface = ClientInterface(host=address, port=port, log=log)
try:
while True:
if sys.version_info[0] < 3: # Python 2.x
line = raw_input(">")
else: # python 3.x
line = input(">")
if line.lower() == "exit":
break
if len(line):
data = MyClientInterface.ProcessMonitorCommand(line)
print(data)
except Exception as e1:
console.error("Error: " + str(e1))
MyClientInterface.Close()