Skip to Content
Mastering Python for Networking and Security
book

Mastering Python for Networking and Security

by José Manuel Ortega
September 2018
Intermediate to advanced
426 pages
10h 46m
English
Packt Publishing
Content preview from Mastering Python for Networking and Security

Port-scanning with scapy

In the following example, we see that we have defined a analyze_port() function that has as parameters the host and port to analyze.

You can find the following code in the port_scan_scapy.py file:

from scapy.all import sr1, IP, TCPOPEN_PORTS = []def analyze_port(host, port): """ Function that determines the status of a port: Open / closed :param host: target :param port: port to test :type port: int """  print "[ii] Scanning port %s" % port res = sr1(IP(dst=host)/TCP(dport=port), verbose=False, timeout=0.2) if res is not None and TCP in res:     if res[TCP].flags == 18:         OPEN_PORTS.append(port)         print "Port %s open" % portdef main(): for x in xrange(0, 80):     analyze_port("domain", x) print "[*] Open ports:" for x in OPEN_PORTS: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Python for Networking and Security - Second Edition

Mastering Python for Networking and Security - Second Edition

José Manuel Ortega
Python for Cybersecurity

Python for Cybersecurity

Howard E. Poston, III

Publisher Resources

ISBN: 9781788992510Supplemental Content