端口扫描python中的IP范围

时间:2016-10-13 19:04:34

标签: python python-2.7 port-scanning

所以我在python中使用一个简单的端口扫描程序来创建一个类(不允许使用python-nmap库),虽然我可以在传递单个IP地址时让它工作,但我可以&# 39;使用一系列IP使其工作。

这就是我所拥有的:

#!/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)

所以当我运行代码时,在

下面添加print语句之后
host = ip
print host   # added

然后再次

port_scan(port, host)
print port   # added

我最终得到以下输出:

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 picture for reference, slightly different

output picture for reference

1 个答案:

答案 0 :(得分:0)

问题证明是使用netaddr.IPRange的问题,正如@ bravosierra99所建议的那样。

再次感谢大家!