Last active
January 23, 2018 01:13
-
-
Save sarg/0ede025bdfb4b30702bcb9c2809c098d to your computer and use it in GitHub Desktop.
notify-tg for ssh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 0. prerequisites: | |
# register bot and get access token | |
# message /newbot to @BotFather | |
# enter bot name | |
# save token received from @BotFather | |
# send message to your newly created bot | |
# find out chat_id with | |
# curl -s https://api.telegram.org/bot${TOKEN}/getUpdates \ | |
# | jq '.result[].message.chat.id' | uniq | |
# | |
# 1. put this script in ~/.ssh/backchannel/notify-tg.sh | |
# | |
# 2. start http server for backchannel | |
# cat <<EOF > ~/.config/systemd/user/ssh-tg.service | |
# [Unit] | |
# Description=ssh backchannel | |
# [Service] | |
# Type=simple | |
# Restart=always | |
# ExecStart=/bin/sh -c 'cat ~/.ssh/backchannel/notify-tg.sh | nc -q 0 -l localhost -p 40000' | |
# [Install] | |
# WantedBy=default.target | |
# EOF | |
# | |
# systemctl --user enable ssh-tg.service | |
# systemctl --user start ssh-tg.service | |
# | |
# 3. ssh with remote forwarding to some host | |
# ssh -R 40000:127.0.0.1:40000 some.server | |
# | |
# 4. on some.server source notify-tg.sh script fetched through backchannel | |
# source <(curl -s http://localhost:40000/notify-tg.sh) | |
# | |
# 5. now you can use notify-tg without actually having the script on the server | |
# | |
# 6. to make things easier, add forwarding to .ssh/config and sourcing to .bashrc | |
# | |
# ps. I know, there are other ways to do this, but they may require enabling features in sshd | |
# and that is not always possible. | |
function notify-tg { | |
TOKEN=your:token | |
CHAT_ID=your_id | |
CURL='curl -s -G --data-urlencode' | |
DEST="https://api.telegram.org/bot${TOKEN}/sendMessage?chat_id=${CHAT_ID}" | |
if [ "$1" = '-l' ]; then | |
while read line; do | |
$CURL "text=${line}" $DEST > /dev/null | |
done | |
else | |
$CURL "text@-" $DEST > /dev/null | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment