Syntax Error

#threading class
class ip_scrape(threading.Thread):
	def __init__(self, page):
		threading.Thread.__init__(self)
		self._stop_event = threading.Event()
		self.page = page
	
	def run (self) -> None:)
		global thread_count, bar_progress, api, query
		thread_count += 1
		while True:
			try:
				results = api.search(query, page=self.page)
				for result in results['matches']:
					with open("shodan_output.txt", "a") as f:
						f.write(result['ip_str'] + "\n")
				break
			except:
				time.sleep(delay)
				continue

		thread_count -= 1
		bar_progress += 1

==================================================================

#threading class
class ip_scrape(threading.Thread):
	def __init__(self, page):
		threading.Thread.__init__(self)
		self._stop_event = threading.Event()
		self.page = page
	
	def run (self) -> None:)
		global thread_count, bar_progress, api, query
		thread_count += 1
		while True:
			try:
				results = api.search(query, page=self.page)
				for result in results['matches']:
					with open("shodan_output.txt", "a") as f:
						f.write(result['ip_str'] + "\n")
				break
			except:
				time.sleep(delay)
				continue

		thread_count -= 1
		bar_progress += 1

==================================================================


[root@centos ~]# python shodan4.py
  File "shodan4.py", line 151
    def run(self) -> None:)
                          ^
SyntaxError: invalid syntax
[/quote]


[root@centos ~]# python shodan4.py
  File "shodan4.py", line 151
    def run(self) -> None:)


#threading class
class ip_scrape(threading.Thread):
	def __init__(self, page):
		threading.Thread.__init__(self)
		self._stop_event = threading.Event()
		self.page = page
	
	def run (self) -> None:)
		global thread_count, bar_progress, api, query
		thread_count += 1
		while True:
			try:
				results = api.search(query, page=self.page)
				for result in results['matches']:
					with open("shodan_output.txt", "a") as f:
						f.write(result['ip_str'] + "\n")
				break
			except:
				time.sleep(delay)
				continue

		thread_count -= 1
		bar_progress += 1

==================================================================

#threading class
class ip_scrape(threading.Thread):
	def __init__(self, page):
		threading.Thread.__init__(self)
		self._stop_event = threading.Event()
		self.page = page
	
	def run (self) -> None:)
		global thread_count, bar_progress, api, query
		thread_count += 1
		while True:
			try:
				results = api.search(query, page=self.page)
				for result in results['matches']:
					with open("shodan_output.txt", "a") as f:
						f.write(result['ip_str'] + "\n")
				break
			except:
				time.sleep(delay)
				continue

		thread_count -= 1
		bar_progress += 1

==================================================================


[root@centos ~]# python shodan4.py
  File "shodan4.py", line 151
    def run(self) -> None:)
                          ^
SyntaxError: invalid syntax


[root@centos ~]# python shodan4.py
  File "shodan4.py", line 151
    def run(self) -> None:)
                          ^
SyntaxError: invalid syntax

 ^
SyntaxError: invalid syntax

#threading class
class ip_scrape(threading.Thread):
def init (self, page):
threading.Thread. init (self)
self._stop_event = threading.Event()
self.page = page

def run (self) -> None:)
	global thread_count, bar_progress, api, query
	thread_count += 1
	while True:
		try:
			results = api.search(query, page=self.page)
			for result in results['matches']:
				with open("shodan_output.txt", "a") as f:
					f.write(result['ip_str'] + "\n")
			break
		except:
			time.sleep(delay)
			continue

	thread_count -= 1
	bar_progress += 1

==================================================================

[root@centos ~]# python shodan4.py
File “shodan4.py”, line 151
def run(self) - > None:)
^
SyntakError invalid syntak
this is what i am getting from this code below

class range_scrape(threading.Thread):
def init(self, ip):
threading.Thread.init(self)
self.ip = ip

def run(self):
	try:
		global thread_count
		thread_count += 1

		try:
			o = self.ip.split(".")
			rang = str(o[0])+ "." + str(o[1]) + "." + "0.0/16"
			
			with open("shodan_range_output.txt", "a") as f:
				if(rang != None and rang != ""):
					f.write("%s\n" %rang)
		except IndexError:
			pass

		thread_count -= 1
	except KeyboardInterrupt:
		pass
		return

#threading class
class ip_scrape(threading.Thread):
def init(self, page):
threading.Thread.init(self)
self._stop_event = threading.Event()
self.page = page

def run (self) -> None:)
	global thread_count, bar_progress, api, query
	thread_count += 1
	while True:
		try:

the ^ is showing up under the minus symbol not the def

