Dnspython

Posted : admin On 1/25/2022
  1. DNSPython Technically, you can use DNSPython to do a reverse lookup.
  2. Dnspython is a utility to work with DNS, /etc/hosts is thus not used. For simple forward DNS lookups, it's better to use socket.getaddrinfo or socket.gethostbyname. Dnspython originated at Nominum where it was developed to facilitate the testing of DNS software. ABOUT THIS RELEASE. This is the development version of dnspython 2.2.0.

Author: MongoDB Documentation Team. In the first Read Data from MongoDB guide, you retrieved a document from the inventory collection. In this guide, you will: use the insertMany command to insert more documents into the collection.

Hey Python, How Powerful You Are.......!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Recently I got one use case from one of my client. Need to automate DNS entries in Microsoft DNS System.

At the beginning stage it was a very difficult one for me to integrate with such kind of systems. The risk factor is normally when we integrate with external DNS platforms ( DNS devices like Infoblox, Bluecat etc.), it should have a proper communication channel like Rest API’s, and commands or we can connect the device directly through SSH and perform some operations on the device itself.

But in this case the destination environment is Windows DNS and its new to me and also there is no API and commands to perform the DNS operations. I’m bit confused and tensed at the beginning stage.

After a long research I found one python package from https://pypy.org/

Dnspython Reverse Lookup

Package Name :- pypsrp (https://pypi.org/project/pypsrp/)

pypsrp is a Python client for the PowerShell Remoting Protocol (PSRP) and Windows Remove Management (WinRM) service. It allows you to execute commands on a remote Windows host from any machine that can run Python

By simply, this package is used to connect the windows system through WinRM service and we can execute the powershell commands within the window system,

Also this package support various authentication modes like Kerberose authentication and CredSSP authentication normally we are used in windows platforms.

I think I need to give a small idea about What Is Powershell?

Powershell:-

PowerShell is a task-based command-line shell and scripting language built on .NET. PowerShell helps system administrators and power-users rapidly automate tasks that manage operating systems (Linux, macOS, and Windows) and processes. PowerShell commands let you manage computers from the command line


So finally I got one breakthrough. Now I can connect the windows system and execute poweshell commands by using pypsrp.

But my final goal is need to automate the Microsoft DNS. Microsoft itself provide various powesehell cmdlet modules to automate DNS systems.

This document support windows10 and above. Also contain different powershell command and scripts to automate the DNS operation, For this usecase I have done some of the operation , but you can use this documentation for the complete DNS automation by using powershell and python.

Below are the operations which are done through this mechanism on MS DNS for my usecase.

1. List all the zones available in Microsoft DNS system

2. Create Host Record

3. Create PTR Record

4. Create CNAME Record

5. Check whether the Host Record is available or not?

6. Check whether the CNAME is available or not?

Dnspython

7. Delete Host Record

8. Delete PTR Record

9. Delete CNAME Record

Please find the sample python code snippet for getting all the available forward zones from MS DNS.

Hope this article helps you when such kind of integrations needed.!!!!!!!!!!!!!!!!


This article will describe appending A record to zone file and removing A record from zone file with dnspython.

Python Dns Library

Table of Contents

1 Install dnspython

Install python-dnspython package in case of Ubuntu. Install python-dns package in case of CentOS.

Install dnspython with pip in case of the distribution which has no package for dnspython.

2 Append A record to zone file

The following script will append A record to zone file.

Python Dnspython

  • dns.zone.from_file creates zone object from zone file.
  • zone.find_rdataset finds rdataset object which has 'foobar' as hostname. If not exist, rdataset object will be created.
  • rdata object has A record which has 192.168.11.250 as IP address. rdata object can have other record like NS record.
  • Write zone object to zone file.

zone file is as below.

Run python script.

A record is appended.

3 Remove A record from zone file

The following script will remove A record from zone file.

Dnspython Git

  • zone.delete_rdataset remove rdataset object which has 'foobar' as hostname.

zone file is as below.

Run python script.

Dnspython

A record is appended.