首页 > 解决方案 > 在公司网络中检查并设置 .zshrc/.bashrc 中的 http(s)_proxy

问题描述

我想在我的私人网络(使用私人计算机)和公司网络(使用公司计算机)中使用我的 doftiles。目前我做

export http_proxy=http://proxy-company.com:8080
export https_proxy=$http_proxy

在我的 .zshrc/.bashrc 文件中,但这会导致在我的家庭网络中与我的私人计算机一起使用时出现问题。

有没有办法检测我在公司网络中,然后在 .zshrc/.bashrc 文件中执行类似的操作(伪代码)

if COMPANY_NETWORK
then
    export http_proxy=http://proxy-company.com:8080
    export https_proxy=$http_proxy
fi

编辑:

这似乎对我有用(感谢@Philippe 和@mattst)

if ping -c 1 proxy-company.com &> /dev/null
then
    export http_proxy=http://proxy-company.com:8080
    export https_proxy=$http_proxy
fi

谢谢您的帮助

标签: bashproxyzsh

解决方案


有很多方法可以做到这一点。就个人而言,我可能会使用该hostname命令。

#!/bin/bash

hostname=$(hostname)
if [ "$hostname" = "CompanyComputersHostName" ]; then
    echo "Do what you want"
fi

根据您的情况,Philippe 关于检查特定目录是否存在的建议是完全合理的,尽管我会优先创建一个文件(可能是隐藏的)并检查它是否存在。

#!/bin/bash

if [ -f "~/.incompany" ]; then
    echo "The ~/.incompany file exists"
fi

if [ -d "~/DirName" ]; then
    echo "The ~/DirName directory exists"
fi

编辑:

根据您的评论,您可以检查您的私人计算机并假设任何不匹配的主机名必须是公司计算机之一。

#!/bin/bash

host=$(hostname)

if [ "$host" = "Private_01" -o "$host" = "Private_02" -o "$host" = "Private_03" ]; then
    echo "This is one of the private computers"
else
    echo "This must be one of the company computers"
fi

推荐阅读