-1

i'm new in python, and i need to do this: if the software print in somewhere (443) the code stop and print (aborted).

My code is here:

import os

alvos = [
    'site1.com',
    'site2.com',
    'site3.com',
    'site4.com',
]

stopwhen = "443"

for alvo in alvos:
    msg = os.system('nmap ' + alvo + ' -F' )
    
    if stopwhen in msg:
      print("Stop")
      break

    else:
      continue

I got this error:

Starting Nmap 7.92 ( https://nmap.org ) at 2021-12-17 17:30 EST
Note: Host seems down. If it is really up, but blocking our ping probes, try -Pn
Nmap done: 1 IP address (0 hosts up) scanned in 3.30 seconds
Traceback (most recent call last):
  File "nmap-test1.py", line 15, in <module>
    if stopwhen in msg:
TypeError: argument of type 'int' is not iterable
Lucas Fernandes
  • 1,360
  • 2
  • 8
  • 12
  • 1
    From the [documentation](https://docs.python.org/3/library/os.html#os.system) for `os.system()`: _"On Unix, the return value is the exit status of the process encoded in the format specified for wait(). On Windows, the return value is that returned by the system shell after running command. The shell is given by the Windows environment variable COMSPEC: it is usually cmd.exe, which returns the exit status of the command run"_ You are expecting it to return the `stdout` of the command you ran, which is not correct – Pranav Hosangadi Dec 17 '21 at 22:35

1 Answers1

0

Python cannot iterate through integers, so searching for 'stopwhen' in something that is of type int won't work.

To check if msg is an int you can add: print(type(msg))

If it is, try converting it to a string using:

if stopwhen in str(msg):
    print('Stop')
    break

Edit:

To get the output of your nmap command you need to use a subprocess:

Python: How to get stdout after running os.system?

Your code might end up looking like this:

import subprocess

alvos = [
'site1.com',
'site2.com',
'site3.com',
'site4.com',
]

stopwhen = "443"

for alvo in alvos:
    msg = subprocess.check_output('nmap ' + alvo + ' -F', shell=True)

    if stopwhen in msg:
        print("Stop")
        break

   else:
       continue
Gamaray
  • 68
  • 1
  • 8