# Copyright (c) 2025 Kim Jarvis TPF Software Services S.A. kim.jarvis@tpfsystems.com # This software is licensed under the MIT License. See the LICENSE file for details.#
[docs]classScript:""" A class to encapsulate the functionality of shell scripts in Unix-like operating systems. It allows users to specify a shell script that is executed on all hosts. additional command-line options, and the ability to execute the command with elevated privileges (`sudo`). Attributes: text (str): The shell script. guard (bool): If `False` the commands will not be executed. sudo (bool): If `True`, the commands will be executed with `sudo` privileges. su (bool): If `True`, the commands will be executed with `su` privileges. **Examples:** .. code:: python from reemote.operations.server.script import Script # Execute a shell command on all hosts yield Script(text="echo Hello World") # The result is available in stdout print(r.cp.stdout) Usage: This class is designed to be used in a generator-based workflow where commands are yielded for execution. Notes: - Commands are constructed based on the `sudo`, and `su` flags. """def__init__(self,user:str=None,password:str=None):self.user=userself.password=passworddef__init__(self,text:str=None,guard:bool=True,sudo:bool=False,su:bool=False):self.text=textself.guard=guardself.sudo=sudoself.su=sudef__repr__(self):return(f"Script("f"text={self.text!r}, "f"guard={self.guard!r}, "f"sudo={self.sudo!r}, su={self.su!r})")defexecute(self):fromreemote.operations.server.shellimportShellfromreemote.operations.sftp.write_fileimportWrite_filefromreemote.operations.sftp.chmodimportChmodfromreemote.operations.sftp.removeimportRemoveyieldRemove(path='/tmp/script.sh',)yieldWrite_file(path='/tmp/script',text=f'{self.txt}')yieldChmod(path='/tmp/script.sh',mode=0o755,)yieldShell("bash /tmp/script.sh",su=self.su)