{"id":14145,"date":"2022-01-19T19:36:50","date_gmt":"2022-01-19T19:36:50","guid":{"rendered":"https:\/\/linuxways.net\/?p=14145"},"modified":"2022-01-19T19:36:50","modified_gmt":"2022-01-19T19:36:50","slug":"how-to-dig-dns-records-in-a-sample-python-program","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/how-to-dig-dns-records-in-a-sample-python-program\/","title":{"rendered":"How to Dig DNS Records in a Sample Python Program"},"content":{"rendered":"<p>Domain Name System is the naming system that helps to translate the ip address of the specific servers, computers to the easy to remember domain names. Python has a module named dnspython which helps to get almost all records types.<\/p>\n<p>There are many kinds of records of a domain name which can be found with the use of the python <strong>dnspython<\/strong> module. With the use of dns.resolver() that is provided by the dnspython module, it is possible.<\/p>\n<h2>Finding records for domain name<\/h2>\n<p>Using the dnspython module, you are able to find the record types of domain names. There are many record types such as A, AAAA, NS, MX, TXT, CNAME, SOA and so on. Here, we have discussed the python program to find such record types. For further details, go through the end of the article.<\/p>\n<h2>Finding specific record type<\/h2>\n<p>To find the specific record type like A record, there is a simple python program which is discussed below. We are going to write the python program to check the ip address of the domain name. It is done by finding the A record of the domain. Check the python program as shown below for further details.<\/p>\n<pre>$ sudo vim A_record.py<\/pre>\n<h2><strong>Python Program to find the \u201cA record\u201d<\/strong><\/h2>\n<pre>import dnspython as dns\r\n\r\nimport dns.resolver\r\n\r\n#To Find and print A record\r\n\r\nresult = dns.resolver.resolve('linuxways.net', 'A')\r\n\r\nfor val in result:\r\n\r\nprint('A record:', val.to_text())<\/pre>\n<p>It is the output after executing the above python program to find A record of a domain name. In our case, we have used \u201clinuxways.net\u201d as a domain name. Check the screenshot as shown below for further details.<\/p>\n<pre>$ python3 A_record.py<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"488\" height=\"54\" class=\"wp-image-14146\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/01\/word-image-432.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/01\/word-image-432.png 488w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/01\/word-image-432-300x33.png 300w\" sizes=\"auto, (max-width: 488px) 100vw, 488px\" \/><\/p>\n<h2>Finding all record types<\/h2>\n<p>In case you need to find and print all record types with a single python program, there is a way to do that with the simple python program. You are able to find all record types such as A, AAAA, NS, MX, TXT, CNAME, SOA and so on. For further details, check the program code as shown below.<\/p>\n<pre>$ sudo vim DNS_records.py<\/pre>\n<h3><strong>Python program to find all records<\/strong><\/h3>\n<pre>import dnspython as dns\r\n\r\nimport dns.resolver\r\n\r\n#To Find and print records\r\n\r\ndef get_x_record(domain_name, record_type):\r\n\r\ntry:\r\n\r\nresult = dns.resolver.resolve(domain_name, record_type)\r\n\r\nfor val in result:\r\n\r\nif record_type=='CNAME':\r\n\r\nprint(f'{record_type} record: {val.target}')\r\n\r\nelse:\r\n\r\nprint(f'{record_type} record: {val.to_text()}')\r\n\r\nexcept Exception as e:\r\n\r\nprint(f'{record_type} record: {e}')\r\n\r\ndomain_name = 'linuxways.net'\r\n\r\nrecords = ['A', 'AAAA', 'TXT', 'MX', 'NS', 'SOA', 'CNAME']\r\n\r\nfor record in records:\r\n\r\nget_x_record(domain_name, record)<\/pre>\n<p>As you see below, the output after executing the above python program to find almost all record types of a specific domain name. In our case, we have used \u201clinuxways.net\u201d as a domain name. Check the screenshot as shown below for further details.<\/p>\n<pre>$ python3 DNS_records.py<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1191\" height=\"314\" class=\"wp-image-14147\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/01\/word-image-433.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/01\/word-image-433.png 1191w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/01\/word-image-433-300x79.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/01\/word-image-433-1024x270.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/01\/word-image-433-768x202.png 768w\" sizes=\"auto, (max-width: 1191px) 100vw, 1191px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, you have learnt how to dig DNS records by using a simple python program with the use of the python module <strong>dnspython<\/strong>. A Python program is useful to find either a single record type at a time or all record types of a domain name. Thank you!<\/p>","protected":false},"excerpt":{"rendered":"<p>Domain Name System is the naming system that helps to translate the ip address of the specific servers, computers to the easy to remember domain names. Python has&hellip;<\/p>","protected":false},"author":1,"featured_media":14202,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[864,10],"class_list":["post-14145","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dns","tag-python"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/14145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=14145"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/14145\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/14202"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=14145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=14145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=14145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}