python如何判断和对端Linux主机是网络通

我们经常要用Python脚本通过paramiko方式登录到远端Linux系统上去执行一些脚本操作,但是如果远端主机很多,这里我们最好可以判断一下本地和远端网络是否为通,通过一下方式可以实现判断网络是否为通。

 1 #!/user/bin/python
 2 import subprocess
 3 def ping (ip):
 4     cmd = 'ping -n 1 %s' %ip
 5     outfile = 'ping.tmp'
 6     ret = subprocess.call(cmd,shell=Ture,stdout = open(outfile,'a+'),stderr = subprocess.STOUT)
 7     if ret == 0:
 8         print "%s server is alive" %ip
 9     else
10         print "%s server is down" %ip
11 
12 if '__name__' == '__main__'
13     ping(192.1.1.1)

 

posted on 2015-12-01 22:01  rootmelon  阅读(1092)  评论(0编辑  收藏  举报

导航