bash - 在公司网络中检查并设置 .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
谢谢您的帮助
解决方案
有很多方法可以做到这一点。就个人而言,我可能会使用该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
推荐阅读
- php - php webserver net 执行 sqlsrv_connect()
- kindle - Kindle Paperwhite 可以离线翻译吗?
- python - Django 2 多模型更新视图
- javascript - 如何使用 Leon Sans 和 HTML5 Canvas 为文本粗细设置动画
- php - 数组显示但子数组显示“非法字符串偏移”错误 - PHP
- python - 在 Azure 机器学习中 H2O 下载 CSV
- r - 在 Shiny app 数据表中用一列预选单元格
- c++ - C++ 中 main() 的参数
- ios - 编程的 UISlider 值不变
- raspberry-pi3 - Python套接字服务器未在覆盆子中连接