如何让路由器按要求定时发BTC价至手机

shell实现让路由器按要求定时发BTC价至手机

有什么用

让路由器按要求定时发BTC价至手机

前提条件

API需要魔法上网来访问

Operwrt的软路由,或者自己刷了OP系统的路由器

下一步

感受:shell真不是人写的,语法太魔性;下一步修改用python实现吧

实现方法

找到两个可用的API

  1. curl -x "http://127.0.0.1:7890" -s https://api-pub.bitfinex.com/v2/ticker/tBTCUSD | awk -F ',' '{print $7}'
  2. curl -x "http://127.0.0.1:7890" -s http://api.coindesk.com/v1/bpi/currentprice.json | python -c "import json, sys; print(json.load(sys.stdin)['bpi']['USD']['rate'])"

发送方案一:微信推送

  1. A plugin for OpenWRT routers to send various information notifications to a mobile phone via WeChat or Telegram.

```bash
#!/bin/sh

priceText=$(curl -x "http://192.168.6.116:7890" -s https://api-pub.bitfinex.com/v2/ticker/tBTCUSD)

#$priceText|awk -F ',' '$7'
price=$(echo $priceText|awk -F ',' '{print $7}')


curl -X POST -H "Content-Type: application/json" -d '{
  "title": "【BTC】:'${price}' ",
  "content": "'${price}'\n----\n##### 内容1\n    设备1\n    设备2\n----\n##### 内容2\n1",
  "token": "6275de3c34fd40ce86a3d7dc5d98465b",
  "template": "markdown"
        }' "http://www.pushplus.plus/send"
```
  1. /etc/init.d/cron start

  2. 定时设置

  3. 添加

0 */3 * * * /usr/share/wechatpush/btc.sh &

crontab -l //显示crontab文件

service cron status

上传btc.sh

打开 http://192.168.6.1:8080/cgi-bin/luci/admin/system/fileassistant

测试用的shell命令:

#!/bin/sh

priceText=$(curl -x "http://192.168.6.116:7890" -s https://api-pub.bitfinex.com/v2/ticker/tBTCUSD)

#$priceText|awk -F ',' '$7'
price=$(echo $priceText|awk -F ',' '{print $7}')

if [ ${price} -gt 39000 ]
then
curl -X POST -H "Content-Type: application/json" -d '{
  "title": "【BTC】:'${price}' ",
  "content": "'${price}'\n----\n##### 内容1\n    设备1\n    设备2\n----\n##### 内容2\n1",
  "token": "6275de3c34fd40ce86a3d7dc5d98465b",
  "template": "markdown"
        }' "http://www.pushplus.plus/send"

fi

切换到这个目录(举个例子)/usr/share/wechatpush/

上传文件后修改计划任务

  1. 打开http://192.168.6.1:8080/cgi-bin/luci/admin/system/crontab
  2. 修改内容为:*/1 * * * * /usr/share/wechatpush/btc.sh

这样每分钟都是会执行,用来测试立即执行的效果;当然也可以直接shell里面敲入./btc.sh执行

灵感来源

现实应用,勉强能用...

还是期待下一步的python实现

posted @ 2023-12-02 14:45  CarlZeng  阅读(17)  评论(0编辑  收藏  举报