-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketinfo.py
97 lines (72 loc) · 2.51 KB
/
socketinfo.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
import socket
import requests
import re
import threading
import subprocess
import pydig
import sys
from tabulate import tabulate
regex_ip = "^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$"
get_arg = sys.argv[1]
if len(sys.argv) > 2:
print('Too much arguments, only IP address or FQDN name')
sys.exit()
def ping_addr(address):
try:
ping_result = subprocess.run(f'ping -c 3 {address}', shell=True, capture_output=True).returncode
if ping_result == 0:
print(tabulate([('Address is responding to ping',)]))
else:
print(tabulate([('Address is not responding to ping',)]))
except Exception as e:
print(e)
def check_ssl(address):
try:
if requests.get(f'https://{address}', timeout=10).status_code == 200:
print(tabulate([('Connection is secure, HTTPS is responding',)]))
except:
print(tabulate([("There aren't any SSL",)]))
def whois_addr(address):
try:
print(tabulate([('WHOIS result', subprocess.getoutput(f'whois {address}'))]))
except Exception as e:
print(e)
def dig_address(address):
try:
a_record = pydig.query(f'{address}', 'A')
txt_record = pydig.query(f'{address}', 'TXT')
mx_record = pydig.query(f'{address}', 'MX')
ns_records = pydig.query(f'{address}', 'NS')
result_dict = {
'A record': a_record,
'TXT record': txt_record,
'MX record': mx_record,
'NS record': ns_records
}
print(tabulate(result_dict, headers=result_dict.keys()) + '\n')
except Exception as e:
print(e)
def run_tasks(data):
task1 = threading.Thread(target=ping_addr, args=(data,))
task2 = threading.Thread(target=check_ssl, args=(data,))
if not re.search(regex_ip, get_arg):
task3 = threading.Thread(target=whois_addr, args=(data,))
task4 = threading.Thread(target=dig_address, args=(data,))
task1.start()
task2.start()
if not re.search(regex_ip, get_arg):
task3.start()
task4.start()
task1.join()
task2.join()
if not re.search(regex_ip, get_arg):
task3.join()
task4.join()
if re.search(regex_ip, get_arg):
fqdn_addr = socket.getfqdn(f'{get_arg}')
print(tabulate([('FQDN name is', socket.getfqdn(f'{get_arg}'))]))
run_tasks(fqdn_addr)
else:
ip_result = [("IP Address is", socket.gethostbyname(f"{get_arg}"))]
print(tabulate(ip_result))
run_tasks(get_arg)