shell和python批量ping

#####baidu ip #######
220.181.38.148
220.181.38.133
192.168.1.10

 

 

 

#!/usr/bin/python
# -*- coding:utf-8 -*-
import os
import sys,re
import subprocess

 

#定义正则,排除以#开头的行

res = r'([^#])'

#判断文件是否存在
if os.path.isfile('/root/iplist.txt'):

#打开文件
with open('/root/iplist.txt', 'r') as f:
for line in f.readlines():

#开始匹配
m = re.match(res,line)
if m:

#匹配到就执行ping操作
tt = subprocess.call('ping -c 10 -w 10 %s' %line, stdout=subprocess.PIPE,shell=True)

if tt :
print ('%s' %line.rstrip(),",","down",",",1)
else:
print ('%s' %line.rstrip(),",","up",",",0)

 

 

 

 

 

#!/bin/bash
for i in `cat iplist.txt |grep -v "#" `

do

#echo $i
ping -c 10 -w 10 $i &> /dev/null
if [ "$?" == "0" ];then

echo $i","up","0

else

echo $i","down","1

fi

#ping -c 10 $i |grep loss|awk '{print $6}'|awk -F "%" '{print $1}'`
#if [ $ping -eq 100 ];then
#echo $i,up
#else
#echo $i,down
#fi

done

posted @ 2020-09-02 11:30  chengxuyonghu  阅读(179)  评论(0编辑  收藏  举报