首页 > 编程语言 >dhcpd.leases的Python解析程序

dhcpd.leases的Python解析程序

时间:2023-10-18 14:11:12浏览次数:35  
标签:return str Python timestamp else tokens dhcpd leases lease

#!/usr/local/bin/python3
import datetime, bisect

def parse_timestamp(raw_str):
       tokens = raw_str.split()

       if len(tokens) == 1:
               if tokens[0].lower() == 'never':
                       return 'never';

               else:
                       raise Exception('Parse error in timestamp')

       elif len(tokens) == 3:
               return datetime.datetime.strptime(' '.join(tokens[1:]),
                       '%Y/%m/%d %H:%M:%S')

       else:
               raise Exception('Parse error in timestamp')


def timestamp_is_ge(t1, t2):
       if t1 == 'never':
               return True

       elif t2 == 'never':
               return False

       else:
               return t1 >= t2


def timestamp_is_lt(t1, t2):
       if t1 == 'never':
               return False

       elif t2 == 'never':
               return t1 != 'never'

       else:
               return t1 < t2


def timestamp_is_between(t, tstart, tend):
       return timestamp_is_ge(t, tstart) and timestamp_is_lt(t, tend)


def parse_hardware(raw_str):
       tokens = raw_str.split()

       if len(tokens) == 2:
               return tokens[1]

       else:
               raise Exception('Parse error in hardware')


def strip_endquotes(raw_str):
       return raw_str.strip('"')


def identity(raw_str):
       return raw_str


def parse_binding_state(raw_str):
       tokens = raw_str.split()

       if len(tokens) == 2:
               return tokens[1]

       else:
               raise Exception('Parse error in binding state')


def parse_next_binding_state(raw_str):
       tokens = raw_str.split()

       if len(tokens) == 3:
               return tokens[2]

       else:
               raise Exception('Parse error in next binding state')


def parse_rewind_binding_state(raw_str):
       tokens = raw_str.split()

       if len(tokens) == 3:
               return tokens[2]

       else:
               raise Exception('Parse error in next binding state')


def parse_leases_file(leases_file):
       valid_keys = {
               'starts':               parse_timestamp,
               'ends':                 parse_timestamp,
               'tstp':                 parse_timestamp,
               'tsfp':                 parse_timestamp,
               'atsfp':                parse_timestamp,
               'cltt':                 parse_timestamp,
               'hardware':             parse_hardware,
               'binding':              parse_binding_state,
               'next':                 parse_next_binding_state,
               'rewind':               parse_rewind_binding_state,
               'uid':                  strip_endquotes,
               'client-hostname':      strip_endquotes,
               'option':               identity,
               'set':                  identity,
               'on':                   identity,
               'abandoned':            None,
               'bootp':                None,
               'reserved':             None,
               }

       leases_db = {}

       lease_rec = {}
       in_lease = False
       in_failover = False

       for line in leases_file:
               if line.lstrip().startswith('#'):
                       continue

               tokens = line.split()

               if len(tokens) == 0:
                       continue

               key = tokens[0].lower()

               if key == 'lease':
                       if not in_lease:
                               ip_address = tokens[1]

                               lease_rec = {'ip_address' : ip_address}
                               in_lease = True

                       else:
                               raise Exception('Parse error in leases file')

               elif key == 'failover':
                       in_failover = True
               elif key == '}':
                       if in_lease:
                               for k in valid_keys:
                                       if callable(valid_keys[k]):
                                               lease_rec[k] = lease_rec.get(k, '')
                                       else:
                                               lease_rec[k] = False

                               ip_address = lease_rec['ip_address']

                               if ip_address in leases_db:
                                       leases_db[ip_address].insert(0, lease_rec)

                               else:
                                       leases_db[ip_address] = [lease_rec]

                               lease_rec = {}
                               in_lease = False

                       elif in_failover:
                               in_failover = False
                               continue
                       else:
                               raise Exception('Parse error in leases file')

               elif key in valid_keys:
                       if in_lease:
                               value = line[(line.index(key) + len(key)):]
                               value = value.strip().rstrip(';').rstrip()

                               if callable(valid_keys[key]):
                                       lease_rec[key] = valid_keys[key](value)
                               else:
                                       lease_rec[key] = True

                       else:
                               raise Exception('Parse error in leases file')

               else:
                       if in_lease:
                               raise Exception('Parse error in leases file')

       if in_lease:
               raise Exception('Parse error in leases file')

       return leases_db


def round_timedelta(tdelta):
       return datetime.timedelta(tdelta.days,
               tdelta.seconds + (0 if tdelta.microseconds < 500000 else 1))


def timestamp_now():
       n = datetime.datetime.utcnow()
       return datetime.datetime(n.year, n.month, n.day, n.hour, n.minute,
               n.second + (0 if n.microsecond < 500000 else 1))


def lease_is_active(lease_rec, as_of_ts):
       return timestamp_is_between(as_of_ts, lease_rec['starts'],
               lease_rec['ends'])


def ipv4_to_int(ipv4_addr):
       parts = ipv4_addr.split('.')
       return (int(parts[0]) << 24) + (int(parts[1]) << 16) + \
               (int(parts[2]) << 8) + int(parts[3])


def select_active_leases(leases_db, as_of_ts):
       retarray = []
       sortedarray = []

       for ip_address in leases_db:
               lease_rec = leases_db[ip_address][0]

               if lease_is_active(lease_rec, as_of_ts):
                       ip_as_int = ipv4_to_int(ip_address)
                       insertpos = bisect.bisect(sortedarray, ip_as_int)
                       sortedarray.insert(insertpos, ip_as_int)
                       retarray.insert(insertpos, lease_rec)

       return retarray


