1

Estoy tratando de montar un bot telegram para gestionar mi servidor centOS.

Sigo el ejemplo mostrado en este blog

https://blog.angelinux-slack.net/2017/01/28/usar-un-bot-de-telegram-como-monitor-de-servidores-linux/

Le indico a la api de telegram donde se encuntra el archivo server.php

https://api.telegram.org/bot<token>/setwebhook?url=https://<url>.php

introducir la descripción de la imagen aquí

Doy permisos en /etc/sudoers para darle permiso al usuario donde colocas el bot (/var/www/html).

apache ALL = NOPASSWD: /usr/sbin/csf
apache ALL = NOPASSWD: /scripts/cphulkdwhitelist
apache ALL = NOPASSWD: /bin/grep
apache ALL = NOPASSWD: /bin/ls
apache ALL = NOPASSWD: /bin/ping
apache ALL = NOPASSWD: /usr/bin/top
apache ALL = NOPASSWD: /usr/bin/whois

Agrego el archivo xxx.php a la ruta /var/www/html

<?php
# author          Horacio Romero Mendez (angelos)
# License         Copyleft 2017
# since           28 de enero de 2017 12:36:09
# version         1.0

define('BOT_TOKEN', '561737869:AAGaCcAri...');
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');

//OBTENER INFORMACION
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"];
$text = $update['message']['text'];
$file = "$chatID/$chatID.txt";

if ($text == "")
    die();


//CREAR DIRECTORIO PARA ESTADOS
if (!file_exists($chatID)) :
    mkdir($chatID, 0700, true);
    fopen($file, 'w');
endif;


//COMANDOS
switch ($text):

    case "/apacheerrorlog":
        file_put_contents($file, '');
        $msg = shell_exec("tail /var/log/httpd/error_log");
    break;

    case "/eximsendmaillog":
        file_put_contents($file, '');
        $msg = shell_exec("/usr/bin/sudo grep cwd /var/log/exim_mainlog | grep -v /var/spool | awk -F\"cwd=\" '{print $2}' | awk '{print $1}' | sort | uniq -c | sort -n");
    break;

    case "/firewall":
        $status = "$text,0";
        file_put_contents($file, $status);
        $msg = "Escribe la ip que se pondrá en lista blanca";
    break;  

    case "/free":
        file_put_contents($file, '');
        $msg = shell_exec("free -m");
    break;

    case "/last":
        file_put_contents($file, '');
        $msg = shell_exec("last -20");
    break;

    case "/ls":
        $status = "$text,0";
        file_put_contents($file, $status);
        $msg = "Escribe la ruta a listar";
    break;  

    case "/ping":
        $status = "$text,0";
        file_put_contents($file, $status);
        $msg = "Escribe el dominio o IP";
    break;      

    case "/ps":
        $status = "$text,0";
        file_put_contents($file, $status);
        $msg = "Escribe el texto a filtrar en los procesos";
    break;  

    case "/start" : 
        file_put_contents($file, '');
        $msg = "Bienvenido a Linux Server Monitor";
    break;

    case "/top" : 
        file_put_contents($file, '');
        $msg = shell_exec("/usr/bin/sudo top -b -n 1 | head -n 15");
    break;

    case "/uptime":
        file_put_contents($file, '');
        $msg = shell_exec("uptime");
    break;

    case "/versions":
        file_put_contents($file, '');
        $cen = shell_exec("cat /etc/redhat-release");
        $des = shell_exec("ls -lct --time-style=+\"%F %T\" / | tail -1 | awk '{print $6, $7}'");
        $apa = shell_exec("/usr/sbin/apachectl -v | grep 'Server version:'");
        $php = phpversion();
        $mys = shell_exec("mysql -V");
        $msg = "S.O.: $cen $des";
        $msg.= "Apache: $apa";
        $msg.= "PHP: $php";
        $msg.= "\nMySQL: $mys";
    break;

    case "/w":
        file_put_contents($file, '');
        $msg = shell_exec("w");
    break;

    case "/whois":
        $status = "$text,0";
        file_put_contents($file, $status);
        $msg = "Escribe el dominio a buscar";
    break;

    default:
        //SI YA SE ESTÁ EJECUTANDO UN COMANDO
        $st = explode(",", file_get_contents($file));

        switch ($st[0]):

            case "/firewall":
                $ip = $text;
                if (!filter_var($ip, FILTER_VALIDATE_IP) === false) :
                    $csf = shell_exec("/usr/bin/sudo /usr/sbin/csf -a $ip Bot consoluciones");
                    $hulk = shell_exec("/usr/bin/sudo /scripts/cphulkdwhitelist $ip");
                    $msg = "CSF: $csf\n";
                    $msg.= "CPHulk: $hulk";
                else :
                    $msg = "$ip no es una IP válida";
                endif;
                $msg.= "\nPuede escribir otra IP";
            break;

            case "/ls":
                $msg = shell_exec("/usr/bin/sudo ls -lh $text");
                $msg.= "\nPuede escribir otra ruta a listar";
            break;

            case "/ping":
                $msg = shell_exec("/usr/bin/sudo ping $text -c 4");
                $msg.= "\nPuede escribir otro dominio o IP";
            break;

            case "/ps":
                $msg = shell_exec("ps aux | grep $text");
                $msg.= "\nPuede escribir otra palabra para filtrar";
            break;

            case "/whois":
                $msg = shell_exec("/usr/bin/sudo whois $text");
                $msg.= "\nPuede escribir otro dominio a buscar";
            break;
        endswitch;

    break;
endswitch;

$sendto =API_URL."sendmessage?chat_id=".$chatID."&text=".urlencode($msg);
file_get_contents($sendto);
?>

Cuando hago cualquier petición en telegram no obtengo ninguna respuesta del servidor. Alguna idea de que puede estar mal.

Como podría comprobar que el bot se este conectando realmente a mi servidor centOS. Algún ejemplo tipo hola mundo.

1 respuesta 1

0

Yo te puedo compartir una solución pero esta en python, esta probado que ejecuta el comando ls y te lo regresa como mensaje en Telegram.

import telepot import sys import time import os from pprint import pprint reload(sys) sys.setdefaultencoding('utf-8') bot = telepot.Bot('561737869:AAGaCcAri...')

def handle(msg): pprint(msg)

def on_chat_message(msg): content_type, chat_type, chat_id = telepot.glance(msg) pprint(msg['text']) cmd = msg['text'] print cmd if cmd == "/start": bot.sendMessage(chat_id, "Inicio de crontab") if cmd == "/ls": f = os.popen('ls') cmd = f.read() bot.sendMessage(chat_id, cmd)

bot.message_loop({'chat': on_chat_message})

print('Listening ...')

while 1: time.sleep(10)

2
  • Ya lo solucioné, al parecer si usas un certificado ssl autofirmado tienes que mandar la clave publica a la api de telegram. el 4 abr. 2018 a las 6:58
  • Muy bien entonces es momento de dar por autosolucionada la pregunta. Saludos
    – pakoy3k
    el 4 abr. 2018 a las 16:59

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.