#!/usr/bin/python3 import os from pathlib import Path import subprocess from getpass import getpass startfile = ['#!/usr/bin/python3', 'from subprocess import check_call', '\n', 'path_to_file = ', '\n', 'with open(path_to_file, 'r') as file:', ' for line in file.readlines(): ', ' a_con = line[5:] ', "check_call(['nmcli', 'connection', 'up', a_con])", 'file.close()'] home = Path.home() # for services file in /lib/systemd/system/ path_for_a_con = home / '.config/wire_py/wg_py' # replace in startfile startfile[3] = 'path_to_file = ' "'" + str(path_for_a_con) + "'" with open('start_wg.py', 'w') as file_for_a_con: for line in startfile: file_for_a_con.write(line + '\n') file_for_a_con.close() # create dir in home mkdir = home / '.config/wire_py' os.makedirs(mkdir, exist_ok=True) with open(mkdir / 'wg_py', 'w+') as file: if 'true' not in file: file.write('false') file.close() # Prompt the user for the sudo password sudo_password = getpass('Enter sudo password: ') #def cp_files(): # Define the command to run the Python script with sudo #command = ['sudo', '-S', 'python3', 'script.py'] # Run the command as a subprocess, passing the sudo password #subprocess.run(command, input=sudo_password.encode(), check=True)