Had you formatted your code correctly this would have been much easier, but this is not valid Python:

def run (self) -> None:)

Should be

def run(self) -> None:

yes i had that at first and i got same error

see …
#threading class
class ip_scrape(threading.Thread):
def init(self, page):
threading.Thread.init(self)
self._stop_event = threading.Event()
self.page = page

def run(self) -> None:
	global thread_count, bar_progress, api, query
	thread_count += 1
	while True:
		try:
			results = api.search(query, page=self.page)
			for result in results['matches']:
				with open("shodan_output.txt", "a") as f:
					f.write(result['ip_str'] + "\n")
			break
		except:
			time.sleep(delay)
			continue

	thread_count -= 1
	bar_progress += 1

Then please re-post your current code, making sure to correctly format it for the forums (as explained here). :slight_smile:

Indentation is very important in Python, and if you just paste your program into the forums it’s lost and we have to guess whether you got it right or not.

4 Likes

def load_options():
global api_key, search, port, country, org, resultsn, delay, ranges

try:
	f = open('shodan_settings.json',) 
	data = json.load(f) 
except:
	print("Failed to open shodan_settings.json !")
	exit()

try:
	delay = data['shodan_settings']['delay'] 
	ranges = data['shodan_settings']['ranges'] 
	api_key = data['shodan_settings']['api'] 
	resultsn = data['shodan_settings']['results'] 
	search = data['shodan_settings']['search'] 
	port = data['shodan_settings']['port'] 
	country = data['shodan_settings']['country'] 
	org = data['shodan_settings']['org']
except:
	print("Error, failed to load settings from config file")
	exit()

def test_api():
url = “https://api.shodan.io/api-info?key=” + api_key
ua = random.choice(user_agents)
headers = {
‘Accept’: ‘/’,
‘User-Agent’: ua
}

json_data = ""
try:
	r = requests.get(url, headers=headers)
	json_data = json.loads(r.text)
except Exception as e:
	print("Error, invalid api !")
	exit(0)

for i in json_data:
	if(i == "error"):
		print("Error, your api is rate limited")
		exit()
	if(i == "query_credits"):
		credits = int(json_data["query_credits"])
		if(credits <= 0 or credits < pages):
			print("Error, not enought credits !")
			exit()
		else:
			print("Credits: " + str(credits) + "\n")

#status bar
class statusbar(threading.Thread):
def init(self, toolbar_width=40, total=0, delay=0.5):
threading.Thread.init(self)
self.toolbar_width = toolbar_width

	if(total == 0):
		print("Error, you haven't set the total !")
		sys.exit(-1)
	self.total = total
	self.delay = 0.5

	self.percentage = 0
	self.bar_progress = 0
	
def update(self):
	try:
		global bar_progress

		ppp = bar_progress / self.total
	except:
		print("Failed to parse global variable: bar_progress !")
		sys.exit(-1)

	percentage = round(ppp, 2) * 100
	
	self.percentage = int(percentage)
	self.bar_progress = int(self.toolbar_width * self.percentage / 100) - 1

def draw(self):
	sys.stdout.write('\r')
	sys.stdout.write("Progress: [" +  ("=" * self.bar_progress) + ">" + (" " * (self.toolbar_width - self.bar_progress)) + "] " + str(self.percentage) + "%")
	sys.stdout.flush()

def complete(self):
	sys.stdout.write('\r')
	sys.stdout.write("Progress: [" +  ("=" * self.toolbar_width) + "] 100% ")
	sys.stdout.flush()

	sys.exit(0)

def run(self):
	while(self.percentage < 100):
		self.update()
		self.draw()
		time.sleep(self.delay)
	self.complete()

class range_scrape(threading.Thread):
def init(self, ip):
threading.Thread.init(self)
self.ip = ip

def run(self):
	try:
		global thread_count
		thread_count += 1

		try:
			o = self.ip.split(".")
			rang = str(o[0])+ "." + str(o[1]) + "." + "0.0/16"
			
			with open("shodan_range_output.txt", "a") as f:
				if(rang != None and rang != ""):
					f.write("%s\n" %rang)
		except IndexError:
			pass

		thread_count -= 1
	except KeyboardInterrupt:
		pass
		return

#threading class
class ip_scrape(threading.Thread):
def init(self, page):
threading.Thread.init(self)
self._stop_event = threading.Event()
self.page = page

def run(self) -> None:
	global thread_count, bar_progress, api, query
	thread_count += 1
	while True:
		try:
			results = api.search(query, page=self.page)
			for result in results['matches']:
				with open("shodan_output.txt", "a") as f:
					f.write(result['ip_str'] + "\n")
			break
		except:
			time.sleep(delay)
			continue

	thread_count -= 1
	bar_progress += 1

