Skip to content

Instantly share code, notes, and snippets.

@mrtyler
Last active January 25, 2017 03:59
Show Gist options
  • Save mrtyler/c3a552e3ad6db149f7089fd4189aee83 to your computer and use it in GitHub Desktop.
Save mrtyler/c3a552e3ad6db149f7089fd4189aee83 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from collections import defaultdict
import re
from subprocess import CalledProcessError, check_output
import sys
IS_INTERESTING = re.compile("^[a-z0-9-.]+ ")
def main():
seen = defaultdict(list)
lines = sys.stdin.readlines()
for line in lines:
line = line.rstrip()
if is_interesting(line):
analyze_line(line, seen)
analyze_seen(seen)
def is_interesting(line):
# Look for actual inventory entries, not headers or variables.
#
# Uses a very dumb strategy that happens to work on all the inventory files
# I care about today :).
m = IS_INTERESTING.match(line)
return m is not None
def analyze_line(line, seen):
words = line.split()
try:
(vm_hostname, raw_vm_ip, raw_vm_mac) = words[0:3]
except ValueError:
print "Malformed line: %s" % line
return
for address in raw_vm_ip, raw_vm_mac:
address = address.split("=")[1]
seen[address].append(vm_hostname)
def analyze_seen(seen):
for address, hostnames in sorted(seen.items()):
if len(hostnames) > 1:
hostnames = sorted(hostnames)
print "======================================================================="
print "COLLISION! %s is used by: %s" % (address, ", ".join(hostnames))
for hostname in hostnames:
print "==========="
print "HOST %s" % hostname.upper()
try:
print check_output(["host", hostname])
except CalledProcessError as exc:
print "FAILED!"
print exc
print "==========="
print "IFCONFIG %s" % hostname.upper()
try:
print check_output("ssh %s /sbin/ifconfig" % hostname, shell=True)
except CalledProcessError as exc:
print "FAILED!"
print exc
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment