哦開發人員和系統管理員的一項重要任務是接收有關服務故障、磁盤空間不足和其他嚴重故障的警報通知。 讓我們看看如何使用 Apple iOS 或 Google Android 手機向移動設備發送或推送直接消息。
如何將消息實時推送/發送到 iOS 和 Android
可以使用 AWS 社交網絡 推送通知服務將警報直接發送到您移動設備上的應用程序。但是,今天我們將使用一個簡單的應用程序服務,稱為 擊倒..這是一個簡單的應用程序,用於在 Android、iPhone、iPad 和桌面設備(如 Android Wear 和 Apple Watch)上接收實時通知。
為什麼要發送通知?
作為一名獨立開發者和 Linux 系統管理員,我需要一種使用 API 獲取輔助項目通知的簡單方法。由於問題或與 MySQL 只讀副本不同步,您可以收到備份失敗或 Nginx 服務過載的通知。我的搜索以失敗告終。但是,這不是免費服務。要自己或在一個小組中使用 pushover,您只需在每個平台上進行一次 5 美元的購買。您每月可以發送 7,500 條消息。這對我的需求來說已經綽綽有餘了。您還可以向一組開發人員或 IT 團隊發送消息。我的標準很簡單:
- 必須支持 Perl、Python、bash/shell 腳本。
- 需要將通知推送到 iPhone。
- 它應該不會很貴。
Pushover 服務似乎滿足了我的所有要求。讓我們用一些例子來練習,沒有任何麻煩。
第 1 步 – 註冊俯臥撑
第一次下載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 上使用郵件命令發送文本文件的內容
結論是
到目前為止,Pushover 服務和應用程序運行良好。 您可以使用 shell / Perl / Python 腳本輕鬆地向 iOS 和 Android 設備發送/推送消息。查看所有其他建議 推特線程: