port scanning an IP range in python
<p>So I'm working on a simple port scanner in python for a class (not allowed to use the python-nmap library), and while I can get it to work when passing a single IP address, I can't get it to work using a range of IPs. </p>

<p>This is what I have:</p>

<pre><code>#!/usr/bin/env python
from socket import *
from netaddr import *

# port scanner
def port_scan(port, host)
   s = socket(AF_INET, SOCK_STREAM)
   try:
      s = s.connect((host, port))
      print "Port ", port, " is open"
   except Exception, e:
      pass

# get user input for range in form xxx.xxx.xxx.xxx-xxx.xxx.xxx.xxx and xx-xx
ipStart, ipEnd = raw_input ("Enter IP-IP: ").split("-")
portStart, portEnd = raw_input ("Enter port-port: ").split("-")

# cast port string to int
portStart, portEnd = [int(portStart), int(portEnd)]

# define IP range
iprange = IPRange(ipStart, ipEnd)

# this is where my problem is
for ip in iprange:
   host = ip
   for port in range(startPort, endPort + 1)
      port_scan(port, host)
</code></pre>

<p>So when I run the code, after adding print statements below</p>

<pre><code>host = ip
print host   # added
</code></pre>

<p>and then again after</p>

<pre><code>port_scan(port, host)
print port   # added
</code></pre>

<p>I end up with the following output:</p>

<pre><code>root@kali:~/Desktop/python# python what.py
Enter IP-IP: 172.16.250.100-172.16.250.104
Enter port-port: 20-22
172.16.250.100
20
21
22
172.16.250.101
20
21
22
...and so on
</code></pre>

<p>Thanks in advance everyone! 
I appreciate any help that I can get!</p>

<p><a href="https://i.stack.imgur.com/yDApd.png" rel="nofollow">code picture for reference, slightly different</a></p>

<p><a href="https://i.stack.imgur.com/HJF6d.png" rel="nofollow">output picture for reference</a></p>

<p>The problem turned out to be an issue with using the netaddr.IPRange, as suggested by @bravosierra99. </p>

<p>Thanks again everyone!</p>

python python-2.7 port-scanning