mercredi 16 janvier 2019

If Statement not working Python while using nmap

I'm having some trouble getting my if statement to work properly. At the end of the iplist.txt there is an empty line that I don't want to run, but for some reason, it's still running. I tried removing the last line of the file but it only removes the last legit line and not the blank line, I even tried removing spaces, empty lines, and null lines but it still would run with a line. And the weirdest part is that the blank runs both in the if block and the else block.

 with open ("iplist.txt", "r") as file:
     filecontents = file.read()
     for line in filecontents.split('\n'):
      filename = (line) + ".txt "
      command = "nmap -O -oG " + ".\\ips\\" + (filename) + (line)
      print(command)
      print(filename)
      strlen = int (len(filename))
      print(strlen)
      compareline = line[:4]
      print(compareline)
      if compareline == beginline: #beginline is declared as 10.9 earlier in the file
       print("Testing 1..2...")
       os.system(command)
       filenameforos = (line + ".txt")
       #detailedosdetection = open(filenameforos)
       #next(filecontents)
      else:
       print("Testing...")
       del line
       #next(StopIteration)

Here are the contents of iplist.txt

10.9.10.38
10.9.10.45
10.9.11.10
#extra line

Aucun commentaire:

Enregistrer un commentaire