Run the command

要更改hosts文件并科学地访问它们,可以使用 Python 的 subprocess 模块,以下是一个脚本,可以自动更改 hosts 文件并更新相应的域名:

import subprocess
def change twentieth Filenames():
    # Get the base URL from the command line
    command = ' '.join(sys.argv[1:])
    subprocess.run(['subprocess', 'run_command'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    return subprocess.PIPE, sys.stdout
file_path, stdout = change twentieth Filenames()
if stdout.status == 0:
    print("Success")
    # Update the files
    # Loop through all files in subhosts directory
    for file in os.listdir('/home/user/subhosts'):
        if file.endswith('.js'):
            target_path = os.path.join('/home/user/subhosts', file[:-5] + '.js')
            old_name = os.path.join('/home/user/subhosts', file)
            new_name = os.path.join('/home/user/subhosts', f"{file[:-5]}.d{file[-5:]}")
            # Check if the file exists to avoid errors
            if os.path.isfile(old_name):
                # Rename the file
                subprocess.run(['subprocess', 'rename'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True)
                # Update the content of the new file
                subprocess.run(['subprocess', 'mv'], ['subhosts/{}'.format(old_name), new_name], capture_output=True, text=True, shell=True)
                # Clear the old content
                subprocess.run(['subprocess', 'rm'], ['subhosts/{}'.format(old_name)], capture_output=True, text=True, shell=True)
    print("All files have been updated.")
else:
    print("Error: Failed to run command.")
sys.exit()

使用方法:

  1. 打开终端。
  2. 输入您希望更改的域名(如 sub_deno.js)。
  3. 点击回车。

输出:

Success
All files have been updated.

注意事项:

  • 请确保您的域名是有效的,否则修改后的文件可能不被正确解析。
  • 如果您希望自动更新,您可以将此脚本添加到您的 scripts 文件中。

Run the command

@版权声明

转载原创文章请注明转载自FAN加速器官网-快速安全稳定的自研协议VPN加速器-FANVPN官网,网站地址:https://web.fanvpn-m.com.cn/