【发布时间】:2022-01-08 14:33:25
【问题描述】:
尝试在终端中获取cyberghostvpn --status 显示的输出,即No VPN connections found. 或VPN connection found.
我在 $VPN_status 上尝试了 | grep -x "No VPN connections found." 并收到错误。甚至不记得过去几个小时我尝试过的所有事情。感觉这是一个简单的解决方案,但我只是想念它。以下是代码($index 是从未在此摘录中列出的数组中随机选择的):
VPN_status="$(cyberghostvpn --status)"
echo $VPN_status
if [[ $? == "No VPN connetions found." ]]; then
echo "Connecting to VPN."
cyberghostvpn --country-code $index --server-type traffic --openvpn --connect
else
echo "You are connected to the VPN already."
fi
这会返回:
No VPN connections found.
You are connected to the VPN already.
编辑 - 现在完美运行。这是更新的代码-
array=("AD" "AE" "AL" "AM" "AR" "AT" "BA" "BD" "BE" "BG" "BR" "BS" "BY" "CH" "CL" "CN" "CO" "CR" "CY" "CZ" "DE" "DK" "DZ" "EE" "EG" "ES" "FI" "FR" "GB" "GE" "GL" "GR" "HK" "HR" "HU" "ID" "IE" "IL" "IM" "IN" "IR" "IS" "IT" "JP" "KE" "KH" "KR" "KZ" "LI" "LK" "LT" "LU" "LV" "MA" "MC" "MD" "ME" "MK" "MN" "MO" "MT" "MX" "MY" "NG" "NL" "NO" "NZ" "PA" "PH" "PK" "PL" "PT" "QA" "RO" "RS" "RU" "SA" "SE" "SG" "SI" "SK" "TH" "TR" "TW" "UA" "US" "VE" "VN" "ZA")
size="${#array[@]}"
index=$[ $RANDOM % $size ]
Country=${array[$index]}
VPN_status="$(cyberghostvpn --status)"
VPN_connect="$(cyberghostvpn --openvpn --traffic --country-code $Country --connect)"
echo $VPN_status
if [[ $VPN_status == "No VPN connections found." ]]; then
echo "Connecting to VPN."
$VPN_connect
echo "New country is $Country"
elif [[ $VPN_status == "VPN connection found." ]]; then
echo "Changing country..."
echo "New country is $Country"
else
echo "This shouldn't have happened..."
fi
全部
【问题讨论】:
-
只是一个随机的想法......
cyberghostvpn --status是否也会发出一个状态,例如0表示已连接,1表示已断开连接?在运行状态检查后,echo $?在这两种情况下都会为您提供什么? -
@tink 可能。不确定。刚走出去,几分钟后回来我会试试 echo $?要查看通过终端输入状态命令后的内容,Ty
-
回声 $?断开连接返回 0
-
对于连接?
-
@Grim :
$?总是一些整数。[[ $? == "No VPN connetions found." ]]将始终为 false,因为没有整数可以等于此字符串。