-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnmap_portscan.py
More file actions
42 lines (37 loc) · 1.26 KB
/
nmap_portscan.py
File metadata and controls
42 lines (37 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Port scanning code using Nmap package
#code by -> payload
import nmap
import optparse
from socket import *
def nmapscan(tgtip,port):
try:
nmscan=nmap.PortScanner()
nmscan.scan(tgtip,port)
state=nmscan[tgtip]['tcp'][int(port)]['state']
try:
connSkt = socket(AF_INET, SOCK_STREAM)
connSkt.connect((tgtip, int(port)))
results = connSkt.recv(100)
print "[*]" + tgtip + " tcp/"+port +" "+state+" "+str(results)
except Exception ,e :
print "[-]Facing error:"+str(e)
except Exception,e:
print "Cant scan IP"+str(e)+" for port:"+str(port)+" either the ip is not rechable or no service is running"
return
def main():
parse=optparse.OptionParser("Program Usage -H <Target IP> -P <Target port[s]>")
parse.add_option('-H',dest='tgtip',type='string',help='Specify target ip')
parse.add_option('-P',dest='ports',type='string',help='Specify ports spearated by commas (,)')
(options,args)=parse.parse_args()
tgtip=options.tgtip
ports=str(options.ports).split(',')
if(tgtip==None)|(ports[0]==None):
print parse.usage
exit(0)
print "[!]Scanning ports using NMAP[!]"
for port in ports:
nmapscan(tgtip,port)
print "[-]Stopping Scan[-]"
#nmapscan()
if __name__=='__main__':
main()