判断计算机是否可以ping通
#!/usr/bin/python # -*- coding: utf-8 -*- import os of = open('servers.txt','r') ofw=open('servers_ping.txt','a') for f in of: server = f.split(',')[0] print server ping_result = os.popen('ping %s -c 1' % (server)).readlines() #convert list to string ping_result_s = ''.join(ping_result) #find string "Destination Host Unreachable" if ping_result_s.find('Destination Host Unreachable') <> -1: print '%s is not reached' % (server) else: print '%s is reached' % (server) ofw.write('%s is reached\n' % (server)) of.close() ofw.close()