-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
145 lines (108 loc) · 4.38 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import platform
import shelve
import subprocess
import config
import tools.utils as ut
from bot import BotHandler
__author__ = ' Ilya Sosnovskiy - https://github.com/pashcovich'
__version__ = 0.1
bot = BotHandler(config.TG_TOKEN)
def start(chat_id):
bot.send_message(chat_id, 'Hello here!')
def show_help(chat_id):
bot.send_message(chat_id, '/ping [host]\n''/port_check [host] [port]\n')
def handle_text(chat_id, msg):
# исключительно для теста
shel = shelve.open(config.SHELVE_DB)
if msg in shel:
bot.send_message(chat_id, shel[msg])
shel.close()
else:
bot.send_message(chat_id, 'Your msg received.')
def ping_host(chat_id, args=None):
if args is None:
bot.send_message(chat_id, 'host must be passed')
else:
if len(args) == 1:
h = args[0]
if h == '127.0.0.1':
bot.send_message(chat_id, 'ha ha, very funny, ping yourself')
else:
ping_args = "-n 2 -w 1 " if platform.system().lower() == "windows" else "-c 2"
ping_str = "ping " + " " + ping_args + " " + h
need_sh = False if platform.system().lower() == "windows" else True
ping_response = subprocess.call(ping_str, stdout=subprocess.PIPE, shell=need_sh)
if ping_response == 0:
bot.send_message(chat_id, 'Host <b>' + str(h) + '</b> is UP.', parse_mode="HTML")
else:
bot.send_message(chat_id, 'Host <b>' + str(h) + '</b is DOWN.', parse_mode="HTML")
def who_host(chat_id, args):
pass
def port_check(chat_id, args=None):
if args is None:
bot.send_message(chat_id, 'host and port must be passed')
else:
if len(args) == 2:
h, p = args[0], args[1]
if ut.knock(h, int(p)):
bot.send_message(chat_id, 'Port <b>' + str(p) + '</b> on <b>' + str(h) + '</b> is open.',
parse_mode="HTML")
else:
bot.send_message(chat_id, 'Port <b>' + str(p) + '</b> on <b>' + str(h) + '</b> is closed or filtered.',
parse_mode="HTML")
else:
bot.send_message(chat_id, 'host and port must be passed')
def handle_command(chat_id, command, args=None):
# print(command)
if command[1:] == 'start':
start(chat_id)
elif command[1:] == 'help':
show_help(chat_id)
elif command[1:] == 'ping':
ping_host(chat_id, args)
elif command[1:] == 'dns':
who_host(chat_id, args)
elif command[1:] == 'port_check':
port_check(chat_id, args)
elif command[1:] == 'check_sudo':
if chat_id in config.ADMIN_LIST: # users id in ADMIN_LIST and chat_id must both be the same type e.x. INT
bot.send_message(chat_id, "You are the superuser!")
else:
bot.send_message(chat_id, "You are not in the superusers list!")
else:
bot.send_message(chat_id, "I don't know such command")
def main():
new_offset = None
try:
while True:
bot.get_updates(new_offset)
lu = bot.get_last_update()
if lu is not None:
lu_id = lu['update_id']
if "message" in lu:
lc_id = lu['message']['chat']['id']
if 'text' in lu['message']:
lm_txt = lu['message']['text']
lm_split_txt = lm_txt.split()
if lm_txt[0] == "/":
if len(lm_split_txt) > 1:
handle_command(lc_id, lm_split_txt[0], lm_split_txt[1:])
else:
handle_command(lc_id, lm_split_txt[0])
else:
lm_txt = ''
if 'entities' in lu['message']:
pass
else:
handle_text(lc_id, lm_txt)
# пока не знаю зачем, но будем хранить сообщения
shel = shelve.open(config.SHELVE_DB)
shel[str(lu_id)] = lm_txt
shel.close()
new_offset = lu_id + 1
except KeyboardInterrupt:
print('interrupted!')
if __name__ == '__main__':
main()