Skip to content

Instantly share code, notes, and snippets.

@cinhtau
Created February 15, 2018 13:52
Show Gist options
  • Save cinhtau/5314249a3693c16491343dbef6f8e555 to your computer and use it in GitHub Desktop.
Save cinhtau/5314249a3693c16491343dbef6f8e555 to your computer and use it in GitHub Desktop.
Read yaml in python with PyYAML, install requirements with pip, hosts.yml contains some ip addresses of dns servers, perform a nslookup on the ip
#!/usr/bin/python
import yaml
def read_hosts_file(file):
try:
stream = open(file, 'r')
hosts = yaml.load(stream)
return hosts
except IOError:
print("Can't read from ", file)
---
- 52.85.89.116
- 185.70.40.182
- 172.217.7.195
#!/usr/bin/python
from dns import reversename, resolver
def ip_lookup(ip):
rev_name = reversename.from_address(ip)
reversed_dns = str(resolver.query(rev_name, "PTR")[0])
return reversed_dns
pyyaml
dnspython
from unittest import TestCase
import host_inventory
import network_agent
import os
class TestRead_hosts_file(TestCase):
def test_read_hosts_file(self):
file = os.path.join('C:\\', 'Users', 'cinhtau', 'PycharmProjects', 'network-mapper', 'resources', 'test.yml');
hosts = host_inventory.read_hosts_file(file)
for host in hosts:
print(host, '=', network_agent.ip_lookup(host))
print("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment