0
import nmap3

def nmap_scan(address,nmapType):
    nmap = nmap3.Nmap()
    result = f'nmap.{nmapType}({address})'
    #result = nmap.nmapType(address) gives the error nmap type is not a member of class nmap
    print(result)

nmap_scan(127.0.0.1,scan_top_ports) #user input will be given here

What I want the code to do is take user input on the type of scan he wants from the python-nmap3 repo and use it as a variable in the result = f'nmap.nmapType(address)' line.

However, if I remove the f string, python treats nmapType variable as a method to the class nmap. What I want is the user to be able to specify the method name in the variable nmapType.

Is there a way around to solve this problem and use user input to call the correct nmap method based on user input?

I want the user to enter the scan type, pass that to nmap_scan function, and use it to perform the scan.

Axe319
  • 4,255
  • 3
  • 15
  • 31
  • Sorry, I have no idea what you're trying to say. Please try to write more clearly and use distinct sentences. – Karl Knechtel Nov 02 '21 at 09:50
  • what exactly is the error and seemingly it is telling you that you can't use `nmap.nmpaType` because that simply is not possible, did you read the docs? – Matiiss Nov 02 '21 at 09:52
  • 2
    Use `getattr` and pass `scan_top_ports` as a string. for example, `my_method = getattr(nmap, nmapType)` `my_method(address)`. To clarify, your function call should be `nmap_scan('127.0.0.1', 'scan_top_ports')` – Axe319 Nov 02 '21 at 09:53
  • nmap3 is a module I'm using in my code......I want the user to enter the type of scan he wants to perform on the target that I will pass on to the function call nmap_scan(127.0.0.1,scan_top_ports) and use the variable nmapType to do different types of scan on the target but the problem I'm getting is that I don't know how to tell python that nmapType is variable I want to use in result = nmap.nmapType(address) and that it is not a function of class nmap. So how can I do that in python? – Bhavik Malhotra Nov 02 '21 at 09:58

0 Answers0