Skip to content

Instantly share code, notes, and snippets.

@staugur
Last active September 19, 2021 18:04
Show Gist options
  • Save staugur/d7951b3873372f1cc6dec9aec03a7014 to your computer and use it in GitHub Desktop.
Save staugur/d7951b3873372f1cc6dec9aec03a7014 to your computer and use it in GitHub Desktop.
向搜索引擎推送自己的URL。 1、Google Search Console是谷歌的搜索管理系统, 2、BaiduSpider
#!/bin/bash
#百度站长平台(http://zhanzhang.baidu.com/linksubmit/index)提供的主动推送(实时)接口自动提交URLs。
#要求有mailx包,开启了邮件服务
#在这里,我们用curl访问接口的方式推送一个包含所有URL列表的文件,不过需要注意的是,你推送的URL应该与百度站长平台上验证的站点一致。
#以本站为例,我在百度站长平台上添加了www.saintic.com,到网页抓取-链接提交下,看到自动提交下有个主动推送(实时),其中有你站点和token信息,看到curl推送示例,主要就是它。
#以下是我的简单推送脚本,因为百度有推送数量限制,所以放到crontab中每三个小时运行一次。
ci_dir=/tmp/baidu_ci
ci_urls=${ci_dir}/urls.txt
ci_r=${ci_dir}/ci_result.txt
[ -d $ci_dir ] || mkdir -p $ci_dir
[ -f $ci_urls ] && rm -f $ci_urls
check() {
code=$(curl -I -s $url |head -1|awk -F "HTTP/1.1 " '{print $2}'|awk '{print $1}')
if [ "$code" = "200" ];then
echo $url >> $ci_urls
fi
}
echo "http://www.saintic.com/" > $ci_urls
static_urls=(
"http://www.saintic.com/coreweb/index.html"
"http://www.saintic.com/sdpv1.0/index.html"
"http://www.saintic.com/sdpv1.0/docker.html"
"http://www.saintic.com/sdpv1.0/subversion.html"
"http://www.saintic.com/sdpv1.0/vsftpd.html"
"http://www.saintic.com/sdpv1.0/autodeploy.html"
"http://www.saintic.com/blog"
)
for url in ${static_urls[@]}
do
check
done
for i in {1..20}
do
url=http://www.saintic.com/blog/${i}.html
check
done
curl -s -H 'Content-Type:text/plain' --data-binary @$ci_urls "http://data.zz.baidu.com/urls?site=此处改为你的站点&token=此处是你的token&type=original" > $ci_r
#此处的curl就是你在百度站长平台看到的curl推送示例,其中@urls.txt改为@$ci_urls即可。
push_nums=$(jq .success ${ci_r})
fail_code=$(jq .error ${ci_r})
fail_msg=$(jq .message ${ci_r})
if [ $push_nums = "0" ];then
echo "$(date +%Y-%m-%d,%H:%M:%S),推送异常,推送结果成功但条数为0!!!" | mailx -r "Baidu_ci@saintic.com" -s "百度实时推送:FAIL" staugur@vip.qq.com
exit 1
fi
if [ "$push_nums" != "null" ];then
mailx -r "Baidu_ci@saintic.com" -s "百度实时推送:SUCCESS" -c "staugur@saintic.com" staugur@vip.qq.com <<EOF
$(date +%Y-%m-%d,%H:%M:%S),成功推送${push_nums}条记录。
推送列表:
$(cat ${ci_urls})
EOF
else
echo "$(date +%Y-%m-%d,%H:%M:%S),推送错误,错误代码:${fail_code},原因是:${fail_msg}." | mailx -r "Baidu_ci@saintic.com" -s "百度实时推送:FAIL" staugur@vip.qq.com
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment