Skip to content

jianminglok/telegramopenwrt

 
 

Repository files navigation

Telegram Scripts for OpenWrt

This is a set of scripts with a plugins API written in bash, you can use it to manage and get informations on OpenWRT Routers.

How its works ?

First of all, Make a bot for you ! https://core.telegram.org/bots/api#authorizing-your-bot

With the bot created, you need to replace "[PUT YOUR BOT KEY HERE]" in variables file with your bot key.

Second, you need to send a initial message to your bot in Telegram App. After you send the message, in the OpenWRT run this:

curl -s -k -X GET https://api.telegram.org/bot<YOUR BOT ID>/getUpdates | grep -oE "\"id\":[[:digit:]]+" | head -n1 | awk -F : '{print $2}'

Get the number and replace "[PUT ID OF THE CHAT THAT YOU START WITH BOT]" in the variables file.

Directory structure

+--- LICENSE
+--- README.md
+--- init.d/
	+--- lanports
	+--- telegram_bot
+--- lanports
+--- plugins/
	+--- block
	+--- fwlist
	+--- gethosts
	+--- getip
	+--- help/
		+--- block
		+--- fwlist
		+--- gethosts
		+--- getip
		+--- getwifi
		+--- start
		+--- unblock
		+--- wifilist
	+--- start
	+--- unblock
	+--- wifilist
+--- telebot
+--- telegram\_bot

lanports file

This file reads the router logs with the logread -f command, and sends messages via bot telegram if a router port is turned off / on or the router delivers an IP address via DHCP.

init.d directory

Contains the necessary files for the scripts to be started at the router boot, just move them to the /etc/init.d/ of the router and run: /etc/init.d/lanports enable /etc/init.d/telegram_bot enable

And then:

/etc/init.d/lanports start /etc/init.d/telegram_bot start

plugins directory

This is the main directory, it contains all the commands that the telegram bot can execute.

There are some pre-built commands, which are:

  • Block: Creates a REJECT rule for a host (by macaddr) to make NAT for the internet.
  • Fwlist: Lists all firewall rules.
  • Gethosts: Lists the hosts that are in dhcp.leases, if the argument is a host, will list only the host found by regex.
  • Getip: WAN IP.
  • Start: Creates command help.
  • Unblock: Removes a firewall rule created by Telegram, without an argument, all rules created by Telegram are deleted.
  • Wifilist: List all devices connected to WiFi.

help directory inside plugins

This is the directory containing the help files, with the same name as the command, to be displayed by the start command.

telebot file

This file sends the telegram bot messages generated by lanports

telegram_bot

The telegram_bot script is a loop that receives the updates every second and checks to see if there is a command to execute. If there is a command, the script checks to see if there is a file with the same name as the command inside the plugins directory and runs it, if it exists. The output of the executed script is sent as a response message from the command. Inside the plugins directory, there is the special command "start", which returns a message with the commands and a brief help on each command. For this command to work properly, you need to create a file inside the plugins / help directory, with the name equal to the command.

About

Telegram Scripts for OpenWrt

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Shell 100.0%