0

だから私はクラスのpythonで単純なポートスキャナーに取り組んでいます(python-nmapライブラリの使用は許可されていません)。単一のIPアドレスを渡すときに動作させることはできますが、動作させることはできません一連の 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

よろしくお願いします!私が得ることができる助けに感謝します!

参照用のコード画像、わずかに異なる

参考までに出力画像

4

1 に答える 1

0

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

Thanks again everyone!

于 2016-10-14T15:48:23.863 に答える