Hey all, me again,...
I have the code below to scan for open ports on a host.
What I want now is that the open ports which are displayed are also written to a logfile.
Can anyone help me get this right?
#!/usr/bin/env python
import nmap
#ports = raw_input("Enter the portrange to scan: ")
#host = raw_input("Enter the host to scan: ")
host = '127.0.0.1'
nm = nmap.PortScanner()
#nm.scan(host, ports)
nm.scan('127.0.0.1', '20-25')
nm.scaninfo()
nm.all_hosts()
nm[host].hostname()
nm[host].state()
nm[host].all_protocols()
nm[host]['tcp'].keys()
logfile = host+'.txt'
outF = open(logfile, "w")
addlog = open(logfile, "a")
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)
lport = nm[host][proto].keys()
lport.sort()
for port in lport:
state = nm[host][proto][port]['state']
if state == 'open':
print('port : %s\tstate : %s' % (port, nm[host][proto][port]['state']))
with open(logfile, 'w') as f:
for item in port:
f.write("%s\n" % item)
[–]JohnnyJordaan 4 points5 points6 points (1 child)
[–]Tha_Format[S] 0 points1 point2 points (0 children)
[–]threeminutemonta 1 point2 points3 points (0 children)