ping using python and save to a file
This question already has an answer here:
Change both of
fp = open("C:UsersanudeepaDesktophostname.csv", 'w')
to
fp = open("C:UsersanudeepaDesktophostname.csv", 'a')
in order to open the file in append mode.
You can also improve your code by using with
, so you don't open the file every iteration:
import os
with open("C:UsersanudeepaDesktophostname.csv", 'a') as fp:
for i in range (0,255):
for j in range(1,254):
hostname = "10.222.{0}.{1}".format(i,j)
response = os.system ("ping -n 1 " + hostname)
if response == 0:
fp.writelines(hostname + "host upn")
else:
fp.write(hostname + "host deadn")
This will also have the benefit of closing the file when the script ends.
链接地址: http://www.djcxy.com/p/42410.html下一篇: 使用python进行ping并保存到文件