-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapexcmd.py
98 lines (75 loc) · 2.45 KB
/
apexcmd.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
##
## imports
##
import argparse
import socket
import json
import logging
import yaml
##
## Code
##
def apexCmdMain():
""""Send a NetCmd to Apex"""
parser = argparse.ArgumentParser()
parser.add_argument("--rccode", "-rcc", help="Specify remote control code to perform")
parser.add_argument("--profile", "-pf", help="Specify profile to activate")
parser.add_argument("--port", "-p", help="Specify port")
parser.add_argument("--ip", "-ip", help="Specify IP address")
parser.add_argument("--secret", "-s", help="Specify secret")
parser.add_argument("--configfile", "-cf", help="Specify location of config file")
parser.add_argument("--noconfigfile", "-ncf", action='store_true', help="Specify all parameters are on command line")
args = parser.parse_args()
cfgName = 'apexcmd.yaml'
if args.noconfigfile:
cfgName = None
if args.configfile:
# user has specified a locaiton for the config file
cfgName = args.configfile
cfg = {}
if cfgName:
# read config
with open(cfgName) as file:
cfg = yaml.full_load(file)
if args.ip:
cfg['ip'] = args.ip
if args.port:
cfg['port'] = int(args.port)
if args.secret:
cfg['secret'] = args.secret
# now check we have acceptable parameters
if not cfg.get('ip'):
print('Must supply an IP address')
return
if not cfg.get('port'):
print('Must supply a Port')
return
if not cfg.get('secret'):
print('Must specify a secret')
return
if (not args.rccode) and (not args.profile):
print('Need to specify something to send')
return
#print('Creating socket...')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
parms = (cfg['ip'], cfg['port'] )
# print(parms)
sock.connect( parms )
if args.rccode:
msg = {}
msg['secret'] = cfg['secret']
msg['rccode'] = args.rccode
js = json.dumps(msg)
#print(js)
sock.sendall(bytes(js,'utf-8'))
print(f"Apex rccode {msg['rccode']} sent")
if args.profile:
msg = {}
msg['secret'] = cfg['secret']
msg['profile'] = args.profile
js = json.dumps(msg)
#print(js)
sock.sendall(bytes(js,'utf-8'))
print(f"Apex profile {msg['profile']} sent")
if __name__ == "__main__":
apexCmdMain()