简体   繁体   中英

Assigning MongoDB Array type value to Python Variable

I'm having a heck of a time trying to assign the Login and Password JSON parameters to a python variable using PyMongo. Here is the JSON stored in Mongo:

{"_id":"5a2700c9eb0a197ba6f8120d"
"protocol":"ssh"
"hpfeed_id":"5a2700c7eb0a197ba6f8120a"
"timestamp":"2017-12-05T20:25:43.922Z"
"source_ip":"37.221.198.242"
"session_ssh":{"version":"SSH-2.0-libssh2_1.4.3"}
"source_port":45557
"destination_port":22
"identifier":"da258962-c5b5-11e7-9c0a-1e7dbf5015ae"
"honeypot":"cowrie"
"auth_attempts":[{"login":"worker2","password":"worker2"}]}

在此处输入图片说明

The auth_attempts is an array type and I can not for the life of me figure out how to get it to a string in Python. Here is what I have been trying so far:

# Query the MongoDB for required data
def query():
    sessionData = session.find({})
    numOfSessions = session.find({}).count()
    pprint.pprint(session.find_one({'auth_attempts': { 'login': [0] }}))
    for item in sessionData:
       if (item['honeypot'] != 'p0f'):
            test = item({'auth_attempts': { '$in': [0] }})
            print(str(test))

Im guessing you would like a string something like:

"login: worker2, password:worker2"

From your code snippet your item is a dict. So to get the auth_attempts in string you should be able to do something like:

for item in sessionData:
    for auth in item.get('auth_attempts'):
        print('login: {}, password:{}'.format(auth.get('login'), auth.get('password'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM