一旦我有主机名,比较文件 [英] Comparing files once I have hostname

查看:131
本文介绍了一旦我有主机名,比较文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来比较两个具有相同主机名的文件。我写了一个函数来解析主机名并将其保存在列表中。一旦我有了,我需要能够比较文件。



每个文件都在不同的目录中。



<第一步:从每个文件中检索主机名。
第二步:对两个目录中具有相同主机名的文件进行比较。



检索主机名代码:

  def hostname_parse(directory):
results = []
try:
for os.listdir (目录):
if filename.endswith(('。cfg','.startup','.confg')):
file_name = os.path.join(目录,文件名)
打开(file_name,r)作为in_file:
用于in_file中的行:
match = re.search('hostname \s(\S +)',line)
如果匹配:
results.append(match.group(1))
#print找到匹配
返回结果
除了IOError as(errno,strerror):
打印I / O错误({0}):{1}。格式(errno,strerror)
打印hostname_parse函数错误

示例数据

测试文件:

  19-30#

版本12.3
服务时间戳记调试日期时间msec
服务时间戳记日期时间msec
服务密码 - 加密

主机名19-30

boot-start-marker
boot-end-marker

ntp时钟周期17179738
ntp源码Loopback0

end

19-30#

在这种情况下,主机名是19-30。为了便于测试,我只使用了相同的文件,但将其修改为相同或不相同。



如上所述。我可以提取主机名,但现在正在寻找一种方法,然后根据找到的主机名比较文件。



它的核心是文件比较。但是能够看到特定的领域将是我想要完成的。对于初学者,我只是希望看到这些文件是相同的。区分大小写应该不重要,因为这些是具有相同格式的cisco生成的文件。这些文件的内容更重要,因为我正在寻找配置更改。

解决方案

以下是一些符合要求的代码你的要求。我无法测试,因此可能会遇到一些挑战。

  import hashlib 
使用散列库来计算文件内容的散列值import
import re

HOSTNAME_RE = re.compile(r'hostname +(\S +)')

def get_file_info_from_lines(filename,file_lines):
hostname = None
a_hash = hashlib.sha1()
用于file_lines中的行:
_hash.update(line.encode('utf-8'))
match = HOSTNAME_RE.match(line)
如果匹配:
hostname = match.group(1)
返回主机名,文件名,a_hash.hexdigest()

def get_file_info (文件名):
如果filename.endswith(('。cfg','.startup','.confg')):
打开(文件名,r)作为in_file:
返回os.listdir(目录)中的文件名get_file_info_from_lines(filename,in_file.readlines())

def hostname_parse(目录):
results = {}

info = get_file_info(文件名)
如果info不是None:
results [info [0]] = info
返回结果

result1 = hostname_parse('dir1')
results2 = hostname_parse(' dir2')

在results1.values()中的主机名,文件名,filehash:
如果results2中的主机名:
_,filename2,filehash2 = results2 [主机名]
如果filehash!= filehash2:
print(%s有变化(%s,%s)%(
hostname,filehash,filehash2))
print(filename)
print(filename2)
print()


I need a way to compare two files that have the same hostname in them. I have written a function that will parse out the hostnames and save that in a list. Once I have that I need to be able to compare the files.

Each file is in a different directory.

Step One: Retrieve "hostname" from each file. Step Two: Run compare on files with same "hostname" from two directories.

Retrieve hostname Code:

def hostname_parse(directory):
    results = []
    try:
        for filename in os.listdir(directory):
            if filename.endswith(('.cfg', '.startup', '.confg')):
                file_name = os.path.join(directory, filename)
                with open(file_name, "r") as in_file:
                    for line in in_file:
                        match = re.search('hostname\s(\S+)', line)
                        if match:
                            results.append(match.group(1))
                            #print "Match Found"
        return results
    except IOError as (errno, strerror):
        print "I/O error({0}): {1}".format(errno, strerror)
        print "Error in hostname_parse function"

Sample Data:

Test File:

19-30#
!
version 12.3
service timestamps debug datetime msec
service timestamps log datetime msec
service password-encryption
!
hostname 19-30
!
boot-start-marker
boot-end-marker
!
ntp clock-period 17179738
ntp source Loopback0
!
end

19-30#

In this case the hostname is 19-30. For ease of testing I just used the same file but modified it to be the same or not the same.

As stated above. I can extract the hostname but am now looking for a way to then compare the files based on the hostname found.

At the core of things it is a file comparison. However being able to look at specific fields will be what I would like to accomplish. For starters I'm just looking to see that the files are identical. Case sensitivity shouldn't matter as these are cisco generated files that have the same formatting. The contents of the files are more important as I'm looking for "configuration" changes.

解决方案

Here is some code to meet your requirements. I had no way to test, so it may have a few challenges. Is used hash lib to calculate a hash on the file contents, as a way to find changes.

import hashlib
import os
import re

HOSTNAME_RE = re.compile(r'hostname +(\S+)')

def get_file_info_from_lines(filename, file_lines):
    hostname = None
    a_hash = hashlib.sha1()
    for line in file_lines:
        a_hash.update(line.encode('utf-8'))
        match = HOSTNAME_RE.match(line)
        if match:
            hostname = match.group(1)
    return hostname, filename, a_hash.hexdigest()

def get_file_info(filename):
    if filename.endswith(('.cfg', '.startup', '.confg')):
        with open(filename, "r") as in_file:
            return get_file_info_from_lines(filename, in_file.readlines())

def hostname_parse(directory):
    results = {}
    for filename in os.listdir(directory):
        info = get_file_info(filename)
        if info is not None:
            results[info[0]] = info
    return results

results1 = hostname_parse('dir1')
results2 = hostname_parse('dir2')

for hostname, filename, filehash in results1.values():
    if hostname in results2:
        _, filename2, filehash2 = results2[hostname]
        if filehash != filehash2:
            print("%s has a change (%s, %s)" % (
                hostname, filehash, filehash2))
            print(filename)
            print(filename2)
            print()

这篇关于一旦我有主机名,比较文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