Skip to content

Commit 4371049

Browse files
committed
Fixed and cleaned up some functions
Some minor changes only, no extra functions
1 parent 0fe6132 commit 4371049

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

censys.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
ssh = ['-s', '--ssh']
1212
invalid_env = 0
1313
protocol = []
14-
num_results = 0 # defaults to no results collected (Just query server)
15-
list_ips = 0 # If 1: print the list of IP's
16-
view_data = 0 # If 1: print the data associated with the IP's
14+
num_results = 0 # defaults to no results collected (Just query server)
15+
list_ips = 0 # If 1: print the list of IP's
16+
view_data = 0 # If 1: print the data associated with the IP's
1717
filename = ""
1818

1919
def helptext(): # just print the help text and quit
@@ -34,7 +34,7 @@ def search(num_results_): # query the database for number of results specified t
3434
pages = 1
3535
num_pages = 0
3636
if num_results_ >= 100:
37-
num_pages = (num_results_/100)
37+
num_pages = (num_results_/100) # Make sure the number of pages in the query are not excessive
3838
for x in range(0,len(protocol)): # For every protocol specified..
3939
for y in range(1,num_pages+2): # And the number of results specified..
4040
if pages >= y:
@@ -51,15 +51,16 @@ def search(num_results_): # query the database for number of results specified t
5151

5252
print "results returned: %i of type %s" % (num_results_, protocol[x]) # Status information to stdout
5353

54-
for y in range(0,num_results_): # Retrieve the content for the first [x] results to stdout
55-
content = get_content(results[y]['ip'],protocol[x])
56-
if len(filename) >= 1:
57-
f.write(content.encode("utf8"))
58-
if view_data == 1:
59-
print content
60-
61-
54+
if (filename != "" or view_data != 0):
55+
for y in range(0,num_results_): # Retrieve the content for the first [x] results to stdout
56+
content = get_content(results[y]['ip'],protocol[x])
57+
if len(filename) >= 1:
58+
f.write(content)
59+
if view_data == 1:
60+
print content
61+
6262
results = [] # Clean up results for this protocol
63+
6364
def query(page, protocol):
6465
query = 'location.province:More AND location.country_code:NO AND tags:' + protocol
6566
query_ = '{"query":"' + query + '", "page":' + str(page) + ', "fields":["ip", "tags"]}'
@@ -78,28 +79,27 @@ def get_content(ip_addr, _protocol): # get the content associated with the ip an
7879
details = ""
7980
res = requests.get(API_URL + "/view/ipv4/" + ip_addr, auth=(UID, SECRET))
8081
if res.status_code != 200:
81-
print "error occurred: %s" % res.json()["error"]
82-
sys.exit(1)
82+
print "Nothing returned on IP: " + ip_addr
8383

8484
if _protocol == "http":
8585
try:
86-
details = res.json()['ip'] + "\n" + res.json()['80']['http']['get']['title'] + "\n" + res.json()['80']['http']['get']['body'] + "\n"
86+
details = str(res.json()['ip']) + "\n" + res.json()['80']['http']['get']['title'] + "\n" + res.json()['80']['http']['get']['body'][300] + "\n"
8787
except:
88-
details = ip_addr, ": NO TITLE"
88+
details = ip_addr + ": NO TITLE\n"
8989

9090
if _protocol == "ftp":
9191
try:
92-
details = res.json()['ip'] + " :\n" + res.json()['21']['ftp']['banner']['banner'] + "\n"
92+
details = str(res.json()['ip']) + "\n" + res.json()['21']['ftp']['banner']['banner']
9393
except:
94-
details = ip_addr, ": NO TITLE"
94+
details = ip_addr + ": NO TITLE\n"
9595

9696
if _protocol == "ssh":
9797
try:
98-
details = res.json()['ip'] + " : " + res.json()['22']['ssh']['banner']['raw_banner']
98+
details = str(res.json()['ip']) + " " + res.json()['22']['ssh']['banner']['raw_banner']
9999
except:
100-
details = ip_addr, ": NO TITLE"
100+
details = ip_addr + ": NO TITLE\n"
101101

102-
return details
102+
return details.encode("utf8")
103103

104104

105105
try:

0 commit comments

Comments
 (0)