我正在关注有关创建Python脚本以自动执行nmap端口扫描的教程,当我尝试输入IP地址时,它给我带来了语法错误。
这是Debian Linux上的python3.x脚本,我尝试研究此问题,但是大多数答案只是使用python2中的raw_input()
。哪个没有太大帮助,因为raw_input()
在python3中只是input()
import nmap
scanner = nmap.PortScanner()
print("Simple nmap automation tool")
print("<-------------------------------------------------->")
ip_addr = input("Ip to scan: ")
print("The IP address you entered is ", ip_addr)
type(ip_addr)
response = input(""" \nPlease enter the type of scan you want to run:
1.SYN ACK Scan
2.UDP scan
3.Comprehensive scan \nSelect Scan: """)
print("you have selected option ", response)
if response == "1":
print("Nmap Version ", scanner.nmap_version())
scanner.scan(ip_addr, "1-1024", "-V -sS")
print(scanner.scaninfo())
print("IP status: ", scanner[ip_addr].state())
print(scanner[ip_addr].all_protocols())
print("Open ports: ", scanner[ip_addr]["tcp"].keys())
当您为192.168.1.1
输入ip_addr
时,它会告诉我SyntaxError: invalid syntax
。当我为192.168.1
键入ip_addr
时,得到SyntaxError: unexpected EOF while parsing
。但是当我输入192.168
时,它继续进行,没有错误。
答案 0 :(得分:2)
问题是您似乎正在Python 2.x中运行Python 3.x脚本。
尝试:
python3 my_script.py
您应该看到不同的结果。这是一个交互式示例:
以下是Python 2和3中input()
的一些文档。二者在外观上相似:
Python 2:https://docs.python.org/2/library/functions.html#input
Python 3:https://docs.python.org/3.7/library/functions.html#input
在Python 3中,input()
将用户输入捕获为字符串。但是,在Python 2中,它将捕获用户输入,然后将用户输入评估为Python表达式。由于您输入的IP地址不是有效的python表达式,因此引发了语法错误。