1
- import textwrap
1
+ from pathlib import Path
2
2
import time
3
3
4
4
import dns .rdatatype as RecordType
@@ -101,9 +101,13 @@ def dig(
101
101
zone_file : bool = typer .Option (
102
102
False , "-z" , "--zone-file" , help = "Output the result in zone file format"
103
103
),
104
+ short : bool = typer .Option (False , "+short" , help = "Output only the relevant information" ),
104
105
) -> None :
105
106
"""
106
107
Run a DNS lookup.
108
+
109
+ Resolves DNS records for the given host name by querying the provided DNS
110
+ nameserver if one is provided, else using Cloudflare's 1.1.1.1 for speed.
107
111
"""
108
112
109
113
# Perform a DNS query and get the result and the time taken to perform the query.
@@ -115,6 +119,8 @@ def dig(
115
119
tcp = tcp ,
116
120
)
117
121
122
+ records : list [str ] = lookup .rrset .to_text ().split ("\n " )
123
+
118
124
# If the user requested zone file output, we will output the result in the zone file format.
119
125
if zone_file :
120
126
rrset : list [str ] = lookup .rrset .to_text ()
@@ -123,24 +129,28 @@ def dig(
123
129
id_ : str = str (lookup .response .id )
124
130
flags_repr : str = flags .to_text (lookup .response .flags )
125
131
126
- output = textwrap . dedent (
127
- f"""
128
- ; <<>> Spade { __version__ } <<>> { hostname_repr }
129
- ;; Got answer:
130
- ;; ->>HEADER<<- opcode: { opcode_repr } , status: NOERROR, id: { id_ }
131
- ;; flags: { flags_repr } ;
132
-
133
- ;; ANSWER SECTION:
134
- { rrset }
135
-
136
- ;; Query time: { time_taken * 1000 :.3f } msec
137
- ;; SERVER: { lookup . nameserver } : { lookup . port }
138
- ;; WHEN: { time . strftime ( "%H:%M:%S %a %d %b %Y %Z" ) }
139
- """
132
+ with open ( Path ( __file__ ). parent . parent / "templates/dig/zone_file.txt" , "r" ) as zone_file_template :
133
+ zone_file_template_content = zone_file_template . read ()
134
+
135
+ output = zone_file_template_content . format (
136
+ __version__ = __version__ ,
137
+ hostname_repr = hostname_repr ,
138
+ rrset = rrset ,
139
+ opcode_repr = opcode_repr ,
140
+ id_ = id_ ,
141
+ flags = flags_repr ,
142
+ ns = nameserver ,
143
+ port = nameserver_port ,
144
+ time_taken = time_taken ,
145
+ timestamp = time . strftime ( "%Y-%m-%d %H:%M:%S" , time . localtime ()),
140
146
)
141
147
typer .echo (output )
142
148
143
- if not zone_file :
149
+ elif short :
150
+ out = "\n " .join (record .split ()[- 1 ] for record in records )
151
+ typer .echo (out )
152
+
153
+ else :
144
154
styled_record_type : str = typer .style (
145
155
lookup .rdtype ._name_ , fg = typer .colors .BLUE
146
156
)
@@ -153,7 +163,7 @@ def dig(
153
163
154
164
output = f"\n DNS Lookup for { styled_record_type } records for { styled_hostname } through { styled_nameserver } :\n "
155
165
156
- records : list [ str ] = lookup . rrset . to_text (). split ( " \n " )
166
+
157
167
styled_records : list [str ] = []
158
168
159
169
for record in records :
0 commit comments