1、创建脚本
下面的命令都需要Root权限 (这个应该不用说吧) vim /usr/bin/proxy
将下面的脚本填入创建的文件中 (第四行Port需要改为自己对应的代理端口号)
#!/bin/sh
output=$(ipconfig.exe)
output=$(echo "$output" | iconv -f GBK -t UTF-8)
hostip=$(echo "$output" | grep -A 4 "vEthernet (WSL (Hyper-V firewall))" | grep "IPv4 地址" | awk '{print $NF}' | tr -d '\r\n')
port=7897
PROXY_HTTP="http://${hostip}:${port}"
PROXY_SOCKS5="socks5://${hostip}:${port}"
set_proxy(){
export http_proxy="${PROXY_HTTP}"
export HTTP_PROXY="${PROXY_HTTP}"
export https_proxy="${PROXY_HTTP}"
export HTTPS_proxy="${PROXY_HTTP}"
git config --global http.proxy "${PROXY_SOCKS5}"
git config --global https.proxy "${PROXY_SOCKS5}"
echo "proxy success!"
}
unset_proxy(){
unset http_proxy
unset HTTP_PROXY
unset https_proxy
unset HTTPS_PROXY
git config --global --unset http.proxy
git config --global --unset https.proxy
}
test_setting(){
echo "Host ip:" ${hostip}
echo "Current proxy:" $https_proxy
echo "Git proxy:" $(git config -l)
}
if [ "$1" = "set" ]
then
set_proxy
elif [ "$1" = "unset" ]
then
unset_proxy
elif [ "$1" = "test" ]
then
test_setting
else
echo "Unsupported arguments."
fi
2、授予脚本可执行权限
sudo chmod +x /usr/bin/proxy
3、运行命令
到这里就完成啦!
以后如需设置代理,
就可以直接使用proxy set
来自动设置代理,
使用proxy unset
来取消设置代理啦!