[ad_1]
哦开发人员和系统管理员的一项基本任务是获取有关失败服务或磁盘空间不足和其他严重故障的警报通知。 让我们看看如何向由 Apple iOS 或 Google Android 手机提供支持的移动设备发送或推送直接消息。
如何实时向 iOS 和 Android 推送/发送消息
我们可以使用 AWS 社交网络 推送通知服务直接向移动设备上的应用程序发送警报。 但是,今天我将使用简单的应用程序服务,称为 推倒. 这是一个简单的应用程序,可在 Android、iPhone、iPad 和桌面设备(包括 Android Wear 和 Apple Watch)上获取实时通知。
为什么要发送通知?
作为一名独立开发人员和 Linux 系统管理员,我需要一种简单的方法来使用 API 为我的副项目获取通知。 由于问题或 MySQL 只读副本不同步,我可以收到备份失败或 Nginx 服务过载的通知。 我的搜索以 Pushover 结束。 但是,这不是免费服务。 要为自己或小团体使用 Pushover,只需在每个平台上一次性购买 5 美元。 您每月可以发送 7,500 条消息,这足以满足我的需求。 他们还可以选择为一组开发人员和 IT 团队发送消息。 我的标准很简单:
- 我需要支持 Perl、Python 和 bash/shell 脚本。
- 必须将通知推送到 iPhone。
- 应该不会很贵。
Pushover 服务似乎符合我的所有要求。 废话不多说,让我们用一些例子来实践一下。
第 1 步 – 注册 Pushover
首先下载 Pushover 设备客户端:
- 安卓版
- iOS(iPhone、iPod Touch 和 iPad)版本
确保您订阅了服务或获得了一个为期 7 天的试用帐户。 登录后,注册您的 cli 应用程序,设置其名称,并获得一个 API 令牌 作为回报。
步骤 2 – 创建一个 shell 脚本包装器 API 脚本
创建一个新的shell脚本如下:$ vi ~/bin/cli_app.sh
附上代码:
#!/usr/bin/env bash # push/send message ios and android using API from my Linux box # Set API stuff _token='YOUR-API-TOKEN-HERE' _user='YOUR-USER-NAME-HERE' # Bash function to push notification to my iPhone # yes you can push/send message android too using the same function push_to_mobile(){ local t="${1:cli-app}" local m="$2" [[ "$m" != "" ]] && curl -s --form-string "token=${_token}" --form-string "user=${_user}" --form-string "title=$t" --form-string "message=$m" https://api.pushover.net/1/messages.json }
接下来,使用 源命令:$ source ~/bin/cli_app.sh
测试一下:$ push_to_mobile "bash-notification" "This is a test. Job foo failed on $HOSTNAME @ $(date)"
我会立即在手机上收到通知:
如何从脚本向您的手机发送电子邮件和推送通知
我们可以从我们的 shell 脚本中简单地使用 sendmail 命令或 mail 命令,如下所示:
#!/usr/bin/bin bash # Wrapper backup-script.sh by Vivek Gite under GPL v2.0+ # ------------------------------------------------------- # # Set email stuff # warning: must need pre-configured smtpd such as sendmail/postfix # subject="rsnapshot backup job failed at $HOSTNAME" log_file="/path/to/my.log.txt" from="[email protected]" to="[email protected]i.biz" # # start daily backup and set log to ${log_file} # all reports created by rsnapreport.pl script including ERROR # /usr/bin/rsnapshot daily 2>&1 | /root/bin/rsnapreport.pl > "${log_file}" # Catch errors status=$? alogs="$(egrep -w '^ERROR:|ERROR' $log_file)" # If failed, notify using both email and push message to my phone if [ $status -ne 0 ] || [ "$alogs" != "" ]; then mail -A "$log_file" -s "$subject" -r "$from" "$to" <<< "Backup script failed with error. Check attached log file" # # Push/send message to iOS and Android using Pushover API too # source ~/bin/cli_app.sh push_to_mobile "backup-script" "$subject. See $to email for detailed failed log." >/dev/null fi
有关从 CLI 发送电子邮件的更多信息,请参阅以下教程:
- UNIX / Linux:使用邮件命令编写 Shell 脚本
- 从 Unix / Linux 命令发送带有附件的电子邮件 [ Shell Prompt ]
- 如何:在 Unix / Linux 中使用 mail 命令发送文本文件的内容
结论
到目前为止,Pushover 服务和应用程序对我来说效果很好。 我可以使用 shell/Perl/Python 脚本轻松地向 iOS 和 Android 设备发送/推送消息。 查看我的所有其他建议 推特主题:
[ad_2]