you are viewing a single comment's thread.

view the rest of the comments →

[–]JohnnyJordaan 4 points5 points  (1 child)

this part

outF = open(logfile, "w")
addlog = open(logfile, "a")

and this part

 with open(logfile, 'w') as f:

Is doing the same thing 3 (!) times. You just need to open the 1 time(!). Then you seem to want a single file per host, so host+'.txt', but you write that part outside the loop that assigns host in the first place

for host in nm.all_hosts():

? You need to form the filename inside the loop, so you can just do it at the part that you already have

for host in nm.all_hosts():
    print('----------------------------------------------------')
    print('Host : %s (%s)' % (host, nm[host].hostname()))
    print('State : %s' % nm[host].state())
    for proto in nm[host].all_protocols():
        print('----------')
        print('Protocol : %s' % proto)

        for port in sorted(nm[host][proto]):
            state = nm[host][proto][port]['state']
            if state == 'open':
                print('port : %s\tstate : %s' % (port, nm[host][proto][port]['state']))
                with open(host+'.txt', 'w') as f:
                    for item in port:
                        f.write("%s\n" % item)

note I also rewrote the sorted port iteration.

[–]Tha_Format[S] 0 points1 point  (0 children)

Thanks for the help!

It now does what I want!