|
1 | 1 | #!/usr/bin/env python |
2 | 2 | '''Usage: scriptname domain |
3 | | - Output will the list of MX records sorted by MX, lowest MX value first, and highest Mx value last. |
4 | | - This script uses PriorityQueue...Class was taken from Python Cookbook 3rd edition. |
| 3 | + Output will list of MX records of domain sorted by MX, |
| 4 | + lowest MX value first, and highest Mx value last. |
| 5 | + This script uses PriorityQueue Class and is based on |
| 6 | + Python Cookbook 3rd edition. |
5 | 7 | ''' |
6 | 8 |
|
7 | 9 | __author__ = "Daniel T." |
|
10 | 12 | __maintainer__ = "danasmera" |
11 | 13 | |
12 | 14 |
|
13 | | -import os,sys |
| 15 | +import os |
| 16 | +import sys |
14 | 17 | import heapq |
15 | 18 |
|
16 | | -maildomain=sys.argv[1] |
| 19 | +maildomain = sys.argv[1] |
17 | 20 | # Use dig command |
18 | | -mycommand="dig +short " + maildomain + " mx" |
| 21 | +mycommand = "dig +short " + maildomain + " mx" |
| 22 | + |
19 | 23 |
|
20 | 24 | class PriorityQueue: |
21 | | - def __init__(self): |
22 | | - self._queue=[] |
23 | | - self._index=0 |
| 25 | + def __init__(self): |
| 26 | + self._queue = [] |
| 27 | + self._index = 0 |
| 28 | + |
| 29 | + def push(self, item, priority): |
| 30 | + heapq.heappush(self._queue, (priority, self._index, item)) |
| 31 | + self._index += 1 |
24 | 32 |
|
25 | | - def push(self, item, priority): |
26 | | - heapq.heappush(self._queue, (priority, self._index, item)) |
27 | | - self._index+=1 |
| 33 | + def pop(self): |
| 34 | + return heapq.heappop(self._queue)[-1] |
28 | 35 |
|
29 | | - def pop(self): |
30 | | - return heapq.heappop(self._queue)[-1] |
| 36 | +q = PriorityQueue() |
31 | 37 |
|
32 | | -q=PriorityQueue() |
33 | 38 |
|
34 | 39 | def push_mail_servers(): |
35 | | - counter=0 |
36 | | - with os.popen(mycommand) as fp: |
37 | | - for line in fp: |
38 | | - mail_servers = line.strip() |
39 | | - priority, mail_server = mail_servers.split() |
40 | | - q.push(mail_server, int(priority)) |
41 | | - counter+=1 |
| 40 | + counter = 0 |
| 41 | + with os.popen(mycommand) as fp: |
| 42 | + for line in fp: |
| 43 | + mail_servers = line.strip() |
| 44 | + priority, mail_server = mail_servers.split() |
| 45 | + q.push(mail_server, int(priority)) |
| 46 | + counter += 1 |
42 | 47 | return counter |
43 | 48 |
|
| 49 | + |
44 | 50 | def main(): |
45 | | - total_mx=push_mail_servers() |
46 | | - while (total_mx > 0): |
47 | | - print q.pop() |
48 | | - total_mx-=1 |
| 51 | + total_mx = push_mail_servers() |
| 52 | + while (total_mx > 0): |
| 53 | + print q.pop() |
| 54 | + total_mx -= 1 |
49 | 55 |
|
50 | 56 | # Main |
51 | | -if __name__=='__main__': |
52 | | - main() |
| 57 | +if __name__ == '__main__': |
| 58 | + main() |
53 | 59 |
|
54 | 60 | '''Usage example |
55 | 61 | [daniel@danasmera tmp]$ dig gmail.com mx +short |
|
0 commit comments