shell实现的自动签到脚本

早就想做个自动签到脚本了,最近有空,就研究了一下

这里使用了curl:

cURL is a computer software project providing a library and command-line tool for transferring data using various network protocols. The name stands for "Client URL".

自动签到无非就两步:1.登录 2.签到 3.自动化

而签到需要用到登录后服务器提供的cookie,所以在登录后要先把cookie存起来,即存在一个文件里

一.登录

1
curl --cookie-jar cookies -d "email=myemail&passwd=mypassword" https://xxx.xxx/signin && echo

&& echo在这里的作用只是为了换行


1
2
3
4
Syntax:
command1 && command2

command2 will execute if command1 has executed successfully. This operator allows us to check the exit status of command1.

用到的curl命令参数:

参数 作用
-c filename
--cookie-jar file name 【参考】
(HTTP)完成操作后将服务器返回的cookies保存到指定的文件; 指定参数值为“-”将定向到标准输出“如控制台”;
-d -d参数用于发送 POST 请求的数据体。
使用-d参数以后,HTTP 请求会自动加上标头Content-Type : application/x-www-form-urlencoded。并且会自动将请求转为 POST 方法,因此可以省略-X POST

二.签到

1
curl --cookie cookies  -H 'content-length: 0' -X POST https://xxx.xxx/user/checkin

用到的curl命令参数:

参数 作用
-b name=data --cookie name=data 【参考】 (HTTP)发送cookie数据到HTTP服务器,数据格式为:"NAME1=VALUE1; NAME2=VALUE2"; 如果行中没有“=”,将把参数值当作cookie文件名; 这个cookie数据可以是由服务器的http响应头“Set-Cookie:”行发送过来的;

注:这里其实有个问题,那就是为什么要加上 -H 'content-length: 0'

By default, POST requests require a Content-Length header, or they result in a 411 error. The error occurs even if the POST request doesn’t contain a body and theoretically shouldn’t require Content-Length header

三.自动化

首先先编写自己的脚本,命名为mycheckin.sh

1
2
3
4
#!/bin/bash
curl --cookie-jar cookies -d "email=myemail&passwd=mypassword" https://xxx.xxx/signin && echo

curl --cookie cookies -H 'content-length: 0' -X POST https://xxx.xxx/user/checkin && echo

用crontab -e进入编辑

1
0 1 * * *  /var/spool/cron/mycheckin.sh >>  /var/spool/cron/checkin.log

每天一点的时候执行一次签到,并且返回的信息会记录在checkin.log中

参考:

https://www.geeksforgeeks.org/difference-between-chaining-operators-in-linux/#:~:text=Logical%20AND%20operator(%26%26)%3A,commands%20in%20the%20command%20line.

https://www.runoob.com/linux/linux-shell-echo.html

http://aiezu.com/article/linux_curl_command.html

https://serverfault.com/questions/315849/curl-post-411-length-required

https://salesforce.stackexchange.com/questions/76662/post-requires-content-length-error-occur-when-i-am-trying-to-call-custom-rest

https://stackoverflow.com/questions/33492178/how-to-pass-content-length-value-in-header-using-curl-command

https://techdocs.akamai.com/property-mgr/docs/allow-post

https://help.salesforce.com/s/articleView?id=000337925&type=1

https://www.runoob.com/w3cnote/linux-crontab-tasks.html

https://importcjj.github.io/jian-dan-de-acfunzi-dong-qian-dao.html