Набросал чутка, пробуйте.
import re
from collections import defaultdict
ips = defaultdict(list)
regular = re.compile(r'Host: ([\d\.]+).+?Ports: (\d+)/')
with open('res.txt', 'r') as f:
for line in f:
line = line.strip()
if not line.startswith('#'):
ip, port = regular.search(line).groups()
ips[ip].append(port)
for k, v in ips.items(): # Выведет:
print(k, ', '.join(v)) # 192.168.1.1 80, 801
# 192.168.1.2 801, 445
# 192.168.1.3 80, 8080, 21
with open('outputfile.txt', 'w') as f: # Запишет тоже самое
for k, v in ips.items():
f.write('{} {}\n'.format(k, ', '.join(v)))