使用python檢測某網段已用ip和未使用的ip的方法

來源:互聯網
上載者:User
借鑒了前輩的部落格,然後自己加了很多東西。

其中用到了subprocess模組

>>> import subprocess

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

#擷取命令執行結果的返回碼,通過wait()函數

>>> 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

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.