最近工作比较忙,然后好久没时间来看博客。这两天忙着写了两个脚本,对工作中帮助很大,这里分享出来。

检测上传token

这个脚本是模仿着别人的脚本,自己加以改进的。话不多说,先放代码

#!/usr/bin/env bash
if [ "$1" = "" ]; then
    echo 'must specify $1 eg: token xxx'
    exit 1
else
    policybase64=$(echo $1 | awk -F ":" '{print $3}')
    policy=$(base64 -D <<<$policybase64)
    echo $policy
    key=$(echo $policy | awk -F "scope" '{print $2}' | awk -F "," '{print $1}' | awk -F ":" '{print $3}')
fi
if [$key = ""]; then
    dd if=/dev/zero of=1M.file bs=1m count=1
    echo 'curl -vo /dev/null upload.qiniu.com -F"token=$token" -F"file=@$1M.file"'
    curl -v upload.qiniu.com -F"token=$1" -F"file=@1M.file"
else
    key1=$(echo $key | awk -F '"' '{print $1}')
    dd if=/dev/zero of=1M.file bs=1m count=1
    echo 'curl -vo /dev/null upload.qiniu.com -F"token=$token" -F"file=@$1M.file"'
    curl -v upload.qiniu.com -F"token=$1" -F"key=$key1" -F"file=@1M.file"
fi
rmfile=1M.file
rm $rmfile
echo ""
echo '上传策略为:'
policy=$(echo $1 | awk -F ":" '{print $3}')
date=$(echo $policy | base64 -D| sed 's/[{}]//g' | awk 'BEGIN{FS=":"} {print $3}')
echo policy字段: $policy
echo policy字段base64解码是: `base64 -D <<<$policy`
echo policy字段过期时间为: `date -r  $date`

使用方法 ./qiniu_uptoken + 上传token
可以解析出上传策略,token是否合法,以及过期时间
链接:
GitHub stars

curl命令检测各类cdn命令脚本

代码:

#!/usr/bin/env bash
echo "**********************curl 脚本**********************
1、中间源访问检测
2、边缘访问检测
3、特定cdn节点访问检测
4、客户源站访问检测(源站测试也可用)
*****************************************************
"
read -p "选择您想要的功能:" num

sourceCNAME(){
echo "中间源访问检测"
read -p "请输入访问的资源:" a1 
if [ "$a1" = "" ]; then
    echo 'must specify file path'
    exit 1
else
    read -p "请输入sourceCNAME:" b1
    if [ "$b1" = "" ]; then
    echo 'must specify sourceCNAME path'
    exit 1
    fi
fi
c1=$(echo $a1 | awk -F '://' '{print $1}')
if [ "$c1" = "http" ] || [ "$c1" = "https" ]; then
    d1=$(echo $a1 | awk -F '://' '{print $2}' )
fi
if [ "$c1" != "http" ] && [ "$c1" != "https" ]; then
    d1=$(echo $a1 )
fi
e1=$(echo $b1 | awk -F ':' '{print $2}')
if [ "$e1" == "" ]; then
    proxy=$b1:80
fi
if [ "$e1" != "" ]; then
    proxy=$b1
fi
f1=$(echo $b1 | awk -F ':' '{print $1}')
echo 中间源: `dig $f1 cname +short`
curl -I $d1 -x $proxy | head -1 
}
edge(){
echo "边缘访问检测"
read -p "请输入访问的资源:" a2
if [ "$a2" = "" ]; then
    echo 'must specify file path'
    exit 1
fi
curl  -I $a2  | head -1
}
resolve(){
echo "特定cdn节点访问检测"
read -p "请输入访问的资源:" a3 
if [ "$a3" = "" ]; then
    echo 'must specify file path'
    exit 1
else
    read -p "请输入用户提供的节点ip:" b3
    if [ "$b3" = "" ]; then
    echo 'must specify ip'
    exit 1
    else if [ "$b3" != "" ];then
            while [ `echo $b3 | awk -F '.' '$1>255||$2>255||$3>255||$4>255'` ]
            do 
            read -p "非法ip,请输入正确的节点ip:" b3
            done
        fi
    fi  
fi
scheme=$(echo $a3 | awk -F '://' '{print $1}')
if [ "$scheme" = "http" ] || [ "$scheme" = "https" ]; then
    domain=$(echo $a3 | awk -F '://' '{print $2}' | awk -F '/' '{print $1}')
    if [ "$scheme" = "http" ];then
        finaldomain=$domain:80
    else if [ "$scheme" = "https" ];then
        finaldomain=$domain:443
        fi
    fi
fi
if [ "$scheme" != "http" ] && [ "$scheme" != "https" ]; then
    finaldomain=$(echo $a3 | awk -F '/' '{print $1}'):80
fi
#echo $finaldomain,$b3
curl  -I $a3 --resolve $finaldomain:$b3  | head -1
}
sourceHOST(){
echo "客户源站访问检测(源站测试也可用)"
read -p "请输入访问的资源:" a4
if [ "$a4" = "" ]; then
    echo 'must specify file path'
    exit 1
else
    read -p "请输入客户源站(或者回源hostq):" b4
    if [ "$b4" = "" ]; then
    echo 'must specify source path'
    exit 1
    fi
fi
c4=$(echo $b4 | awk -F '://' '{print $1}')
if [ "$c4" = "http" ] || [ "$c4" = "https" ]; then
    d4=$(echo $a4 | awk -F '://' '{print $2}' )
fi
if [ "$c4" != "http" ] && [ "$c4" != "https" ]; then
    d4=$(echo $a4 )
fi
curl -I $d4 -H  HOST:$b4 | head -1
}
choose(){
case "$num" in
1)
sourceCNAME
;;
2)
edge
;;
3)
resolve
;;
4)
sourceHOST
;;
*)
read -p "非法输入,请输入正确的数字[1-4]:" num
choose
;;
esac
}
choose

链接:

GitHub stars

版权声明:本文为原创文章,版权归 heroyf 所有
本文链接:https://heroyf.club/2018/07/qiniu-shell/


“苹果是给那些为了爱选择死亡的人的奖励”