If语句在使用nmap时不适用于Python

2024-05-15 18:02:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难让if语句正常工作。在iplist.txt的末尾有一个空行,我不想运行,但由于某些原因,它仍然在运行。我尝试删除文件的最后一行,但它只删除最后一个合法行而不是空白行,我甚至尝试删除空格、空行和空行,但它仍然会用一行行。最奇怪的部分是在IF块和其他块中都运行空白。

 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)

以下是iplist.txt的内容

10.9.10.38
10.9.10.45
10.9.11.10
#extra line
编辑

我试过了,但没有成功,我肯定我做错了

with open ("iplist.txt", "r") as file:
 filecontents = file.read()
 lines = file.readlines()
 lines = [x.strip() for x in lines]
 print("Creating list")
 for line in lines:
  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)

Tags: intxtaslineopenfilenametestingcommand