判断计算机是否可以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()

 

posted on 2016-01-04 14:35  momingliu11  阅读(437)  评论(0编辑  收藏  举报