sus/susmng.py

59 lines
1.5 KiB
Python
Raw Normal View History

2022-05-26 01:08:50 +00:00
#!/usr/bin/env python3
import sys
import requests
import hmac
import hashlib
import pathlib
import os
2022-05-26 01:08:50 +00:00
def get_args():
""" Get command line arguments """
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('susserver')
parser.add_argument('command')
parser.add_argument('shortlink')
parser.add_argument('value')
parser.add_argument('--secret-file', type=pathlib.Path, default=pathlib.Path(os.path.expanduser('~/.susmng_secret')))
2022-05-26 01:08:50 +00:00
parser.add_argument('--http', action='store_true')
return parser.parse_args()
def main(args):
""" Entry point for script """
if not args.secret_file.exists():
print(f"secret file does not exist at: {args.secret_file}")
return
with open(args.secret_file) as fp:
secret = fp.read().strip()
2022-05-26 01:08:50 +00:00
r = requests.post(f"{'http' if args.http else 'https'}://{args.susserver}",
data = {
'Command': args.command,
'Shortlink': args.shortlink,
'Value': args.value,
},
headers = {
'Signature': 'SUS-SIGNATURE-' + hmac.new(
secret.encode("UTF-8"),
2022-05-26 01:08:50 +00:00
(args.command+":"+args.shortlink+":"+args.value).encode("UTF-8"),
hashlib.sha256
).hexdigest()
}
)
print(r)
print(r.content)
return 0
if __name__ == '__main__':
try:
sys.exit(main(get_args()))
except KeyboardInterrupt:
sys.exit(0)