Created
February 15, 2018 13:52
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- 52.85.89.116 | |
- 185.70.40.182 | |
- 172.217.7.195 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pyyaml | |
dnspython |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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