UnderwaterBaker
UnderwaterBaker

Reputation: 9

Don't understand why I'm getting a SyntaxError from user input

I'm following a tutorial on creating python script to automate nmap port scanning and it gives me a syntax error when I try to enter an IP address.

This is a python3.x script on Debian Linux and I've tried researching the problem but most answers are just to use raw_input() from python2. Which isnt too helpful because raw_input() is just input() in python3

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())

When you type an 192.168.1.1 for ip_addr it tells me SyntaxError: invalid syntax. When I type 192.168.1 for ip_addr, I get SyntaxError: unexpected EOF while parsing. but when I type 192.168 it goes on without error.

Upvotes: 0

Views: 832

Answers (1)

elethan
elethan

Reputation: 16993

The issue is that you appear be running your Python 3.x script in Python 2.x.

Try:

python3 my_script.py

And you should see a different result. Here is an interactive example:

enter image description here

Here are some docs for input() in Python 2 and 3. The two are deceptively similar:

Python 2: https://docs.python.org/2/library/functions.html#input

Python 3: https://docs.python.org/3.7/library/functions.html#input

In Python 3, input() will capture user input as a string. In Python 2 however, it will capture user input, and then evaluate the user input as a Python expression. Since the ip address you were entering is not a valid python expression, a syntax error was raised.

Upvotes: 2

Related Questions