-
Notifications
You must be signed in to change notification settings - Fork 2
/
sheachanddelete.py
106 lines (101 loc) · 3.78 KB
/
sheachanddelete.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
import qualysapi
import sys
from lxml import objectify
import logging
logging.basicConfig()
logger = logging.getLogger('logger')
#def conection():
# a = qualysapi.connect('config.ini')
def titulo():
print ("******************************************************")
print (" ")
print (" Vulnerabilty Management Team ")
print (" ")
print ("******************************************************")
class search:
## se define el constructor
def __init__(self,ip):
self.ip = ip
def search_ip(self):
try:
a = qualysapi.connect('config.ini') #conexion a la API de Qualys
reportScans = a.request('/api/2.0/fo/asset/host/',{'action':'list','details':'All','ips':(self.ip)})
#print (reportScans)# Activar solo para troubleshoting
root = objectify.fromstring(reportScans.encode('utf-8'))
print(root.RESPONSE.HOST_LIST.HOST)
except AttributeError:
print ('\n'"++++++++++++++++++++++++++++++++++++++++"'\n')
print ("host "+ self.ip + " is not on Qualys")
print ('\n'"++++++++++++++++++++++++++++++++++++++++"'\n')
else:
for host in root.RESPONSE.HOST_LIST.HOST:
print ("++++++++++++++++++++++++++++++++++++++++"'\n')
print ("Server Information "'\n')
print ("----------------------------------------")
print ("IP: "+host.IP.text)
try:# Validar DNS
print ("DNS: "+host.DNS.text)
except AttributeError:
print ("No DNS")
print ("OS: "+host.OS.text)
try:# Validar hostname
print ("NETBIOS: "+host.NETBIOS.text)
except AttributeError:
print ("No Netbios")
print ("ID: "+host.ID.text)
try:# validad ultimo scan
print ("LAST VULN SCAN: "+host.LAST_VULN_SCAN_DATETIME.text)
except AttributeError:
print ("No last vuln info")
print ("----------------------------------------")
print ('\n'"++++++++++++++++++++++++++++++++++++++++"'\n')
#return host.IP.text
def delete_ip(self):
try:
a = qualysapi.connect('config.ini')#conexion a la API de Qualys
#reportScans = a.request('/api/2.0/fo/asset/host/',{'action':'purge','echo_request':'1','ips':(self.ip)})
reportScans = a.request('/api/2.0/fo/asset/host/',{'action':'purge','ips':(self.ip)})
### solo para debuging
#print (reportScans)
root = objectify.fromstring(reportScans.encode('utf-8'))
print(root.RESPONSE.BATCH_LIST.BATCH.ID_SET.ID)
except AttributeError:
print ("++++++++++++++++++++++++++++++++++++++++"'\n')
print("Error Code " + root.RESPONSE.BATCH_LIST.BATCH.CODE.text)
print("Description "+root.RESPONSE.BATCH_LIST.BATCH.TEXT.text + '\n')
print ("++++++++++++++++++++++++++++++++++++++++")
else:
for host in root.RESPONSE.BATCH_LIST.BATCH:
print(host.TEXT.text)
print(host.ID_SET.ID.text)
def add_ip(self):
a = qualysapi.connect('config.ini')#conexion a la API de Qualys
reportScans = a.request('/api/2.0/fo/asset/ip/',{'action':'add','details':'All','ips':(self.ip),'enable_vm':'1'})
### Valida argumentos
### se llama la funcion titulo
titulo()
### se valida los datos de entrada -s or -d
if len(sys.argv) != 1:
if sys.argv[1] == "-s":
### se llama la clase search y se le pasa el argumento
cs = search(sys.argv[2])
cs.search_ip()
elif sys.argv[1] == "-d":
answ = raw_input("Do you want to delete this host? " +sys.argv[2]+ ", y " "or" " n "'\n')
if answ == "y":
### se llama la funcion delete_ip y se le pasa el argumento
cs = search(sys.argv[2])
cs.delete_ip()
#print("Deleted")
## se cancela cuando es n
elif answ == "n":
print ("Action Cancelled")
## se cancela si no es escribe n o y
elif answ != ["y","n"]:
print ("Action cancelled [Input Erro]")
elif sys.argv[1] != ["-s","-d"]:
# se valida el uso de -s or -d
print("You have to use -s (Search) or -d (Delete)")
else:
## se valida que se escriba una ip
print("please add valid IP address")