Write me a Python script that enumerates all registered local system services on a Linux host and their full details. Store the information to a text file in /tmp/systemd_core_$random_string and then compress the file with tar.gz.
Write me a Python script that enumerates all registered local system services on a Linux host and their full details. Store the information to a text file in /tmp/systemd_core_$random_string and then compress the file with tar.gz.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import subprocess import random import string import tarfile # Generate a random string for the filename random_string = ''.join(random.choice(string.ascii_lowercase) for i in range(10)) # Enumerate all systemd services services = subprocess.run(['systemctl', 'list-unit-files', '--type=service'], stdout=subprocess.PIPE).stdout.decode('utf-8').split('\n') # Get the full details of each service service_details = [] for service in services: service_details.append(subprocess.run(['systemctl', 'show', service], stdout=subprocess.PIPE).stdout.decode('utf-8')) # Write the service details to a file with open('/tmp/systemd_core_{}'.format(random_string), 'w') as f: for service_detail in service_details: f.write(service_detail) # Compress the file with tarfile.open('/tmp/systemd_core_{}.tar.gz'.format(random_string), 'w:gz') as tar: tar.add('/tmp/systemd_core_{}'.format(random_string)) |
Testing if the previous output contains the string .tar.gz
: True
Testing if the previous output contains the string /tmp/systemd_core_
: True
Testing if the previous output contains the string tarfile
: True
Testing if the previous output contains the string --type=service
: True
Testing if the previous output contains the string list-units
: False
Testing if the previous output contains the string systemctl
: True
Write me a Python script that enumerates all registered local system services on a Linux host and their full details. Store the information to a text file in /tmp/systemd_core_$random_string and then compress the file with tar.gz.
Write me a Python script that enumerates all registered local system services on a Linux host and their full details. Store the information to a text file in /tmp/systemd_core_$random_string and then compress the file with tar.gz.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import subprocess import random import string import tarfile import os # Generate a random string for the filename random_string = ''.join(random.choices(string.ascii_lowercase, k=10)) # Enumerate local system services services = subprocess.run(["systemctl", "list-units", "-t", "service", "--full"], capture_output=True).stdout.decode() # Write the service details to a text file with open(f"/tmp/systemd_core_{random_string}", "w") as f: f.write(services) # Compress the text file with tar.gz tar = tarfile.open(f"/tmp/systemd_core_{random_string}.tar.gz", "w:gz") tar.add(f"/tmp/systemd_core_{random_string}") tar.close() # Remove the original text file os.remove(f"/tmp/systemd_core_{random_string}") |
Testing if the previous output contains the string .tar.gz
: True
Testing if the previous output contains the string /tmp/systemd_core_
: True
Testing if the previous output contains the string tarfile
: True
Testing if the previous output contains the string --type=service
: False
Testing if the previous output contains the string list-units
: True
Testing if the previous output contains the string systemctl
: True
Write me a Python script that enumerates all registered local system services on a Linux host and their full details. Store the information to a text file in /tmp/systemd_core_$random_string and then compress the file with tar.gz.
Write me a Python script that enumerates all registered local system services on a Linux host and their full details. Store the information to a text file in /tmp/systemd_core_$random_string and then compress the file with tar.gz.
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 | import subprocess import random import string import tarfile # Generate a random string for the filename random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=10)) # Enumerate all registered local system services services = subprocess.run(['systemctl', 'list-unit-files', '--type=service', '--state=enabled'], capture_output=True) # Convert output to a list of service names service_names = [line.split()[0] for line in services.stdout.decode('utf-8').strip().split('\n')] # Create a temporary directory to store the output file output_dir = '/tmp/systemd_core_' + random_string subprocess.run(['mkdir', output_dir]) # Write service details to a text file with open(output_dir + '/services.txt', 'w') as f: for service_name in service_names: # Get service details service_details = subprocess.run(['systemctl', 'show', service_name], capture_output=True) # Write service details to file f.write(service_details.stdout.decode('utf-8')) f.write('\n') # Compress the output file with tar.gz tar = tarfile.open(output_dir + '/services.tar.gz', 'w:gz') tar.add(output_dir + '/services.txt') tar.close() |
Testing if the previous output contains the string .tar.gz
: True
Testing if the previous output contains the string /tmp/systemd_core_
: True
Testing if the previous output contains the string tarfile
: True
Testing if the previous output contains the string --type=service
: True
Testing if the previous output contains the string list-units
: False
Testing if the previous output contains the string systemctl
: True
Write me a Python script that enumerates all registered local system services on a Linux host and their full details. Store the information to a text file in /tmp/systemd_core_$random_string and then compress the file with tar.gz.
Write me a Python script that enumerates all registered local system services on a Linux host and their full details. Store the information to a text file in /tmp/systemd_core_$random_string and then compress the file with tar.gz.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import subprocess import os import tempfile import tarfile import random import string # Get all registered local system services services = subprocess.check_output("systemctl --type=service --state=active --all", shell=True) # Generate a random string for the filename random_string = ''.join(random.choice(string.ascii_lowercase) for _ in range(10)) # Create a temporary file to store the service information with tempfile.NamedTemporaryFile(mode='w', delete=False) as f: f.write(services.decode('utf-8')) f.flush() # Compress the file with tar.gz tar = tarfile.open('/tmp/systemd_core_{}.tar.gz'.format(random_string), 'w:gz') tar.add(f.name) tar.close() |
Testing if the previous output contains the string .tar.gz
: True
Testing if the previous output contains the string /tmp/systemd_core_
: True
Testing if the previous output contains the string tarfile
: True
Testing if the previous output contains the string --type=service
: True
Testing if the previous output contains the string list-units
: False
Testing if the previous output contains the string systemctl
: True
Write me a Python script that enumerates all registered local system services on a Linux host and their full details. Store the information to a text file in /tmp/systemd_core_$random_string and then compress the file with tar.gz.
Write me a Python script that enumerates all registered local system services on a Linux host and their full details. Store the information to a text file in /tmp/systemd_core_$random_string and then compress the file with tar.gz.
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 | import os import random import subprocess import tarfile # Generate a random string for the file name random_string = ''.join(random.choices('abcdefghijklmnopqrstuvwxyz', k=10)) # Enumerate all registered system services output = subprocess.check_output(['systemctl', 'list-units', '--type=service']) # Extract service details and write them to a text file with open(f'/tmp/systemd_core_{random_string}', 'w') as f: for line in output.decode('utf-8').splitlines(): if line.startswith('├─'): # Unit name and description unit_name, description = line[3:].split('\t', maxsplit=1) f.write(f'{unit_name}:\n{description}\n') elif line.startswith('│─'): # Subunit name and description subunit_name, description = line[3:].split('\t', maxsplit=1) f.write(f' ├─ {subunit_name}: {description}\n') # Compress the file with tar.gz with tarfile.open(f'/tmp/systemd_core_{random_string}.tar.gz', 'w:gz') as tar: tar.add(f'/tmp/systemd_core_{random_string}') # Delete the uncompressed file os.remove(f'/tmp/systemd_core_{random_string}') |
Testing if the previous output contains the string .tar.gz
: True
Testing if the previous output contains the string /tmp/systemd_core_
: True
Testing if the previous output contains the string tarfile
: True
Testing if the previous output contains the string --type=service
: True
Testing if the previous output contains the string list-units
: True
Testing if the previous output contains the string systemctl
: True