[Tutor] Having issues with a simple port scanner using scapy

Alan Gauld learn2program at gmail.com
Mon Jun 28 15:51:35 EDT 2021


On 25/06/2021 22:34, Chris Verhalen via Tutor wrote:
> I’m new to python andI have no idea where I went wrong 

First, My apologies for the late posting.

As a new poster your message got held in te moderation queue but the
moderator(me!)
was on vacation and I just got back.

Also if posting code please post using plai8n text. Rich text or HTML
gets mangled in the mail system and loses all the formarting that is
so important in Python. See below...

If you can repost in plain text we might be able to help 9assuming you
still need it!)



> to get this port scanner using Scapy to work properly. Any help is appreciated.
>
> import loggingfrom scapy.layers.inet import TCP, ICMP, IPlogging.getLogger("scapy.runtime").setLevel(logging.ERROR) # Disable the annoying No Route found warning !from scapy.all import *ip = "10.0.0.3"closed_ports = 0open_ports = []def is_up(ip): #""" Tests if host is up """ icmp = IP(dst=ip)/ICMP() resp = sr1(icmp, timeout=10) if resp == None: return False else: return Trueif __name__ == '__main__': conf.verb = 0 # Disable verbose in sr(), sr1() methods start_time = time.time() ports = range(1, 1024) if is_up(ip): print("Host %s is up, start scanning" % ip) for port in ports: src_port = RandShort() # Getting a random port as source port p = IP(dst=ip)/TCP(sport=src_port, dport=port, flags='S') # Forging SYN packet resp = sr1(p, timeout=2) # Sending packet if str(type(resp)) == "<type 'NoneType'>": closed += 1 elif resp.haslayer(TCP): if resp.getlayer(TCP).flags == 0x12: send_rst = sr(IP(dst=ip)/TCP(sport=src_port, dport=port, flags='AR'), timeout=1) openp.append(port) elif resp.getlayer(TCP).flags == 0x14: closed += 1 duration = time.time()-start_time print("%s Scan Completed in %fs" % (ip, duration)) if len(openp) != 0: for opp in openp: print("%d open" % pop) print("%d closed ports in %d total port scanned" % (closed, len(ports)) else: print("Host %s is Down" % ip)
> line 47    else:    ^SyntaxError: invalid syntax
> Process finished with exit code 1
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list