使用python检测某网段已用ip和未使用的ip的方法

高洛峰
发布: 2017-03-22 10:11:00
原创
1965人浏览过

借鉴了前辈的博客,然后自己加了很多东西。

其中用到了subprocess模块

>>> import subprocess

>>> p = subprocess.Popen('df -h',stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)

#获取命令执行结果的返回码,通过wait()函数

立即学习Python免费学习笔记(深入)”;

>>> p.wait()

0

#获取命令输出结果(标准输出),通过read()方法

>>> p.stdout.read()

'Filesystem            Size  Used Avail Use% Mounted on\n/dev/sda1              18G   11G  5.8G  65% /\ntmpfs                 495M     0  495M   0% /dev/shm\n'

#获取命令错误输出结果,通过read()方法

>>> p.stderr.read()            

''

#为空,说明没有错误输出

#获取错误输出

<subprocess.Popen object at 0x7f267528dbd0>

>>> p = subprocess.Popen('ls /etc/password',stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True,close_fds=True)

>>> p.stderr.read()

'ls: cannot access /etc/password: No such file or directory\n'

@获取错误输出的其他方法还有:read(),readline(),readlines(),close(),write()和writelines()等.

#!/usr/bin/env python

#_*_ coding:utf8 _*_

# by lijiajun

import re,subprocess,os,sys

net_region='192.168.3'

print("#########################################################")

print("#此脚本主要基于ping,测试某网段已用ip和未使用的ip       #")

print("#分别将其保存到/tmp/alive_ip.txt                        #")

print("#以及/tmp/dead_ip.txt                                   #")

print("#########################################################")

print(" ")

if os.path.isfile("/tmp/alive_ip.txt"):  

os.popen("mv /tmp/alive_ip.txt /tmp/alive_ip.txt.old")

print "you can see the used ip in this file : /tmp/alive_ip.txt"

if os.path.isfile("/tmp/dead_ip.txt"):  

os.popen("mv /tmp/dead_ip.txt /tmp/dead_ip.txt.old")

print "you can see the unused ip in this file : /tmp/dead_ip.txt"

print(" ")

dead_ip=0

无阶未来模型擂台/AI 应用平台
无阶未来模型擂台/AI 应用平台

无阶未来模型擂台/AI 应用平台,一站式模型+应用平台

无阶未来模型擂台/AI 应用平台 35
查看详情 无阶未来模型擂台/AI 应用平台

alive_ip=0

def check_alive(ip,count,timeout):

global alive_ip

global dead_ip

cmd='ping -c %d -w %d %s' % (count,timeout,ip)

p=subprocess.Popen(cmd,

stdin=subprocess.PIPE,

stdout=subprocess.PIPE,

stderr=subprocess.PIPE,

shell=True)

result=p.stdout.read()

regx=re.findall('100% packet loss',result)

if len(regx)==0:

print("\033[1;32;40m %s is UP \033[0m")  % (ip)

f=file('/tmp/alive_ip.txt','a')

f.write('%s\n' %ip)

f.close()

alive_ip=alive_ip+1

print "alive_ip count is %d" % alive_ip

return alive_ip

else:

print "\033[31m %s is DOWN\033[0m" % (ip)

                f=file('/tmp/dead_ip.txt','a')

                f.write('%s\n' %ip)

f.close()

dead_ip=dead_ip+1

print "dead_ip count is %d" % dead_ip

return dead_ip

if name=="main":

#f=file('/tmp/iplist.txt')

for i in range(1,255):

ip='%s.%s' % (net_region,i)

print ip

check_alive(ip,1,1)

print ("  ")

print "final dead_ip count is %d" % dead_ip

print "final alived_ip count is %d" % alive_ip

以上就是使用python检测某网段已用ip和未使用的ip的方法的详细内容,更多请关注php中文网其它相关文章!

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号