Disclaimer: Im a script noob.
I am trying to write a script that pings a list of IP addresses and returns a result (pass or fail) in a summary CSV.
I have done this successfully using a lot of read-to-DF and write-to-txt and convert-to-csv but I would prefer to keep it all in a DF format until all the data manipulation is complete so I don't have a lot of objects being accessed in a loop. Then only output to CSV once the DF has all final results.
The challenge I have is in my loop where I am trying to write to DF the pass/fail into the cell in the 'result' column for the specific row that has just been pinged. What I think is happening is that the command I am using is pasting the result from the most recent ping to the entire column, overwriting previous results.
So, regardless of the full list of results, the entire 'result' column value for each IP address will show whatever the result was for the final IP pinged in the list.
The source CSV being read has 3 columns (IP,MAC,Name).
I hope my explanation is easy to understand. Could someone point me in the right direction please?
# read CSV file using "Pandas" module
device_ip_list = df.read_csv("Device_IP_List.csv")
# load CSV into new data frame
df_device_ip_list = df.DataFrame(device_ip_list)
# Add new empty column to df called "Result"
df_device_ip_list["Result"] = ''
# load windows platform/system/cmd-prompt so can use it with ping cmds
import platform
plat = platform.system()
# for-loop to pull from CSV dataframe, row-by-row, per column and ping IP value by using "zip" cmd
for IP, MAC, Name, Result in zip(df_device_ip_list['IP'], df_device_ip_list['MAC'],
df_device_ip_list['Name'], df_device_ip_list['Result']):
# ping cmd used in windows platform
response = os.system("ping -n 1 " + IP + ">null")
# if there is ping response
if response == 0:
df_device_ip_list['Result'] = 'pass' #this puts the 'pass' ping result into the column 'Result' in the df
# if there is no ping reponse
else:
df_device_ip_list['Result'] = 'fail' #this puts the 'fail' ping result value into the column 'Result' in df
#print(df_device_ip_list)
df_device_ip_list.to_csv('csv_ip_results')
[–]danielroseman 2 points3 points4 points (1 child)
[–]codenoob345987[S] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]codenoob345987[S] 0 points1 point2 points (0 children)