def build_search_query():
global search, query, port, org, pages
query = search
if(country != “”):
query += ’ country:"’ + country + ‘"’
if(port > 0):
query += ’ port:"’ + str(port) + ‘"’
if(org != “”):
query += ’ org:"’ + org + ‘"’
return query

def remove_dups(file):
with open(file, “r”) as f:
lines = set(f.readlines())
time.sleep(0.10)
with open(file, “w”) as f:
for line in lines:
f.write(line)

def display_message():
print("\n")
print(" /$$$$$$ /$$ /$$ “)
print(” /$$__ $$| $$ | $$ “)
print(”| $$ \/| $$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$$ “)
print(”| $$$$$$ | $$
$$ /$$__ $$ /$$__ $$ |____ $$| $$__ $$ “)
print(” \____ $$| $$ \ $$| $$ \ $$| $$ | $$ /$$$$$$$| $$ \ $$ “)
print(” /$$ \ $$| $$ | $$| $$ | $$| $$ | $$ /$$__ $$| $$ | $$ “)
print(”| $$$$$$/| $$ | $$| $$$$$$/| $$$$$$$| $$$$$$$| $$ | $$ “)
print(” \/ |/ |/ \/ \/ \/|/ |/ “)
print(” “)
print(” “)
print(” “)
print(” /$$$$$$ “)
print(” /$$__ $$ “)
print(”| $$ \/ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ “)
print(”| $$$$$$ /$$
_/ /$$ $$|
$$ /$$__ $$ /$$__ $$ /$$__ $$ “)
print(” \____ $$| $$ | $$ \/ /$$$$$$$| $$ \ $$| $$$$$$$$| $$ \/ “)
print(” /$$ \ $$| $$ | $$ /$$__ $$| $$ | $$| $$/| $$ “)
print(”| $$$$$$/| $$$$$$$| $$ | $$$$$$$| $$$$$$$/| $$$$$$$| $$ “)
print(” \
/ \/|__/ \/| $$/ \____/|/ “)
print(” | $$ “)
print(” | $$ “)
print(” |
/ “)
print(”\n\nShodanScraper 4.2:\nStarting scraping…\n")

#check dependencies
try:
import shodan
except ImportError:
print(“Missing module shodan: trying to install\n”)
os.system(‘pip3 install shodan’)
time.sleep(1)
exit(0)

try:
import requests
except ImportError:
print(“Missing module shodan: trying to install\n”)
os.system(‘pip3 install request’)
time.sleep(1)
exit(0)

load_options()

if(api_key == “” or api_key == None or api_key == “API”):
print(“Error: you haven’t set the api !”)
exit()

if(resultsn == None or resultsn == 0):
print(“Error: you haven’t set the number of results”)
exit()
if(resultsn < 100):
print(“Error: results must be 100 at least”)
exit()

display_message()

query = build_search_query()

#check if the api is valid
test_api()
api = shodan.Shodan(api_key)

results = api.search(query)
print(‘Results found: {}’.format(results[‘total’]))

if(results == 0):
print(“Failed to find any result !”)
exit(-1)
if(resultsn > results[‘total’]):
resultsn = results[‘total’]

pages = int(resultsn / 100) + 1

thread_count = 0

bar_progress = 0
percentage = 0

#start status bar thread
statusbar(total=pages).start()

#pages
try:
for x in range(pages):
ip_scrape(x).start()
time.sleep(delay)

#remove dupe ips
while(thread_count > 0):
	time.sleep(1)
time.sleep(1)

print("\n\nRemoving duplicated ips & ipv6")
remove_dups("shodan_output.txt")
time.sleep(0.10)

#remove ipv6
os.system("egrep '(([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[0-9]{2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])' shodan_output.txt > temp_shodan.txt")
os.system("cp temp_shodan.txt shodan_output.txt; rm temp_shodan.txt")

#iprange scrape
print("Ip scraping finished !\n")

if(ranges == "yes" or ranges == "y" or ranges == "Y" or ranges == "YES" or ranges == "true" or ranges == "TRUE" or ranges == "T"):
	#anges
	with open("shodan_output.txt", "r") as f:
		ips = f.readlines()

	for ip in ips:
		range_scrape(ip).start()

	while(thread_count > 0):
		time.sleep(0.5)
	
	remove_dups("shodan_range_output.txt")
	time.sleep(0.10)

	print("Range scraping finsihed, quitting..\n")
	exit()
else:
	exit()

except KeyboardInterrupt:
pass
exit()

how about that is that better or am i still not doing correct for you