title: Doing multiple actions in a for loop
body: <p>I am trying to write a small script to essentially check various IP addresses listed in an excel spreadsheet, and reference them with hostnames listed in the same excel spreadsheet. I have all of the ground work layed out but there are a lot of inconsistencies and things in my code I think I could do better.</p>
  <p>The 2 main problems I am having right now is that when I add in my if - else statements, all of the code breaks and the for loop tries to use the entire list as the input instead of doing a item in the list. The other problem is that the output of the gethostbyaddr, when it does work, comes out with the domain at the end leading to the hostnames not being exactly in the excel spreadsheet as it appears in python.</p>
  <pre><code>#Import Libraries Nessisary for NSLookup and Excel
  import socket
  import xlrd
  
  #Declare The Excel File to be Used at Runtime by User Input
  input_file = input(&quot;Enter the full address of the spreadsheet to be used: &quot;)
  excel_workbook = xlrd.open_workbook(input_file)
  
  #Declare The Sheet to be Used at Runtime by User Input and convert sheet number into &quot;Index Value&quot;
  input_sheet = int(input(&quot;Enter the sheet number you would like to select (1,2,3, etc...): &quot;))
  input_sheet = input_sheet - 1
  excel_sheet = excel_workbook.sheet_by_index(input_sheet)
  
  #Declare the First Column (Skip Header) as Hostname list to be used
  excel_hostname = excel_sheet.col_values(0, start_rowx=1, end_rowx=None)
  #Declare the Second Column (Skip Header) as ip address list to be used
  excel_ip_address = excel_sheet.col_values(1, start_rowx=1, end_rowx=None)
  
  #Create a For Loop consisting of every ip address in the Excel Spreadsheet
  for i,ip in enumerate(excel_ip_address):
      try:
          #Use an Equivelant NSLookup Command to Check the Current Spreadsheet Index ip for Hostname
          nslookup = socket.gethostbyaddr(ip)[0]
          #Use If - Else Statement to check for Excel Spreadsheet Inconsistencies
          if nslookup == excel_hostname:
              #If First NSLookup Matches Excel Spreadsheet, re-run to now Check for Multiple Entries
              nslookup2 = socket.gethostbyaddr(ip)[0]
              #Use Second If - Else Statement to check for Multiple DNS Entries
              if nslookup == nslookup2:
                  #If Second NSLookup Matches Excel Spreadsheet, re-run to now Check for Multiple Entries One Last Time
                  nslookup3 = socket.gethostbyaddr(ip)[0]
                  #Use Third If - Else Statement to check for Multiple DNS Entries One Last Time
                  if nslookup == nslookup3:
                      #If all Checks Have Been Passed, Proceed to the Next ip with a Message that this ip is ok
                      print(ip, &quot;No Anomalies Detected&quot;)
                  #Third If - Else Statement - Third Try to Resolve a Different Hostname in Case the First Two Times Brought the Same Result
                  else:
                      print(ip, &quot;-- Multiple DNS Entries Detected --&quot;,      &quot;Spreadsheet DNS is --&quot;, excel_hostname, &quot;Second DNS Entry is --&quot;, nslookup3)
              #Second If - Else Statement - If Hostname Matches the First If Statement but Changes the Second Time it Means there are Multiple DNS Entries 
              else:
                  print(ip, &quot;-- Multiple DNS Entries Detected --&quot;, &quot;Spreadsheet DNS is --&quot;, excel_hostname, &quot;Second DNS Entry is --&quot;, nslookup2)
          #First If - Else Statement - If Hostname does not Match Spreadsheet on First try, must mean spreadsheet inconsistencies
          else:
              print(excel_ip_address, &quot;-- Hostname does not match Excel Spreadsheet --&quot;, &quot;Spreadsheet --&quot;, excel_hostname, &quot;NSLookup --&quot;, nslookup)
      #Use a Try - Except to Catch any possible errors Such as Unreachable Host
      except socket.error:
          print(ip, &quot;An Error Occured with this Address&quot;)
  </code></pre>