import os
import requests
import json
access_token = ''
ip_list = []
def login():
"""
输入用户米密码 进行登录操作
:return: 访问口令 access_token
"""
user = raw_input('[-] input : username :')
passwd = raw_input('[-] input : password :')
data = {
'username': user,
'password': passwd
}
data_encoded = json.dumps(data)
try:
r = requests.post(url='https://api.zoomeye.org/user/login', data=data_encoded)
r_decoded = json.loads(r.text)
global access_token
print "ACCESS Response"
print r_decoded
access_token = r_decoded['access_token']
except Exception, e:
print '[-] info : username or password is wrong, please try again '
exit()
def saveStrToFile(file, str):
"""
将字符串写如文件中
:return:
"""
with open(file, 'w') as output:
output.write(str)
def saveListToFile(file, list):
"""
将列表逐行写如文件中
:return:
"""
s = '\n'.join(list)
with open(file, 'w') as output:
output.write(s)
def apiTest():
"""
进行 api 使用测试
:return:
"""
page = 1
global access_token
with open('access_token.txt', 'r') as input:
access_token = input.read()
headers = {
'Authorization': 'JWT ' + access_token,
}
while (True):
try:
query_keywords = "gpms"
r = requests.get(url='https://api.zoomeye.org/host/search?query=' + query_keywords + '&facet=app,os&page=' + str(page),
headers=headers)
r_decoded = json.loads(r.text)
for x in r_decoded['matches']:
print x['ip']
ip_list.append(str(x['ip']) + " 80")
except Exception, e:
if str(e.message) == 'matches':
break
else:
print '[-] info : ' + str(e.message)
else:
if page == 10:
break
page += 1
def main():
if not os.path.isfile('access_token.txt'):
print '[-] info : access_token file is not exist, please login'
login()
saveStrToFile('access_token.txt', access_token)
apiTest()
saveListToFile('ip_list.txt', ip_list)
if __name__ == '__main__':
main()