使用python怎么检测主机的连通性

  介绍

本篇文章给大家分享的是有关使用python怎么检测主机的连通性,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

python是什么意思

python是一种跨平台的,具有解释性,编译性,互动性和面向对象的脚本语言,其最初的设计是用于编写自动化脚本,随着版本的不断更新和新功能的添加,常用于用于开发独立的项目和大型项目。

<强>目录结构

ping_test/├──本   │,├──ping.py   │,├──ping_run.sh.origin   │,└──ping.sh   ├──相依   │,└──ip.lst   ├──,日志   │,├──10.10.37.196_2017-06-28_ping.log   │,└──10.10.62.229_2017-06-28_ping.log   └──README。md

<>强代码

猫bin/平。py

# !/usr/bin/env  python   # - *安康;编码:utf - 8      得到subprocess  import  Popen,管   import  shlex   import 时间   import  datetime   import 系统,操作系统      时间=basedir  os.path.dirname (, os.path.dirname (, os.path.abspath (__file__),),)   时间=cnf  os.path.join (basedir,大敌;& # 39;相依# 39;,,& # 39;ip.lst& # 39;,)   #,print  cnf      while 真正的:=,,today  datetime.datetime.strftime (, datetime.datetime.now (),“Y % - % - % d",)   ,with 开放(cnf), as  f:   for 才能;host  f:拷贝   ,,host =, host.strip ()   ,,cmd =, & # 39; sh  ping.sh  % & # 39; %,主机   ,,args =, shlex.split (cmd)   ,,p =, Popen (args, stdout=管,stderr=管)   ,,,标准输出,stderr =, p.communicate ()      ,,filename =, host  +, & # 39; _ % s_ping.log& # 39; %,今天   ,,logfile =, os.path.join (basedir, & # 39;日志# 39;,,文件名)   ,,#,print 日志文件      ,,if  stdout:   ,,,with 开放(日志文件,& # 39;ab # 39;), as  fd:   ,,,,fd.write (, stdout )   ,,,,fd.flush ()   ,,elif  stderr:   ,,,print (& # 39; ping 失去# 39;)   之前,time . sleep (1)

猫平。sh

# !/bin/bash      主机=1美元   ping  -c  1, ${主机},|,grep  & # 39; bytes 从# 39;,|,awk  & # 39; {print  $ 0“\ t", strftime (“% T  % F",, systime ())} & # 39;

以上就是使用python怎么检测主机的连通性,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。

使用python怎么检测主机的连通性