##############################################################################


myfile = open('/var/lib/dhcp/dhcpd.leases', 'r')
leases = parse_leases_file(myfile)
myfile.close()

now = timestamp_now()
report_dataset = select_active_leases(leases, now)

print('+------------------------------------------------------------------------------')
print('| DHCPD ACTIVE LEASES REPORT')
print('+-----------------+-------------------+----------------------+-----------------')
print('| IP Address      | MAC Address       | Expires (days,H:M:S) | Client Hostname ')
print('+-----------------+-------------------+----------------------+-----------------')

for lease in report_dataset:
       print('| ' + format(lease['ip_address'], '<15') + ' | ' + \
               format(lease['hardware'], '<17') + ' | ' + \
               format(str((lease['ends'] - now) if lease['ends'] != 'never' else 'never'), '>20') + ' | ' + \
               lease['client-hostname'])

print('+-----------------+-------------------+----------------------+-----------------')
print('| Total Active Leases: ' + str(len(report_dataset)))
print('| Report generated (UTC): ' + str(now))
print('+------------------------------------------------------------------------------')

  

标签:return,str,Python,timestamp,else,tokens,dhcpd,leases,lease
From: https://www.cnblogs.com/ahuo/p/17771944.html

相关文章

  • Python3,3分钟,带你了解PyTorch,原来科学计算库也不是很难嘛。
    1、引言小屌丝:鱼哥,最近忙啥嘞?小鱼:啥也没干。小屌丝:确定没干??小鱼:…这话到你嘴里,咋就变为了。小屌丝:也没有啊,我就是确认下,你干没干。小鱼:…能干啥,你想干啥?小屌丝:我想请教你个问题。小鱼:正儿八经的问题,是不?小屌丝:你就看我今天这身穿的,还能不正经?小鱼:穿新鞋走老路小屌丝:此话咋......
  • Python3, 33行代码搞了一个聊天机器人, 这下再也不怕没人说话了。
    Python制作聊天机器人1、引言2、实战2.1准备2.2介绍2.2.1NLTK2.2.2ChatterBot2.3安装2.4示例2.4.1创建聊天机器人2.4.2与用户交互3、总结1、引言小屌丝:鱼哥,看这段代码fromchatterbotimportChatBot#创建聊天机器人chatbot=ChatBot('MyBot')#加载语料库with......
  • Python3,6行代码,搞定网络测速神器,我直接卸载某60测速器。
    6行代码搞定网络测速器1、引言2、代码实战2.1介绍2.1.1定义2.1.2常用方法2.1.3功能2.2安装2.3示例2.3.1测试上传下载速度2.3.2测试延迟2.3.3自定义服务器测试2.3.4多连接测试2.3.5实战3、总结1、引言小屌丝:鱼哥,你知道speedtest这个库吗?小鱼:嗯,知道一点点,咋了?小屌丝:那......
  • Python保留字有哪些?分为几类?
    保留字也称为关键字,这些保留字都被赋予了特殊含义,不能把保留字作为函数、模块、变量、类和其他对象的名称来使用。那么Python保留字有哪些?分为几类?以下是具体内容介绍。Python语言保留字是指在Python编程语言中,被保留不可用于变量名或函数名的标识符。这些保留字具有特定......
  • 调用Python的openpyxl包对Excel表格进行美化
    Python中运用openpyxl包对Excel表格进行美化,包括字体样式调整、单元格对齐方式调整、单元格边框调整、单元格背景颜色调整、行高和列宽调整。使用的Python中openpyxl包的版本为3.0.5先看实际美化前后的效果对比详细的开发代码如下,代码当中对关键信息进行了说明。复制代码......
  • 软件测试|深入理解Python的encode()和decode()方法
    简介在Python中,字符串是不可变的序列对象,它由Unicode字符组成。当我们需要在字符串和字节之间进行转换时,Python提供了两个非常重要的方法:encode()和decode()。这两个方法允许我们在Unicode字符和字节之间进行相互转换,以便在处理文本和二进制数据时更加灵活。在本文中,我们将深入......
  • 软件测试|Python字符串拼接详细解析
    简介在Python编程中,字符串拼接是一个非常常见的操作,它允许我们将多个字符串连接成一个新的字符串。字符串拼接在处理文本和数据时非常有用,比如构建消息、生成文件路径、格式化输出等。在本文中,我们将深入探讨Python中字符串拼接的不同方法和技巧。方法一:连续书写拼接在Python......
  • python 处理异步物化视图同时执行导致内存溢出问题
    python处理异步物化视图同时执行导致内存溢出问题一、前提:因为物化视图过多,同时物化视图到时间同时爆发,导致CPU爆满,所以采用datax自带的调度服务来执行python命令二、直接看代码:importpymysqlimportpymssqlimportdatetimeimporttimeclassMaterialized_plan:d......
  • python 操作向量数据库qdrant
    qdrant官网:https://qdrant.tech/documentation/overview/两个步骤:1、文本进行向量化2、连接qdrant进行存储步骤一:文本向量化文本向量化可以借助很多现有模型,个人使用bge-large-zhfromsentence_transformersimportSentenceTransformer#模型已下载到本地MODELBGE=Sen......
  • 【Python入门教程】CV2报错:cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-p
    ​     OpenCV作为一个强大计算机视觉库被各个领域广泛应用,今天分享下自己编程遇到的报错信息以及解决办法。1报错信息​[WARN:[email protected]]globalgrfmt_tiff.cpp:716cv::TiffDecoder::readDataOpenCVTIFF:TIFFRGBAImageOK:Sorry,cannothandleimageswith6......