2023-03-08 13:15:15 +00:00
|
|
|
import modules.server.server as server_mod
|
|
|
|
|
|
|
|
|
2023-09-17 09:42:39 +00:00
|
|
|
def ping_from_server(server_from: str, server_to: str, action: str) -> str:
|
2023-03-08 13:15:15 +00:00
|
|
|
stderr = ''
|
|
|
|
action_for_sending = ''
|
2023-09-17 09:42:39 +00:00
|
|
|
output1 = ''
|
2023-03-08 13:15:15 +00:00
|
|
|
|
|
|
|
if server_to == '':
|
2023-09-17 09:42:39 +00:00
|
|
|
return 'warning: enter a correct IP or DNS name'
|
2023-03-08 13:15:15 +00:00
|
|
|
|
|
|
|
if action == 'nettools_ping':
|
|
|
|
action_for_sending = 'ping -c 4 -W 1 -s 56 -O '
|
|
|
|
elif action == 'nettools_trace':
|
|
|
|
action_for_sending = 'tracepath -m 10 '
|
|
|
|
|
|
|
|
action_for_sending = action_for_sending + server_to
|
|
|
|
|
|
|
|
if server_from == 'localhost':
|
|
|
|
output, stderr = server_mod.subprocess_execute(action_for_sending)
|
|
|
|
else:
|
|
|
|
action_for_sending = [action_for_sending]
|
|
|
|
output = server_mod.ssh_command(server_from, action_for_sending, raw=1, timeout=15)
|
|
|
|
|
|
|
|
if stderr != '':
|
2023-09-17 09:42:39 +00:00
|
|
|
return f'error: {stderr}'
|
2023-03-08 13:15:15 +00:00
|
|
|
for i in output:
|
|
|
|
if i == ' ' or i == '':
|
|
|
|
continue
|
|
|
|
i = i.strip()
|
|
|
|
if 'PING' in i:
|
2023-09-17 09:42:39 +00:00
|
|
|
output1 += '<span style="color: var(--link-dark-blue); display: block; margin-top: -5px;">'
|
2023-03-08 13:15:15 +00:00
|
|
|
elif 'no reply' in i or 'no answer yet' in i or 'Too many hops' in i or '100% packet loss' in i:
|
2023-09-17 09:42:39 +00:00
|
|
|
output1 += '<span style="color: var(--red-color);">'
|
2023-03-08 13:15:15 +00:00
|
|
|
elif 'ms' in i and '100% packet loss' not in i:
|
2023-09-17 09:42:39 +00:00
|
|
|
output1 += '<span style="color: var(--green-color);">'
|
2023-03-08 13:15:15 +00:00
|
|
|
else:
|
2023-09-17 09:42:39 +00:00
|
|
|
output1 += '<span>'
|
|
|
|
|
|
|
|
output1 += i + '</span><br />'
|
2023-03-08 13:15:15 +00:00
|
|
|
|
2023-09-17 09:42:39 +00:00
|
|
|
return output1
|
2023-03-08 13:15:15 +00:00
|
|
|
|
|
|
|
|
2023-09-17 09:42:39 +00:00
|
|
|
def telnet_from_server(server_from: str, server_to: str, port_to: str) -> str:
|
2023-03-08 13:15:15 +00:00
|
|
|
count_string = 0
|
|
|
|
stderr = ''
|
2023-09-17 09:42:39 +00:00
|
|
|
output1 = ''
|
2023-03-08 13:15:15 +00:00
|
|
|
|
|
|
|
if server_to == '':
|
2023-09-17 09:42:39 +00:00
|
|
|
return 'warning: enter a correct IP or DNS name'
|
2023-03-08 13:15:15 +00:00
|
|
|
|
|
|
|
if server_from == 'localhost':
|
|
|
|
action_for_sending = f'echo "exit"|nc {server_to} {port_to} -t -w 1s'
|
|
|
|
output, stderr = server_mod.subprocess_execute(action_for_sending)
|
|
|
|
else:
|
|
|
|
action_for_sending = [f'echo "exit"|nc {server_to} {port_to} -t -w 1s']
|
|
|
|
output = server_mod.ssh_command(server_from, action_for_sending, raw=1)
|
|
|
|
|
|
|
|
if stderr != '':
|
2023-09-17 09:42:39 +00:00
|
|
|
return f'error: <b>{stderr[5:]}</b>'
|
2023-03-08 13:15:15 +00:00
|
|
|
|
|
|
|
for i in output:
|
|
|
|
if i == ' ':
|
|
|
|
continue
|
|
|
|
i = i.strip()
|
|
|
|
if i == 'Ncat: Connection timed out.':
|
2023-09-17 09:42:39 +00:00
|
|
|
return f'error: <b>{i[5:]}</b>'
|
|
|
|
output1 += i + '<br>'
|
2023-03-08 13:15:15 +00:00
|
|
|
count_string += 1
|
|
|
|
if count_string > 1:
|
|
|
|
break
|
2023-09-17 09:42:39 +00:00
|
|
|
return output1
|
2023-03-08 13:15:15 +00:00
|
|
|
|
2023-09-17 14:34:56 +00:00
|
|
|
|
2023-09-17 09:42:39 +00:00
|
|
|
def nslookup_from_server(server_from: str, dns_name: str, record_type: str) -> str:
|
2023-03-08 13:15:15 +00:00
|
|
|
count_string = 0
|
|
|
|
stderr = ''
|
2023-09-17 09:42:39 +00:00
|
|
|
output1 = ''
|
2023-03-08 13:15:15 +00:00
|
|
|
|
|
|
|
if dns_name == '':
|
2023-09-17 09:42:39 +00:00
|
|
|
return 'warning: enter a correct DNS name'
|
2023-03-08 13:15:15 +00:00
|
|
|
|
|
|
|
action_for_sending = f'dig {dns_name} {record_type} |grep -e "SERVER\|{dns_name}"'
|
|
|
|
|
|
|
|
if server_from == 'localhost':
|
|
|
|
output, stderr = server_mod.subprocess_execute(action_for_sending)
|
|
|
|
else:
|
|
|
|
action_for_sending = [action_for_sending]
|
|
|
|
output = server_mod.ssh_command(server_from, action_for_sending, raw=1)
|
|
|
|
|
|
|
|
if stderr != '':
|
2023-09-17 09:42:39 +00:00
|
|
|
return 'error: ' + stderr[5:-1]
|
2023-03-08 13:15:15 +00:00
|
|
|
|
2023-09-17 09:42:39 +00:00
|
|
|
output1 += f'<b style="display: block; margin-top:10px;">The <i style="color: var(--blue-color)">{dns_name}</i> domain has the following records:</b>'
|
2023-03-08 13:15:15 +00:00
|
|
|
for i in output:
|
|
|
|
if 'dig: command not found.' in i:
|
2023-09-17 09:42:39 +00:00
|
|
|
return 'error: Install bind-utils before using NSLookup'
|
2023-03-08 13:15:15 +00:00
|
|
|
if ';' in i and ';; SERVER:' not in i:
|
|
|
|
continue
|
|
|
|
if 'SOA' in i and record_type != 'SOA':
|
2023-09-17 09:42:39 +00:00
|
|
|
return '<b style="color: red">There are not any records for this type'
|
2023-03-08 13:15:15 +00:00
|
|
|
if ';; SERVER:' in i:
|
|
|
|
i = i[10:]
|
2023-09-17 09:42:39 +00:00
|
|
|
output1 += '<br><b>From NS server:</b><br>'
|
2023-03-08 13:15:15 +00:00
|
|
|
i = i.strip()
|
2023-09-17 09:42:39 +00:00
|
|
|
output1 += '<i>' + i + '</i><br>'
|
2023-03-08 13:15:15 +00:00
|
|
|
count_string += 1
|
2023-09-17 09:42:39 +00:00
|
|
|
|
|
|
|
return output1
|