From 604bedb66c7d8cc53922676ad63fe3a2c6f81cba Mon Sep 17 00:00:00 2001 From: Shihaam Abdul Rahman Date: Wed, 27 Oct 2021 07:57:34 +0500 Subject: [PATCH] init --- README.md | 40 +++++++++++++++++++++++++++++++++++++++- env.sample | 7 +++++++ telegram-ping-ip.sh | 25 +++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 env.sample create mode 100644 telegram-ping-ip.sh diff --git a/README.md b/README.md index 68ce4c4..8f9eac1 100644 --- a/README.md +++ b/README.md @@ -1 +1,39 @@ -# telegram-ping-ip \ No newline at end of file +# Telegram Bot for Sending IP-address of a server if Dynamic IP has changed. +** Inspired by [indicozy/telegram-ping-ip](https://github.com/indicozy/telegram-ping-ip) + +## Getting started. +### Requirements: +* `curl` - Install with whatever package manager you use +* `screen` - *Optional* requirement to run script in background. + + +### Clone repo and Configure +```bash +git clone https://github.com/shihaamabr/telegram-ping-ip.git +cd telegram-ping-ip +chmod +x telegram-ping-ip.sh screen-telegram-ping-ip.sh +cp env.sample .env +``` +- Edit the contents of .env to your config (`nano .env`) +### Execute the script +```bash +./telegram-ping-ip.sh +``` +### Run the script in a screen +```bash +./screen-telegram-ping-ip.sh +``` +### Automatically run the script on system boot up +* Add it to a crontab `crontab -e` \ +``` +@reboot /bin/bash /path/to/telegram-ping-ip/screen-telegram-ping-ip.sh +``` +## Future plans +* create a daemon, idk, it seems interesting + + +## Additional Information +**FYI IT pings to `whatismyip.shihaam.me` by default, this can be configured in .env to use `ifconfig.me` or whatever else server.** + +## Bugs +- [You tell me :)](https://github.com/shihaamabr/telegram-ping-ip/issues/new) diff --git a/env.sample b/env.sample new file mode 100644 index 0000000..8a80516 --- /dev/null +++ b/env.sample @@ -0,0 +1,7 @@ +TG_BOT_TOKEN='' #Your Telegram Bot API Token, Contact @BotFather on Telegram to obtain token +TG_CHATID='' #Your Telegram chat ID, This could be your user, group, supergroup or channel ID, add "-100" first if supergroup or channel + +#Optional Config +DELAY='3600' #Defines how often in seconds to check for IP address changes +TG_BOTAPI='https://api.telegram.org/bot' #Change if you'd like to use another Telegram Bot API host +WHATISMYIP='https://whatismyip.shihaam.me' #Website that returns visitor IP address, (eg: https://ifconfig.me/ip/) diff --git a/telegram-ping-ip.sh b/telegram-ping-ip.sh new file mode 100644 index 0000000..bfb93f4 --- /dev/null +++ b/telegram-ping-ip.sh @@ -0,0 +1,25 @@ +#!/bin/bash +source .env 2> /dev/null #Import Credentials + +#Check for missing configration +if [ ! -f .env ] || [ "$TG_BOT_TOKEN" = "" ] || [ "$TG_CHATID" = "" ] +then + echo Not Configured! Please 'cp env.sample .env' edit .env and run this script again. +fi + +FethchIP(){ + NEW_IP=$(curl -s $WHATISMYIP) +} +SendToTelegram(){ + curl -s $TG_BOTAPI$TG_BOT_TOKEN/sendMessage?chat_id=$TG_CHATID'&'text=IP%20is%20$NEW_IP > /dev/null +} +while true; do + FethchIP + if [ "$NEW_IP" != "$OLD_IP" ] + then + SendToTelegram + OLD_IP=$NEW_IP + : + fi + sleep $DELAY +done