在ubuntu上快速配置Clash脚本

忍无可忍 image.png

# -*- coding: UTF-8 -*-

import os
import wget
import gzip
import argparse


def un_gz(file_name):
    """ungz zip file"""
    f_name = file_name.replace(".gz", "")
    g_file = gzip.GzipFile(file_name)
    open(f_name, "wb+").write(g_file.read())
    g_file.close()

def config(subscribe_url):
    os.system('wget -O config.yaml '+subscribe_url+' -U Chrome')
    os.system('./clash-linux-amd64-v1.13.0 -d .')



# linux必须是x86架构的(不能是ARM)
clash_package_url = 'https://github.com/Dreamacro/clash/releases/download/v1.13.0/clash-linux-amd64-v1.13.0.gz'
the_location_of_the_downloaded_file = os.getcwd()+'/clash-linux-amd64-v1.13.0'

parser = argparse.ArgumentParser(
                    prog = '',
                    description = '',
                    epilog = '')

parser.add_argument('-url')
args = parser.parse_args()
subscribe_url = args.url
if subscribe_url == None:
    print('错误: 启动时-url 提供订阅链接')
    exit(0)




# 如果bin已经存在的话,只是简单的配置订阅,不执行下载
if os.path.exists(the_location_of_the_downloaded_file):
    print('-- 存在Clash客户端 执行配置操作')
# 
else:
    print('-- 下载Clash客户端')
    wget.download(clash_package_url)
    un_gz(the_location_of_the_downloaded_file+'.gz')
    os.system('rm '+the_location_of_the_downloaded_file+'.gz')

os.system('chmod +x ./clash-linux-amd64-v1.13.0')
config(subscribe_url)


上面脚本自动下载Clash并启动,但是linux上还需要配置环境变量,如下。

export http_proxy="http://127.0.0.1:7890"
export https_proxy="http://127.0.0.1:7890"
unset http_proxy
unset https_proxy

export和unset必须shell中手动输入,不能在shell脚本中执行。

2023.3.4 发现个好网站 https://ghproxy.com/ 专门代理github仓库的,good