第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

以 ip:port 格式解析工作“masscan”的結果

以 ip:port 格式解析工作“masscan”的結果

萬千封印 2021-09-24 16:09:20
我開始學習編程,我正在嘗試編寫一個簡單的解析器,但我很困惑。如果有人幫助我,我會很高興。文件.txt# Masscan 1.0.3 scan initiated Sun Dec 23 23:00:31 2018# Ports scanned: TCP(1;80-80) UDP(0;) SCTP(0;) PROTOCOLS(0;)Host: 192.168.1.1 ()    Ports: 80/open/tcp////Host: 192.168.1.1 ()    Ports: 801/open/tcp////Host: 192.168.1.2 ()    Ports: 801/open/tcp////Host: 192.168.1.2 ()    Ports: 445/open/tcp////Host: 192.168.1.3 ()    Ports: 80/open/tcp////Host: 192.168.1.3 ()    Ports: 8080/open/tcp////Host: 192.168.1.3 ()    Ports: 21/open/tcp////# Masscan done at Sun Dec 23 23:00:45 2018我想以一種格式接收數據:192.168.1.1 80, 801192.168.1.2 801, 445192.168.1.3 80, 8080, 21腳本文件#!/usr/bin/env pythonimport sys, re, osports = []ip = Nonewith open('mres.txt','r') as f:for elem in f.readlines():    if not elem.startswith('#'):          if ip != None:              if elem.split(' ')[1] == ip:                  ports.append(elem.split(' ')[3].split('/')[0])                  continue              else:                  print(ip + str(ports))                  ports=[]          else:              #print('Unic: '+str(ip) + ' port: '+ str(elem.split(' ')[3].split('/')[0]))              ip = elem.split(' ')[1]              ports.append(elem.split(' ')[3].split('/')[0]) 
查看完整描述

2 回答

?
白衣染霜花

TA貢獻1796條經驗 獲得超10個贊

你最好使用dict來處理數據:1)IP可以是Dict中的key 2)List可以是Dict中的值。

如果需要,我可以為您創(chuàng)建示例代碼。


查看完整回答
反對 回復 2021-09-24
?
哆啦的時光機

TA貢獻1779條經驗 獲得超6個贊

這將操作您的數據,并打印您想要的輸出。我已經嘗試在下面的評論中盡可能地解釋它,但請?zhí)岢鋈魏尾磺宄膯栴}。


from collections import defaultdict

import socket


# build a dictionary of ip -> set of ports

# default dict is cool becasue if the key doesn't exist when accessed,

# it will create it with a default value (in this case a set)

# I'm using a set because I don't want to add the same port twice

ip_to_ports = defaultdict(set)


with open('mres.txt') as fp:

    for line in fp:

        # grab only the lines we're interested in

        if line.startswith('Host:'):

            parts = line.strip().split()

            ip = parts[1]

            port = parts[-1]

            # split it by '/' and grab the first element

            port = port.split('/')[0]


            # add ip and ports to our defaultdict

            # if the ip isn't in the defaultdict, it will be created with

            # an empty set, that we can add the port to.

            # if we run into the same port twice, 

            # the second entry will be ignored.

            ip_to_ports[ip].add(port)


# sort the keys in ip_to_ports by increasing address

for ip in sorted(ip_to_ports, key=socket.inet_aton):

    # sort the ports too

    ports = sorted(ip_to_ports[ip])

    # create a string and print

    ports = ', '.join(ports)

    print(ip, ports)


查看完整回答
反對 回復 2021-09-24
  • 2 回答
  • 0 關注
  • 293 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號