-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain
More file actions
executable file
·96 lines (83 loc) · 3.25 KB
/
main
File metadata and controls
executable file
·96 lines (83 loc) · 3.25 KB
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
#!/usr/bin/env python3
import socket
import sys
from subprocess import DEVNULL, Popen, PIPE
from time import sleep, strftime, gmtime
from utils import is_pingable, is_avail_whois
from utils import send_report_mail
from utils import nmap
from utils import is_local_address
from logging import debug, error, info, warning, critical
import logging
logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)
try:
try_whois = Popen(["whois"], stdout=PIPE, stderr=DEVNULL)
info("WHOIS command was found!")
except FileNotFoundError:
critical("WHOIS command was not found. You need to install whois command on your distro.")
exit(10)
try:
try_nmap = Popen(["nmap"], stdout=PIPE, stderr=DEVNULL)
info("NMAP command was found!")
except FileNotFoundError:
critical("NMAP command was not found. You need to install nmap command on your distro.")
exit(10)
HOSTNAME_DNS = "8.8.8.8"
HOSTNAME_DNS_PORT = 80
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((HOSTNAME_DNS, HOSTNAME_DNS_PORT))
my_ip = s.getsockname()[0]
info("current host: " + str(my_ip))
s.close()
def check_complete_status(hostnames: tuple):
info("Starting scan all ips...")
mail_body = ""
for host in hostnames:
host = host.strip() # delete "\n" and other some shits in the strings
mail_body += f"\nPer l'hostname {host} : \n\n"
if not is_pingable(host):
error(f"{host} is not pingable")
mail_body += f"{host} is not pingable!\n"
continue
else:
mail_body += f"{host} is pingable!\n"
try:
name_host = socket.gethostbyname(host)
info("Name Host is : " + host + ": " + name_host)
mail_body += f"Name host {name_host} resolved : {name_host}\n"
except socket.gaierror: # If you can't ping, hostname won't exist...
name_host = host
error(f"Name host {name_host} can't be resolved!")
mail_body += f"Name host {name_host} can't be resolved!\n"
continue
# else, if the host is not local and the public domain is unknown send the msg
if is_avail_whois(host):
mail_body += f"{name_host} is AVAILABLE!\n"
error(host + " Status: is AVAILABLE.")
else:
mail_body += f"{name_host} is not AVAILABLE!\n"
if nmap(host):
mail_body += nmap(host)
if mail_body:
first_part = "Ecco il report del "+strftime("%Y-%m-%d %H:%M:%S", gmtime())+"\n\n"
send_report_mail(first_part + mail_body, "", "tommydzepina@gmail.com")
if __name__ == '__main__':
if len(sys.argv) < 2: # if you tipe nothing exit
print("Need more arguments")
exit(1)
elif len(sys.argv) == 2: # if third element is empty exit
host_to_check = (sys.argv[1])
check_complete_status(host_to_check)
exit(11)
elif len(sys.argv) == 3: # check right element
if sys.argv[1] != "-f":
print("the first is invalid.")
else:
file = open(sys.argv[2], "r") # read the files with the name of host
lines = file.readlines()
file.close()
check_complete_status(tuple(lines))
exit(0)
else:
print("Too many arguments")
exit(99)