diff --git a/README.md b/README.md index fb8459e..cb0cc41 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # recurSHion +A CLI tool that lets you nest any POSIX shell commands (ssh, sudo, bash, etc). \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..33ed399 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,15 @@ +[project] +name = "recurshion" +version = "0.1.0" +description = "CLI tool for chaining POSIX shells" +readme = "README.md" +authors = [ + { name = "Adrian Jaroszewski", email = "jaroszea@sheridancollege.ca" } +] +requires-python = ">=3.13" +dependencies = [ + "pyyaml>=6.0.2", +] + +[project.scripts] +recurshion = "recurshion:main" diff --git a/recurshion.yaml.example b/recurshion.yaml.example new file mode 100644 index 0000000..10dc704 --- /dev/null +++ b/recurshion.yaml.example @@ -0,0 +1,39 @@ +guy: + trans_command: '/bin/sh -c' + command: '/bin/sh' + children: + john: + trans_command: '/bin/sh -c' + command: '/bin/sh' + children: + yet_another: + trans_command: '/bin/sh -c' + command: '/bin/sh' + children: + another: + command: '/bin/sh' +alpine3: + command: 'ssh -t root@192.168.122.117' + children: + alpine2: + command: 'ssh -t root@192.168.122.202' + children: + alpine1: + command: 'ssh -t root@192.168.122.169' + children: + proxmox_ve: + command: 'ssh -t root@192.168.122.73' + children: + lxc-100: + command: 'lxc-console 100' +alpine33: + command: 'ssh root@192.168.122.117' + children: + alpine22: + command: 'ssh root@192.168.122.202' + children: + alpine11: + command: 'ssh root@192.168.122.169' + children: + proxmox_: + command: 'ssh root@192.168.122.73' diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..bc0fff1 --- /dev/null +++ b/src/main.py @@ -0,0 +1,62 @@ +import argparse +import yaml +import pty +from resource import Resource + +def read_yaml(file_path: str): + with open(file_path, 'r') as file: + yaml_dict = yaml.safe_load(file) + return Resource.recurse_dictionary(yaml_dict) + +def main(): + parser = argparse.ArgumentParser( + prog='recurshion', + description='A CLI tool for chaining POSIX shell commands, typically ssh', + epilog='') + + subparsers = parser.add_subparsers(help='subcommands', dest='command') + subparsers.required = True + + parser_yaml = subparsers.add_parser('connect', help='reads from recurshion.yaml file in present directory.') + + #parser_yaml.add_argument('resource', choices=tree_node_list) + parser_yaml.add_argument('resource', help='name of resource to connect to (specified in yaml file)') + + parser_yaml.add_argument('-p', '--print', action='store_true', help='prints final command instead of executing') + + parser_yaml.add_argument('-f', '--file', help='manually specify yaml file') + + parser_quick = subparsers.add_parser('exec', help='takes shell commands as a series of positional arguments') + + parser_quick.add_argument('commands', nargs='+', help='shell commands to chain, each command passed a string') + + parser_quick.add_argument('-p', '--print', action='store_true', help='prints final command instead of executing') + + args = parser.parse_args() + + match args.command: + case 'connect': + if args.file: + tree_nodes, tree_root_list = read_yaml(args.file) + else: + tree_nodes, tree_root_list = read_yaml('./recurshion.yaml') + tree_node_list = list(tree_nodes.keys()) + if args.resource in tree_nodes: + if args.print == True: + command = Resource.build_command(tree_nodes[args.resource].get_command_list()) + print(command) + else: + command = Resource.build_command_list(tree_nodes[args.resource].get_command_list()) + pty.spawn(command) + else: + print('error: invalid resource name') + case 'exec': + if args.print == True: + command = Resource.build_command(args.commands) + print(command) + else: + command = Resource.build_command_list(args.commands) + pty.spawn(command) + +if __name__ == "__main__": + main() diff --git a/src/resource.py b/src/resource.py new file mode 100644 index 0000000..0b52124 --- /dev/null +++ b/src/resource.py @@ -0,0 +1,170 @@ +class Resource(): + + @staticmethod + def recurse_dictionary(yaml_dict: dict, node_dict: dict={}, tree_root_list: list=[], parent: str=None): + for key in yaml_dict: + #Checks if command and trans_command are both present. If only trans_command is specified, the resource object is initialized and treats it as command. + if 'trans_command' in yaml_dict[key] and 'command' in yaml_dict[key]: + node_dict[key] = Resource(command=yaml_dict[key]['command'], name=key, trans_command=yaml_dict[key]['trans_command']) + + elif 'trans_command' in yaml_dict[key]: + node_dict[key] = Resource(command=yaml_dict[key]['trans_command'], name=key) + + elif 'command' in yaml_dict[key]: + node_dict[key] = Resource(command=yaml_dict[key]['command'], name=key) + + else: + return None + + if 'children' in yaml_dict[key]: + Resource.recurse_dictionary(yaml_dict[key]['children'], node_dict, tree_root_list, key) + + if parent != None: + node_dict[key].parent_list = node_dict[parent] + + else: + tree_root_list.append(node_dict[key]) + + if parent == None: + return node_dict, tree_root_list + else: + return None + + + @staticmethod + def return_resource_tree(name_list: list[str]) -> str: + for i in range(len(name_list)): + pass + + # Returns command as a string + @staticmethod + def build_command(command_list: list[str]) -> str: + if len(command_list) < 2: + return ''.join(command_list) + else: + original_list_len = len(command_list) + + i = 0 + while i < len(command_list) - 1: + command_list[i] = command_list[i] + ' ' + if i == 0: + command_list.insert(1, '\'') + else: + command_list.insert(i+1, '\'\\' + '\\'.join(list(command_list[i-1])) + '\'') + i += 2 + + for j in range(i//2): + command_list.append(((2**j)-1)*'\\' + '\'') + + return ''.join(command_list) + + + @staticmethod + def build_command_list(command_list: list[str]) -> list[str]: + if len(command_list) < 2: + return command_list[0].split(' ') + else: + original_list_len = len(command_list) + new_command_list = command_list.pop(0).split(' ') + i = 0 + while i < len(command_list) - 1: + command_list[i] = command_list[i] + ' ' + if i == 0: + command_list.insert(1, '\'') + else: + command_list.insert(i+1, '\'\\' + '\\'.join(list(command_list[i-1])) + '\'') + i += 2 + + for j in range(i//2): + command_list.append(((2**j)-1)*'\\' + '\'') + + new_command_list.append(''.join(command_list)) + return new_command_list + + def get_name_list(self) -> list[str]: + if len(self.parent_list) > 0: + name_list = self.parent_list[0].get_name_list() + name_list.append(self.name) + return name_list + else: + return [self.name] + + def get_command_list(self, first: bool=True) -> list[str]: + if len(self.parent_list) > 0: + command_list = self.parent_list[0].get_command_list(first=False) + if first == False: + if hasattr(self, 'trans_command'): + command_list.append(self.trans_command) + return command_list + command_list.append(self.command) + return command_list + else: + if first == False: + if hasattr(self, 'trans_command'): + return [self.trans_command] + return [self.command] + + def link_parent(self, parent: 'Resource'): + self._parent_list.append(parent) + parent.child_list.append(self) + + def link_child(self, child: 'Resource'): + self._child_list.append(child) + child.parent_list.append(self) + + def __init__(self, command: str, name: str, trans_command: str=None, parent: 'Resource'=None, child: 'Resource'=None): + self._command = command + self._name = name + if trans_command: + self._trans_command = trans_command + self._parent_list = [] + if parent: + self.link_parent(parent) + self._child_list = [] + if child: + self.link_child(child) + + @property + def name(self) -> str: + """The name property.""" + return self._name + + @name.setter + def name(self, value: str): + self._name = value + + @property + def command(self) -> str: + """The command property.""" + return self._command + + @command.setter + def command(self, value: str): + self._command = value + + @property + def trans_command(self) -> str: + """The trans_command property.""" + return self._trans_command + + @trans_command.setter + def trans_command(self, value: str): + self._trans_command = value + + @property + def parent_list(self): + """The parent_list property.""" + return self._parent_list + + @parent_list.setter + def parent_list(self, value: 'Resource'): + self.link_parent(value) + + @property + def child_list(self): + """The child_list property.""" + return self._child_list + + @child_list.setter + def child_list(self, value: 'Resource'): + self.link_child(value) diff --git a/uv.lock b/uv.lock new file mode 100644 index 0000000..3e1cb9b --- /dev/null +++ b/uv.lock @@ -0,0 +1,50 @@ +version = 1 +revision = 3 +requires-python = ">=3.13" + +[[package]] +name = "pyyaml" +version = "6.0.3" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/05/8e/961c0007c59b8dd7729d542c61a4d537767a59645b82a0b521206e1e25c2/pyyaml-6.0.3.tar.gz", hash = "sha256:d76623373421df22fb4cf8817020cbb7ef15c725b9d5e45f17e189bfc384190f", size = 130960, upload-time = "2025-09-25T21:33:16.546Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/d1/11/0fd08f8192109f7169db964b5707a2f1e8b745d4e239b784a5a1dd80d1db/pyyaml-6.0.3-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:8da9669d359f02c0b91ccc01cac4a67f16afec0dac22c2ad09f46bee0697eba8", size = 181669, upload-time = "2025-09-25T21:32:23.673Z" }, + { url = "https://files.pythonhosted.org/packages/b1/16/95309993f1d3748cd644e02e38b75d50cbc0d9561d21f390a76242ce073f/pyyaml-6.0.3-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:2283a07e2c21a2aa78d9c4442724ec1eb15f5e42a723b99cb3d822d48f5f7ad1", size = 173252, upload-time = "2025-09-25T21:32:25.149Z" }, + { url = "https://files.pythonhosted.org/packages/50/31/b20f376d3f810b9b2371e72ef5adb33879b25edb7a6d072cb7ca0c486398/pyyaml-6.0.3-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:ee2922902c45ae8ccada2c5b501ab86c36525b883eff4255313a253a3160861c", size = 767081, upload-time = "2025-09-25T21:32:26.575Z" }, + { url = "https://files.pythonhosted.org/packages/49/1e/a55ca81e949270d5d4432fbbd19dfea5321eda7c41a849d443dc92fd1ff7/pyyaml-6.0.3-cp313-cp313-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:a33284e20b78bd4a18c8c2282d549d10bc8408a2a7ff57653c0cf0b9be0afce5", size = 841159, upload-time = "2025-09-25T21:32:27.727Z" }, + { url = "https://files.pythonhosted.org/packages/74/27/e5b8f34d02d9995b80abcef563ea1f8b56d20134d8f4e5e81733b1feceb2/pyyaml-6.0.3-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:0f29edc409a6392443abf94b9cf89ce99889a1dd5376d94316ae5145dfedd5d6", size = 801626, upload-time = "2025-09-25T21:32:28.878Z" }, + { url = "https://files.pythonhosted.org/packages/f9/11/ba845c23988798f40e52ba45f34849aa8a1f2d4af4b798588010792ebad6/pyyaml-6.0.3-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:f7057c9a337546edc7973c0d3ba84ddcdf0daa14533c2065749c9075001090e6", size = 753613, upload-time = "2025-09-25T21:32:30.178Z" }, + { url = "https://files.pythonhosted.org/packages/3d/e0/7966e1a7bfc0a45bf0a7fb6b98ea03fc9b8d84fa7f2229e9659680b69ee3/pyyaml-6.0.3-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:eda16858a3cab07b80edaf74336ece1f986ba330fdb8ee0d6c0d68fe82bc96be", size = 794115, upload-time = "2025-09-25T21:32:31.353Z" }, + { url = "https://files.pythonhosted.org/packages/de/94/980b50a6531b3019e45ddeada0626d45fa85cbe22300844a7983285bed3b/pyyaml-6.0.3-cp313-cp313-win32.whl", hash = "sha256:d0eae10f8159e8fdad514efdc92d74fd8d682c933a6dd088030f3834bc8e6b26", size = 137427, upload-time = "2025-09-25T21:32:32.58Z" }, + { url = "https://files.pythonhosted.org/packages/97/c9/39d5b874e8b28845e4ec2202b5da735d0199dbe5b8fb85f91398814a9a46/pyyaml-6.0.3-cp313-cp313-win_amd64.whl", hash = "sha256:79005a0d97d5ddabfeeea4cf676af11e647e41d81c9a7722a193022accdb6b7c", size = 154090, upload-time = "2025-09-25T21:32:33.659Z" }, + { url = "https://files.pythonhosted.org/packages/73/e8/2bdf3ca2090f68bb3d75b44da7bbc71843b19c9f2b9cb9b0f4ab7a5a4329/pyyaml-6.0.3-cp313-cp313-win_arm64.whl", hash = "sha256:5498cd1645aa724a7c71c8f378eb29ebe23da2fc0d7a08071d89469bf1d2defb", size = 140246, upload-time = "2025-09-25T21:32:34.663Z" }, + { url = "https://files.pythonhosted.org/packages/9d/8c/f4bd7f6465179953d3ac9bc44ac1a8a3e6122cf8ada906b4f96c60172d43/pyyaml-6.0.3-cp314-cp314-macosx_10_13_x86_64.whl", hash = "sha256:8d1fab6bb153a416f9aeb4b8763bc0f22a5586065f86f7664fc23339fc1c1fac", size = 181814, upload-time = "2025-09-25T21:32:35.712Z" }, + { url = "https://files.pythonhosted.org/packages/bd/9c/4d95bb87eb2063d20db7b60faa3840c1b18025517ae857371c4dd55a6b3a/pyyaml-6.0.3-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:34d5fcd24b8445fadc33f9cf348c1047101756fd760b4dacb5c3e99755703310", size = 173809, upload-time = "2025-09-25T21:32:36.789Z" }, + { url = "https://files.pythonhosted.org/packages/92/b5/47e807c2623074914e29dabd16cbbdd4bf5e9b2db9f8090fa64411fc5382/pyyaml-6.0.3-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:501a031947e3a9025ed4405a168e6ef5ae3126c59f90ce0cd6f2bfc477be31b7", size = 766454, upload-time = "2025-09-25T21:32:37.966Z" }, + { url = "https://files.pythonhosted.org/packages/02/9e/e5e9b168be58564121efb3de6859c452fccde0ab093d8438905899a3a483/pyyaml-6.0.3-cp314-cp314-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:b3bc83488de33889877a0f2543ade9f70c67d66d9ebb4ac959502e12de895788", size = 836355, upload-time = "2025-09-25T21:32:39.178Z" }, + { url = "https://files.pythonhosted.org/packages/88/f9/16491d7ed2a919954993e48aa941b200f38040928474c9e85ea9e64222c3/pyyaml-6.0.3-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:c458b6d084f9b935061bc36216e8a69a7e293a2f1e68bf956dcd9e6cbcd143f5", size = 794175, upload-time = "2025-09-25T21:32:40.865Z" }, + { url = "https://files.pythonhosted.org/packages/dd/3f/5989debef34dc6397317802b527dbbafb2b4760878a53d4166579111411e/pyyaml-6.0.3-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:7c6610def4f163542a622a73fb39f534f8c101d690126992300bf3207eab9764", size = 755228, upload-time = "2025-09-25T21:32:42.084Z" }, + { url = "https://files.pythonhosted.org/packages/d7/ce/af88a49043cd2e265be63d083fc75b27b6ed062f5f9fd6cdc223ad62f03e/pyyaml-6.0.3-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:5190d403f121660ce8d1d2c1bb2ef1bd05b5f68533fc5c2ea899bd15f4399b35", size = 789194, upload-time = "2025-09-25T21:32:43.362Z" }, + { url = "https://files.pythonhosted.org/packages/23/20/bb6982b26a40bb43951265ba29d4c246ef0ff59c9fdcdf0ed04e0687de4d/pyyaml-6.0.3-cp314-cp314-win_amd64.whl", hash = "sha256:4a2e8cebe2ff6ab7d1050ecd59c25d4c8bd7e6f400f5f82b96557ac0abafd0ac", size = 156429, upload-time = "2025-09-25T21:32:57.844Z" }, + { url = "https://files.pythonhosted.org/packages/f4/f4/a4541072bb9422c8a883ab55255f918fa378ecf083f5b85e87fc2b4eda1b/pyyaml-6.0.3-cp314-cp314-win_arm64.whl", hash = "sha256:93dda82c9c22deb0a405ea4dc5f2d0cda384168e466364dec6255b293923b2f3", size = 143912, upload-time = "2025-09-25T21:32:59.247Z" }, + { url = "https://files.pythonhosted.org/packages/7c/f9/07dd09ae774e4616edf6cda684ee78f97777bdd15847253637a6f052a62f/pyyaml-6.0.3-cp314-cp314t-macosx_10_13_x86_64.whl", hash = "sha256:02893d100e99e03eda1c8fd5c441d8c60103fd175728e23e431db1b589cf5ab3", size = 189108, upload-time = "2025-09-25T21:32:44.377Z" }, + { url = "https://files.pythonhosted.org/packages/4e/78/8d08c9fb7ce09ad8c38ad533c1191cf27f7ae1effe5bb9400a46d9437fcf/pyyaml-6.0.3-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:c1ff362665ae507275af2853520967820d9124984e0f7466736aea23d8611fba", size = 183641, upload-time = "2025-09-25T21:32:45.407Z" }, + { url = "https://files.pythonhosted.org/packages/7b/5b/3babb19104a46945cf816d047db2788bcaf8c94527a805610b0289a01c6b/pyyaml-6.0.3-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:6adc77889b628398debc7b65c073bcb99c4a0237b248cacaf3fe8a557563ef6c", size = 831901, upload-time = "2025-09-25T21:32:48.83Z" }, + { url = "https://files.pythonhosted.org/packages/8b/cc/dff0684d8dc44da4d22a13f35f073d558c268780ce3c6ba1b87055bb0b87/pyyaml-6.0.3-cp314-cp314t-manylinux2014_s390x.manylinux_2_17_s390x.manylinux_2_28_s390x.whl", hash = "sha256:a80cb027f6b349846a3bf6d73b5e95e782175e52f22108cfa17876aaeff93702", size = 861132, upload-time = "2025-09-25T21:32:50.149Z" }, + { url = "https://files.pythonhosted.org/packages/b1/5e/f77dc6b9036943e285ba76b49e118d9ea929885becb0a29ba8a7c75e29fe/pyyaml-6.0.3-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:00c4bdeba853cc34e7dd471f16b4114f4162dc03e6b7afcc2128711f0eca823c", size = 839261, upload-time = "2025-09-25T21:32:51.808Z" }, + { url = "https://files.pythonhosted.org/packages/ce/88/a9db1376aa2a228197c58b37302f284b5617f56a5d959fd1763fb1675ce6/pyyaml-6.0.3-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:66e1674c3ef6f541c35191caae2d429b967b99e02040f5ba928632d9a7f0f065", size = 805272, upload-time = "2025-09-25T21:32:52.941Z" }, + { url = "https://files.pythonhosted.org/packages/da/92/1446574745d74df0c92e6aa4a7b0b3130706a4142b2d1a5869f2eaa423c6/pyyaml-6.0.3-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:16249ee61e95f858e83976573de0f5b2893b3677ba71c9dd36b9cf8be9ac6d65", size = 829923, upload-time = "2025-09-25T21:32:54.537Z" }, + { url = "https://files.pythonhosted.org/packages/f0/7a/1c7270340330e575b92f397352af856a8c06f230aa3e76f86b39d01b416a/pyyaml-6.0.3-cp314-cp314t-win_amd64.whl", hash = "sha256:4ad1906908f2f5ae4e5a8ddfce73c320c2a1429ec52eafd27138b7f1cbe341c9", size = 174062, upload-time = "2025-09-25T21:32:55.767Z" }, + { url = "https://files.pythonhosted.org/packages/f1/12/de94a39c2ef588c7e6455cfbe7343d3b2dc9d6b6b2f40c4c6565744c873d/pyyaml-6.0.3-cp314-cp314t-win_arm64.whl", hash = "sha256:ebc55a14a21cb14062aa4162f906cd962b28e2e9ea38f9b4391244cd8de4ae0b", size = 149341, upload-time = "2025-09-25T21:32:56.828Z" }, +] + +[[package]] +name = "recurshion" +version = "0.1.0" +source = { virtual = "." } +dependencies = [ + { name = "pyyaml" }, +] + +[package.metadata] +requires-dist = [{ name = "pyyaml", specifier = ">=6.0.2" }]