· 6 years ago · Mar 31, 2020, 07:50 AM
1import time
2import shodan
3import os
4import shutil
5import colored
6from selenium.webdriver.firefox.options import Options
7from selenium import webdriver
8import requests
9import re
10options = Options()
11options.add_argument('--headless')
12
13
14# Variables
15nombrecarpeta = "IPTV-Jonirulah"
16ListaIP = []
17ListaDefinitiva = []
18green = colored.fg(40)
19white = colored.fg(231)
20yellow = colored.fg(228)
21blue = colored.fg(32)
22red = colored.fg(9)
23
24
25# Creación Carpeta con streams.m3u
26if os.path.exists(nombrecarpeta):
27 print(str(yellow) + "INFO: " + str(green) + "El directorio " + str(nombrecarpeta) + " ya existe.")
28 shutil.rmtree(nombrecarpeta)
29 os.mkdir(nombrecarpeta)
30else:
31 print(str(yellow) + "INFO: " + str(green) + "Creando carpeta " + str(nombrecarpeta))
32 os.mkdir(nombrecarpeta)
33os.chdir(nombrecarpeta)
34
35
36# API Key
37apikey = "DCYjpY0wARRzw35PKGOtC46ZGrcs9cOW"
38api = shodan.Shodan(apikey)
39
40
41# API Lookup
42results = api.search('port:"9981" HTS/tvheadend country:"ES" org:"Vodafone Spain"')
43for result in results['matches']:
44 IP=''.format(result['ip_str'])
45 ListaIP.append('{}'.format(result['ip_str']))
46Total = len(ListaIP)
47print(str(yellow) + "INFO: " + str(blue) + "Se han encontrado " + str(red) + str(Total) + str(blue) + " resultados!\n")
48print(ListaIP)
49
50# Comprobación Servidores Activos
51print(str(yellow) + "INFO: " + str(green) + "Comprobación de Servidores!" + str(yellow) + "\n" + str(yellow) + "INFO: " + "Servidores a comprobar: " + str(blue) + str(Total) + "\n")
52for i in ListaIP:
53 try:
54 requests.get("http://" + str(i), timeout=0.4)
55 ListaDefinitiva.append(i)
56 print(str(yellow) + "INFO: " + str(white) + "El host " + str(i) + " está funcionando!")
57 except:
58 print(str(yellow) + "INFO: " + str(white) + "No se ha podido conectar con " + str(i))
59 TotalDef = len(ListaDefinitiva)
60print(str(yellow) + "\nINFO: " + str(green) + "SERVIDORES ACTIVOS: " + str(blue) + str(TotalDef) + str(white) + "/" + str(blue) + str(Total))
61print(str(yellow) + "INFO: " + str(green) + "LA DESCARGA DE FICHEROS ESTÁ EN CURSO, ESTO PUEDE LLEGAR A TARDAR UNOS MINUTOS!")
62
63# WebScrapping para pillar streams.m3u
64browser = webdriver.Firefox(options=options)
65for IP in ListaDefinitiva:
66 # Navegador URL y Mini-Timeout
67 URL = "http://" + str(IP)
68 try:
69 browser.get(URL)
70 browser.implicitly_wait(1)
71
72 # COMPROBACIÓN VERSIÓN HTML
73 streams = browser.find_elements_by_xpath("//li/a[@target='_blank']")
74 if len(streams) != 0:
75 version = "1"
76 else:
77 streams = browser.find_elements_by_xpath("//div[@class='bouquetIcons']/a[@target='_blank']")
78 if len(streams) != 0:
79 version = "2"
80 else:
81 version = "3"
82
83 # VERSIÓN 1 - HTML 1
84 if version == "1":
85 streams = browser.find_elements_by_xpath("//li/a[@target='_blank']")
86 #print("VERSION 1")
87 #print(streams)
88 link = browser.find_elements_by_xpath("//li/a[@target='_blank']/img")
89 os.mkdir(IP)
90 os.chdir(IP)
91 for i in range(len(link)):
92 stream = streams[i].get_attribute("href")
93 nombrefichero = link[i].get_attribute("title")
94 nombrefichero = re.sub(r'\W+', " ", str(nombrefichero)) + ".m3u"
95 archivo = requests.get(stream)
96 file = open(str(nombrefichero), "wb")
97 file.write(archivo.content)
98 file.close()
99 os.chdir("..")
100
101 # VERSION 2 - HTML 2
102 elif version == "2":
103 streams = browser.find_elements_by_xpath("//div[@class='bouquetIcons']/a[@target='_blank']")
104 #print("VERSION 2")
105 #print(streams)
106 link = browser.find_elements_by_xpath("//div[@class='bouquetIcons']/a[@target='_blank']/img")
107 os.mkdir(IP)
108 os.chdir(IP)
109 for i in range(len(link)):
110 stream = streams[i].get_attribute("href")
111 nombrefichero = link[i].get_attribute("title")
112 nombrefichero = re.sub(r'\W+', " ", str(nombrefichero)) + ".m3u"
113 archivo = requests.get(stream)
114 file = open(str(nombrefichero), "wb")
115 file.write(archivo.content)
116 file.close()
117 os.chdir("..")
118
119 # VERSION 3 - HTML 3
120 elif version == "3":
121 streams = browser.find_elements_by_xpath("/html/body/div/div[3]/div[2]/div/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr/td/a[@target='_blank']")
122 #print("VERSION 3")
123 #print(streams)
124 link = browser.find_elements_by_xpath("/html/body/div/div[3]/div[2]/div/table/tbody/tr/td[1]/table/tbody/tr/td/table/tbody/tr/td/a[@target='_blank']/img")
125 os.mkdir(IP)
126 os.chdir(IP)
127 for i in range(len(link)):
128 stream = streams[i].get_attribute("href")
129 nombrefichero = link[i].get_attribute("title")
130 nombrefichero = re.sub(r'\W+', " ", str(nombrefichero)) + ".m3u"
131 archivo = requests.get(stream)
132 file = open(str(nombrefichero), "wb")
133 file.write(archivo.content)
134 file.close()
135 os.chdir("..")
136
137 except:
138 print(str(yellow) + "INFO: " + str(white) + "No se ha podido conectar con " + str(IP))
139
140
141# TODOS LOS DERECHOS RESERVADOS
142file = open("ListaIP.txt", "wb")
143file.write(ListaIP)
144file.close()
145file = open("ListaIPDefinitiva.txt", "wb")
146file.write(ListaDefinitiva)
147file.close()
148# PACO SANZ TELNET INDUSTRIES
149browser.close()
150browser.quit()