├── usr └── lib │ └── telegram-bot │ ├── plugins │ ├── free.sh │ ├── uptime.sh │ ├── memory.sh │ ├── wanip.sh │ ├── wol.sh │ ├── functions │ │ ├── ping.sh │ │ └── get_mac.sh │ ├── netstat.sh │ ├── wifi_list.sh │ ├── swports_list.sh │ ├── clients.sh │ └── wll_list.sh │ └── telegram_bot ├── etc ├── config │ └── telegram_bot └── init.d │ └── telegram_bot └── README.md /usr/lib/telegram-bot/plugins/free.sh: -------------------------------------------------------------------------------- 1 | free -------------------------------------------------------------------------------- /usr/lib/telegram-bot/plugins/uptime.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | uptime -------------------------------------------------------------------------------- /usr/lib/telegram-bot/plugins/memory.sh: -------------------------------------------------------------------------------- 1 | cat /proc/meminfo | sed -n '1,5p' 2 | -------------------------------------------------------------------------------- /usr/lib/telegram-bot/plugins/wanip.sh: -------------------------------------------------------------------------------- 1 | nslookup myip.opendns.com resolver1.opendns.com | awk '/^Address( 1)?: / { print $3 }' -------------------------------------------------------------------------------- /usr/lib/telegram-bot/plugins/wol.sh: -------------------------------------------------------------------------------- 1 | if [ -z "$1" ]; then 2 | echo "MAC address is empty" 3 | else 4 | /usr/bin/etherwake -D -i 'br-lan' "$1" 5 | fi 6 | -------------------------------------------------------------------------------- /etc/config/telegram_bot: -------------------------------------------------------------------------------- 1 | config telegram_bot 'config' 2 | option bot_token '' 3 | option chat_id '' 4 | option polling_time '1' 5 | option timeout '30' 6 | -------------------------------------------------------------------------------- /usr/lib/telegram-bot/plugins/functions/ping.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | DEVICE="$1" 4 | NL="$2" 5 | 6 | ping ${DEVICE} -w 1 -q &>/dev/null; [ $? == 0 ] && echo -en "Up" || echo -en "Down" 7 | if [ -z ${NL} ];then 8 | echo -en "\n" 9 | fi -------------------------------------------------------------------------------- /usr/lib/telegram-bot/plugins/netstat.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | netstat -ntu | grep -v "127.0.0.1" | grep -v "::ffff:" |grep "ESTABLISHED\|CLOSED\|TIME_WAIT" | sed -e 's/_/\\_/g' | awk '{print "Protocolo: " $1 " \nOrigem: " $4 "\nDestino: " $5 "\nState: " $6 "\n" } ' 4 | -------------------------------------------------------------------------------- /etc/init.d/telegram_bot: -------------------------------------------------------------------------------- 1 | #!/bin/sh /etc/rc.common 2 | # Copyright (c) 2011-2015 OpenWrt.org 3 | 4 | START=99 5 | 6 | start() { 7 | if [ ! -f "/var/run/telegram_bot.pid" ]; then 8 | echo "1" > /var/run/telegram_bot.pid 9 | /usr/lib/telegram-bot/telegram_bot >/dev/null 2>&1 & 10 | fi 11 | return 0 12 | } 13 | 14 | stop() { 15 | rm -f /var/run/telegram_bot.pid 16 | kill $(pgrep -f telegram-bot/telegram_bot) >/dev/null 2>&1 17 | } 18 | 19 | restart(){ 20 | stop 21 | sleep 1 22 | start 23 | } 24 | 25 | boot() { 26 | (sleep 5 && start >/dev/null 2>&1) & 27 | return 0 28 | } -------------------------------------------------------------------------------- /usr/lib/telegram-bot/plugins/wifi_list.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | 4 | old=$IFS 5 | IFS=$'\n' 6 | TOTAL=0 7 | 8 | echo "*Wireless*" 9 | 10 | for rule in $(uci show wireless | grep "default_radio" ) 11 | do 12 | line=$(echo ${rule} | awk -F "." '{print $3}') 13 | id=$(echo ${rule} | grep ".ssid" | grep -oE "\[[[:digit:]]+\]" | awk '{gsub("\\[|]","");printf $1}') 14 | if [ "$id" != "" ]; then 15 | echo "rate: "$(iwinfo wlan${id} info | grep -oE "[[:digit:]]+\.[[:digit:]]+ MBit\/s") 16 | awk 'BEGIN{x='$(iwinfo wlan${id} info | grep -oE "[[:digit:]]+/[[:digit:]]+")' * 100; printf "quality: %.0f%%\n", x}' 17 | fi 18 | new=${line//\'/} 19 | new2=${new//_/\\_} 20 | new3=${new2//=/: } 21 | echo ${new3//\*/all} 22 | done 23 | IFS=$old -------------------------------------------------------------------------------- /usr/lib/telegram-bot/plugins/swports_list.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | 4 | IFS=$'\n' 5 | PORTS=$(swconfig dev switch0 show | grep link | grep -oE "port:[[:digit:]][[:space:]]link:(up|down)([[:space:]]speed:[[:digit:]]+|$)") 6 | for port in $PORTS 7 | do 8 | id=$(echo $port | grep -oE "port:[[:digit:]]" | cut -d ':' -f 2) 9 | if [ "$id" == "0" ] 10 | then 11 | echo "CPU" 12 | elif [ "$id" == "1" ] 13 | then 14 | echo "WAN" 15 | elif [ "$id" == "2" ] 16 | then 17 | echo "LAN 1" 18 | elif [ "$id" == "3" ] 19 | then 20 | echo "LAN 2" 21 | elif [ "$id" == "4" ] 22 | then 23 | echo "LAN 3" 24 | elif [ "$id" == "5" ] 25 | then 26 | echo "LAN 4" 27 | elif [ "$id" == "6" ] 28 | then 29 | echo "EMPTY" 30 | fi 31 | echo -en "${port// /\n}\n\n" 32 | done -------------------------------------------------------------------------------- /usr/lib/telegram-bot/plugins/functions/get_mac.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | PUREMAC="${1:0:8}" 4 | LMAC=$(echo $PUREMAC | awk '{ print toupper($0) }') 5 | MAC=${LMAC//:/} 6 | 7 | maclist=$(curl -s -k -L -X GET -H "Content-Type: application/json; charset=utf-8" \ 8 | -H "Origin: https://regauth.standards.ieee.org" \ 9 | -H "Referer: https://regauth.standards.ieee.org/standards-ra-web/pub/view.html" \ 10 | "https://services13.ieee.org/RST/standards-ra-web/rest/assignments/?registry=MA-L&sortby=organization&sortorder=asc&text=${MAC}") 11 | 12 | IDS=${MAC//-/} 13 | HEX=$(jsonfilter -s "$maclist" -e $.data.hits[*].assignmentNumberHex) 14 | 15 | i=0 16 | for hexs in $HEX 17 | do 18 | if [ "$hexs" == "$IDS" ]; then 19 | name=$(jsonfilter -s "$maclist" -e $.data.hits[$i].organizationName) 20 | #echo "Identifier: $LMAC" 21 | echo "Organization: $name" 22 | exit 23 | fi 24 | i=$((i+1)) 25 | done 26 | echo "Organization: Not Found!" -------------------------------------------------------------------------------- /usr/lib/telegram-bot/plugins/clients.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Shows MAC, IP address and any hostname info for all connected wifi devices 4 | 5 | # list all wireless network interfaces 6 | # (for MAC80211 driver; see wiki article for alternative commands) 7 | for interface in `iw dev | grep Interface | cut -f 2 -s -d" "` 8 | do 9 | # for each interface, get mac addresses of connected stations/clients 10 | maclist=`iw dev $interface station dump | grep Station | cut -f 2 -s -d" "` 11 | # for each mac address in that list... 12 | for mac in $maclist 13 | do 14 | # If a DHCP lease has been given out by dnsmasq, 15 | # save it. 16 | ip="UNKN" 17 | host="" 18 | ip=`cat /tmp/dhcp.leases | cut -f 2,3,4 -s -d" " | grep $mac | cut -f 2 -s -d" "` 19 | host=`cat /tmp/dhcp.leases | cut -f 2,3,4 -s -d" " | grep $mac | cut -f 3 -s -d" "` 20 | # ... show the mac address: 21 | echo -e "$ip\t$host\t$mac" 22 | done 23 | done 24 | -------------------------------------------------------------------------------- /usr/lib/telegram-bot/plugins/wll_list.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # https://github.com/alexwbaule/telegramopenwrt/blob/master/plugins/wll_list 3 | for wifi in wlan0 wlan1 4 | do 5 | if [ "${wifi}" == "wlan0" ]; then 6 | echo -en "-----------------*${wifi} - 2.4 Ghz*-----------------------\n" 7 | else 8 | echo -en "-----------------*${wifi} - 5 Ghz*-------------------------\n" 9 | fi 10 | macaddr=$(iw dev ${wifi} station dump | grep Station | grep -oE "([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}" | awk '{printf $0 " "}') 11 | for lease in ${macaddr} 12 | do 13 | line=$(cat /tmp/dhcp.leases | grep ${lease}) 14 | if [ $? == 0 ]; then 15 | echo "${line}" | awk '{gsub( "*","\\*" ); gsub( "_","\\_" ); printf "Device: " $4 "\nIP: " $3 "\nMac: " toupper($2) "\nState: ";system("./functions/get_ping.sh "$4" 1");printf "\n"}' 16 | else 17 | cat /proc/net/arp | grep ${lease} | awk '{gsub( "_","\\_" ); printf "IP: " $1 "\nMac: " toupper($4) "\nState: ";system("./functions/get_ping.sh "$1" 1");printf "\n"}' 18 | fi 19 | ./functions/get_mac.sh "${lease}" 20 | echo -en "\n" 21 | done 22 | #echo -en "-----------------------------------------------------------\n" 23 | done -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Openwrt Telegram Bot 2 | 3 | Telegram bot for router with firmware Lede/Openwrt. 4 | 5 | ## Implemented functions (Commands via Telegram message) 6 | 7 | - */memory* return RAM info 8 | - */clients* connected clients 9 | - */wll_list* wifi clients 10 | - */wifi_list* wifi info 11 | - */reboot* reboot the device 12 | - */wol * wake on lan over the Internet 13 | - */wanip* WAN ip address 14 | - */* any scripts in the `plugins` directory 15 | 16 | ## Prerequisites 17 | 18 | Telegram bot run under Lede or Openwrt firmware than the first prerequisite is to have Lede/Openwrt installed. 19 | 20 | Second prerequisite is to have `curl` package installed. You can do this with command `opkg update && opkg install curl`. 21 | 22 | ## Installation Steps 23 | 24 | ### Step one: 25 | 26 | - Get your chat_id of Telegram. If you don't know what is your chat_id you can use bot @get_id_bot. 27 | 28 | - Get a bot token and start your bot. If you don't know how get it you can use bot @BotFather. Send him /newbot command , name of your new bot and a username. Get the returned string "Use this token to access the HTTP API:" 29 | 30 | ### Step two: 31 | 32 | Copy the files of this repo under `/` directory of your Lede/Openwrt system. 33 | 34 | Set files as executable with commands: 35 | 36 | ```sh 37 | chmod +x -R /usr/lib/telegram-bot/* /usr/lib/telegram-bot/functions/* 38 | chmod +x /etc/init.d/telegram_bot 39 | service telegram_bot enable 40 | ``` 41 | 42 | ### Step three: 43 | 44 | Set your variables (bot token and chat id) in `telegram_bot` file under `/etc/config/` dir. 45 | 46 | ```sh 47 | uci set telegram_bot.config.bot_token='[PUT YOUR BOT TOKEN HERE]' 48 | uci set telegram_bot.config.chat_id='[PUT YOUR CHAT ID HERE]' 49 | 50 | uci commit telegram_bot 51 | ``` 52 | 53 | Start `telegram_bot` service with commands: 54 | 55 | ```sh 56 | service telegram_bot restart 57 | ``` 58 | 59 | Enjoy your bot! 60 | -------------------------------------------------------------------------------- /usr/lib/telegram-bot/telegram_bot: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | PATH='/usr/bin:/usr/sbin:/bin:/sbin' 4 | 5 | token=$(uci get telegram_bot.config.bot_token 2>/dev/nul) 6 | chat_id=$(uci get telegram_bot.config.chat_id 2>/dev/nul) 7 | server=$(uci get telegram_bot.config.server 2>/dev/nul) 8 | timeout=$(uci get telegram_bot.config.timeout 2>/dev/nul) 9 | polling_time=$(uci get telegram_bot.config.polling_time 2>/dev/nul) 10 | 11 | if [ -z "$token" ]; then 12 | echo "token is empty" 13 | exit 2 14 | fi 15 | 16 | if [ -z "$chat_id" ]; then 17 | echo "chat_id is empty" 18 | exit 2 19 | fi 20 | 21 | if [ -z "$server" ]; then 22 | server="https://api.telegram.org" 23 | fi 24 | 25 | if [ -z "$timeout" ]; then 26 | timeout=30 27 | fi 28 | 29 | if [ -z "$polling_time" ]; then 30 | polling_time=1 31 | fi 32 | 33 | api="${server}/bot${token}" 34 | 35 | if [ ! -d "/tmp/telegram-bot" ]; then 36 | mkdir /tmp/telegram-bot 37 | fi 38 | 39 | plugins="/usr/lib/telegram-bot/plugins" 40 | #telegram_log_file=/tmp/telegram-bot/telegram.log 41 | telegram_log_file=/dev/null 42 | offset_file=/tmp/telegram-bot/telegram_offset 43 | 44 | command_not_found="Sorry, 45 | I only recognize *commands*. 46 | Commands are words beginning with a slash. 47 | Try it ! 48 | Send [/start](/start) to get my commands list." 49 | 50 | command_start="*/memory* return RAM info 51 | */clients* connected clients 52 | */wll_list* wifi clients 53 | */wifi_list* wifi info 54 | */reboot* reboot the device 55 | */wol * wake on lan over the Internet 56 | */wanip* WAN ip address 57 | */* any scripts in the \`plugins\` directory" 58 | 59 | curl -k -s -X POST $api/sendMessage -d chat_id=$chat_id -d parse_mode=Markdown --data-urlencode text="Router started." &> $telegram_log_file 60 | 61 | if [ -f "$offset_file" ] ; then 62 | offset=$( cat $offset_file ) 63 | else 64 | offset=0 65 | echo $offset > $offset_file 66 | fi 67 | 68 | reply_to_msg () { 69 | local msg_id=$1 70 | local origin=$2 71 | eval local text="$3" 72 | curl -k -s -X POST $api/sendMessage -d reply_to_message_id=$msg_id -d chat_id=$origin -d parse_mode=Markdown --data-urlencode text="$text" &> $telegram_log_file 73 | } 74 | 75 | while [ true ] 76 | do 77 | updates=$(curl -s -k -X GET ${api}/getUpdates?offset=${offset}&timeout=${timeout}) 78 | status=$(jsonfilter -s "$updates" -e $.ok) 79 | if [ $status = 'true' ]; then 80 | update_ids=$(jsonfilter -s "$updates" -e $.result[*].update_id) 81 | for update_id in $update_ids 82 | do 83 | offset=$((update_id+1)) 84 | echo $offset > $offset_file 85 | origin=$(jsonfilter -s "$updates" -e "$.result[@.update_id=$update_id].message.chat.id") 86 | msg_id=$(jsonfilter -s "$updates" -e "$.result[@.update_id=$update_id].message.message_id") 87 | command=$(jsonfilter -s "$updates" -e "$.result[@.update_id=$update_id].message.text") 88 | is_a_cmd=$(jsonfilter -s "$updates" -e "$.result[@.update_id=$update_id].message.entities[*].type") 89 | query_ans=$(jsonfilter -s "$updates" -e "$.result[@.update_id=$update_id].callback_query.id") 90 | origin_ans=$(jsonfilter -s "$updates" -e "$.result[@.update_id=$update_id].callback_query.message.chat.id") 91 | if [[ "$origin" != "$chat_id" && "$origin_ans" != "$chat_id" ]];then 92 | curl -k -s -X POST $api/sendMessage -d reply_to_message_id=$msg_id -d chat_id=$origin -d parse_mode=Markdown --data-urlencode \ 93 | text="This is a Private bot. If you want to implement one for you, check this out https://github.com/ixiumu/openwrt-telegram-bot" &> $telegram_log_file 94 | curl -k -s -X POST $api/leaveChat -d chat_id=$origin &> $telegram_log_file 95 | else 96 | if [ $(echo "$is_a_cmd" | grep "bot_command") != "" ]; then 97 | 98 | cmd=$(echo $command | awk '{print $1}') 99 | prms=$(echo $command | awk '{$1="";print $0}' | sed 's/^[ \t]*//g') 100 | parms=${prms//[\"\&\;\\\>\<\|]/} 101 | DATE=`date +%Y-%m-%d_%H:%M:%S` 102 | RETURN_TEXT="" 103 | case "$cmd" in 104 | ("/start") 105 | reply_to_msg $msg_id $origin "\${command_start}" 106 | ;; 107 | ("/reboot") 108 | (echo "Call $cmd" | logger -t "telegram_bot" -p daemon.info)& 109 | RETURN_TEXT="Command executed!" 110 | reply_to_msg $msg_id $origin "\${RETURN_TEXT}" && sleep 5 && reboot 111 | ;; 112 | ("/wol") 113 | (echo "Call $cmd [$parms]" | logger -t "telegram_bot" -p daemon.info)& 114 | RETURN_TEXT="$(${plugins}/wol.sh ${parms})" 115 | reply_to_msg $msg_id $origin "\${RETURN_TEXT}" 116 | ;; 117 | (*) 118 | if [ -f "${plugins}/${cmd}.sh" ] ; then 119 | (echo "Call $cmd [$parms]" | logger -t "telegram_bot" -p daemon.info)& 120 | RETURN_TEXT=$(${plugins}/${cmd}.sh ${parms}) 121 | else 122 | RETURN_TEXT="Command *${cmd}* not found!" 123 | fi 124 | 125 | reply_to_msg $msg_id $origin "\${RETURN_TEXT}" 126 | ;; 127 | esac 128 | 129 | else 130 | reply_to_msg $msg_id $origin "\${command_not_found}" 131 | fi 132 | 133 | fi 134 | done 135 | fi 136 | sleep $polling_time 137 | done& 138 | --------------------------------------------------------------------------------