From 1931445c13bd7a87bc1d35bd5a8a950a3d72794f Mon Sep 17 00:00:00 2001 From: gitea Date: Thu, 9 Nov 2023 19:44:45 +0800 Subject: [PATCH] [add] rewrite utool with enclosure function --- utool/utool.py | 128 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100755 utool/utool.py diff --git a/utool/utool.py b/utool/utool.py new file mode 100755 index 0000000..75255b2 --- /dev/null +++ b/utool/utool.py @@ -0,0 +1,128 @@ +#!/usr/bin/python3 +# coding: utf-8 +import os +import sys +import time + +def utool(option): + + def print_usage(): + intro = """utool -- a self-defined command line interface, which is used to facilitate operating the system, supports the following options. In the description part, where there is a leading asterisk signifies that this option must take an argument, and for more information of this situation, simply run `utool -h` for details.""" + print(f"{c_intro}{intro}{c_e}") + print("_" * 121) + print(f"{c_title}|Option| Description{' ' * 40}|Option| Description{' ' * 40}|{c_e}") + count = 0 + for opt, desc in dict_func.items(): + description = "" if desc[0] == "Reserved" else desc[0] + if count % 2 == 0: + print(f"{c_text}|{opt:^6}|{description:<52}{c_e}", end="") + else: + print(f"{c_text}|{opt:^6}|{description:<52}|{c_e}") + count += 1 + exit(100) + + # -a + def list_local_using_port(): + exit_on_wrong_number() + os.system('/usr/bin/bash /opt/scripts/utool/ipports.sh port') + + # -b + def github_two_factor(): + exit_on_wrong_number() + os.system("/usr/bin/oathtool -b --totp 'G3NHHFO2L2LZ5W2R'") + + # -c + def get_external_ip(): + exit_on_wrong_number() + os.system("/usr/bin/python3 /opt/scripts/roll_api/get_self_ip.py") + + # -d + def get_ip_location(): + exit_on_wrong_number(2) + import re + checked_ip = sys.argv[2] + ipv4 = re.match(r"^((([01]?\d\d?)|(2[0-4]\d)|(25[0-5]))\.){3}(([01]?\d\d?)|(2[0-4]\d)|(25[0-5]))$", checked_ip) + if ipv4: + os.environ['checked_ip'] = checked_ip + os.system('/usr/bin/python3 /opt/scripts/roll_api/get_ip.py $checked_ip') + else: + print(f"{c_br}Plz enter correct IP...{c_e}") + exit(ord(option.strip('-'))) + + + def exit_on_wrong_number(number=1): + if len(sys.argv) != (number+1): + print(f"{c_br}{sys.argv[1:]}: Wrong arguments!") + print(f"Option `{option}' will {dict_func[option][0].strip()}") + print(f"NEED: {number}\tGIVE: {len(sys.argv)-1}\n{c_e}") + exit(ord(option.strip('-'))) + + dict_func = { + '-a': [' print all local ports of using for now', list_local_using_port], + '-n': ['Reserved', None], + '-b': [' generate two-factor key of GITHUB', github_two_factor], + '-o': ['Reserved', None], + '-c': [' show external IP of this machine', get_external_ip], + '-p': ['Reserved', None], + '-d': [' show the location of ip(only v4 for now)', get_ip_location], + '-q': ['Reserved', None], + '-e': ['Reserved', None], + '-r': ['Reserved', None], + '-f': ['Reserved', None], + '-s': ['Reserved', None], + '-g': ['Reserved', None], + '-t': ['Reserved', None], + '-h': ['Reserved', print_usage], + '-u': ['Reserved', None], + '-i': ['Reserved', None], + '-v': ['Reserved', None], + '-j': ['Reserved', None], + '-w': ['Reserved', None], + '-k': ['Reserved', None], + '-x': ['Reserved', None], + '-l': ['Reserved', None], + '-y': ['Reserved', None], + '-m': ['Reserved', None], + '-z': ['Reserved', None], + } + return dict_func[option][1] + + + +def main(): + + global option + options = [ '-' + chr(x) for x in range(ord('a'), ord('z')+1)] + print_usage = utool('-h') + + if len(sys.argv) > 1: + option = sys.argv[1] + if option in options: + func = utool(option) + if func == None: + print(f"{c_bg}Option `{option}' is reserved for now...{c_e}\n") + else: + func() + else: + print_usage() + else: + print_usage() + +c_title = '\033[1;4;37;40m' # title color +c_text = '\033[37;40m' # text color +c_intro = '\033[3;32m' # text color +c_br = '\033[1;31m' # bold red +c_bg = '\033[1;32m' # bold green +c_by = '\033[1;33m' # bold yellow +c_bb = '\033[1;34m' # bold blue +c_bp = '\033[1;35m' # bold purple +c_bc = '\033[1;36m' # bold cyan +c_bir= '\033[1;3;31m' # * bold italic red +c_bib = '\033[1;3;34m' # * bold italic cyan +c_bic = '\033[1;3;36m' # bold italic cyan +c_e = '\033[0m' # reset + +if __name__ == '__main__': + main() + +