#!/bin/bash
SCRIPTPATH=$(realpath $0)
#SCRIPTPATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
#SCRIPTPATH=$(dirname $(readlink -f "$0"))
display_usage() {
echo -e "$SCRIPTPATH\n"
echo -e "\t快速打开宝塔面板后台,原理:查找 ~/.ssh/config 文件对应配置项."
echo -e "\nUsage:\n\tbt [keyword]"
echo -e "Example:\n\tbt myvps"
}
# if less than two arguments supplied, display usage
if [ $# -lt 1 ]
then
display_usage
exit 1
fi
# check whether user had supplied -h or --help . If yes display usage
if [[ ( $* == "--help") || $* == "-h" ]]
then
display_usage
exit 0
fi
##查找并打开宝塔面板,当存在多个网址时,以最后一个为准
hoststr=$1
btinfo=$(sed -nr '/Host .*'$hoststr'.*/,/Host /{/Host .*'$hoststr'.*/{=};/Host [^'$hoststr']/b;p}' ~/.ssh/config|grep -i 'bt.*panel'|tail -n 1)
echo -e "Open URL:$btinfo\n"
bturl=$(echo $btinfo|sed -r -e 's|^[^:]+:||' -e 's/\s//g' -e 's/([&])/^\1/g')
if [ ! -n "$bturl" ];then
#未配置的情况下,尝试先从服务器上查询信息
echo "$1 宝塔控制面板未配置"
ssh $hoststr '/etc/init.d/bt default'
exit 0
fi
#echo $bturl
#如果使用openurl打开网址时,本文件不需要转义&,交给下级逻辑转换
#openurl $bturl
#exit 0
browserPath='%userProfile%\Desktop\360极速浏览器.lnk'
#winpty cmd /c explorer $bturl
$(cmd /c 'dir /b /q '$browserPath>/tmp/desktop.find 2>/tmp/desktop.find)
cat /tmp/desktop.find|iconv -f GBK -t UTF-8|grep -i '找不到文件'>/dev/null
haveCustomPath=$?
rm -f /tmp/desktop.find
if [ $haveCustomPath -ne 0 ];then
#echo "有这个文件"
winpty cmd /c $browserPath $bturl
else
#echo "没有这个文件"
winpty cmd /c explorer $bturl
fi
ssh -o ConnectTimeout=3 $hoststr '/etc/init.d/bt default'