-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpelnet.py
118 lines (71 loc) · 3.28 KB
/
pelnet.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
import socket
import json
import sys
from senders import *
from constants import *
from commands import *
import re
with open("config.json" , 'r') as file:
conf = json.load(file)
IP = conf['ip']
PORT = conf['port']
ENCODING = conf['encoding']
MSG_LEN = conf['msg_len']
SERVER_INFO = (IP , PORT)
def main():
with socket.socket(socket.AF_INET , socket.SOCK_STREAM) as sock:
try:
sock.connect(SERVER_INFO)
print("connection established !!!")
message = ''
print("enter mode : ")
print(MODES)
message = input(CMD_BEG)
mode = message.strip('\n') == 'p'
if not mode:
welcome_message = sock.recv(MSG_LEN).decode(ENCODING).strip('\r\n')
print(welcome_message)
while True:
try:
message = input(CMD_BEG)
if message == 'openports':
beg = int(input('start from : '))
end = int(input('end :'))
ports = scan_ports(IP ,beg , end)
print('\n'.join([str(port) for port in ports]))
continue
if mode:
if message == "quit":
simple_message_sender(sock , message ,ENCODING , MSG_LEN )
break
if message == 'telnet help':
print(HELP)
continue
parts = decompose_command(message)
msg = f' '.join(re.sub('\".*?\"',"" , message).split())
simple_message_sender(sock , msg , ENCODING , MSG_LEN)
res = sock.recv(MSG_LEN).decode(ENCODING)
if res == COMMAND_VALID:
args = re.findall('\".*?\"' , message)
arg = re.sub( '"','', re.findall('\".*?\"' , message)[0]) if len(args) else ""
sender = COMMANDS[parts['cmd']]['options'][parts['option']]['sender']
sender(sock , arg , ENCODING , MSG_LEN)
else:
request_sender(sock, message, ENCODING, MSG_LEN)
except NotACommandError as e:
print(str(e))
print('invalid command \n type "telnet help" to see all commnds')
except BrokenPipeError:
print("connection finished")
exit()
if __name__ == '__main__':
if len(sys.argv) == 3:
args = sys.argv
IP = args[1] if args[1] else IP
PORT = int(args[2]) if args[2] else PORT
SERVER_INFO = (IP , PORT)
try:
main()
except ConnectionRefusedError as e:
print('connection refused !!!')
exit()