-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathudt.py
More file actions
executable file
·74 lines (68 loc) · 2.91 KB
/
udt.py
File metadata and controls
executable file
·74 lines (68 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python
# Filename udt.py
from collections import defaultdict
import telnetlib
import os,sys,commands,multiprocessing
import smtplib
import time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
#-------config------------------
allarpmacfile = '/root/gitHub/udt/allarpmac.html'
pythonlog = '/root/mylog.txt'
history_path = '/root/gitHub/udt/macarpbak/'
getMacArp_py_path = '/root/gitHub/udt/getMacArp.py'
if(len(sys.argv) != 3):
print "please input ip or mac !</p>"
print "MAC example: 90b1.1c94.c1fb</p>"
print "IP example: 10.10.8.30</p>"
sys.exit()
ipormac = sys.argv[1]
s_or_his = sys.argv[2]
if (ipormac.count('.') <= 1)|(ipormac.count('*') > 0)|(ipormac.count('.') >= 4):
print "content not ip or mac "
sys.exit()
def main(_ipormac,_allarpmacfile):
logtime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
arpsearch_cmd = "grep 'ARP.*."+_ipormac+" ' "+ _allarpmacfile
os.system("echo "+ logtime +" UDT be using now ! cmd:"+arpsearch_cmd+" >> "+pythonlog) # log to mylog.txt
#print arpsearch_cmd
tagidstr = os.popen(arpsearch_cmd,'r').read()
#print tagidstr
tagidcount = tagidstr.count('\n')
if (tagidcount == 0):
print "No result!"+"</p>"
for tagidindex in xrange(tagidcount):
tagid = tagidstr.split('\n')[tagidindex].split()[0]
macsearch_cmd = "awk -vbb="+tagid+" '{if ($1==bb){print $2,$3,$4,$5,$6}}' "+_allarpmacfile
#print macsearch_cmd
allresult = os.popen(macsearch_cmd,'r').read()
allresult_count = allresult.count('\n')
for resultindex in xrange(allresult_count):
#print resultindex
if (resultindex == 0): #the first line is arp info
print '<font color="red">'+allresult.split('\n')[resultindex]+'</font>'+'</p>'
else: #other line is mac-address info
print allresult.split('\n')[resultindex]+'</p>'
if (allresult_count == 1):
print "no mac-address infomation!"+'</p>'
if __name__ == "__main__":
if (s_or_his == 'search'):
main(ipormac,allarpmacfile)
sys.exit()
if (s_or_his == 'history'):
#print history_path
listdirstr = os.listdir(history_path)
listdirstr = sorted(listdirstr,reverse=True)
#print listdirstr
#print len(listdirstr)
if (len(listdirstr)>500):
print '<font color="green">'+">>>>>>>>>Warning: history list > 500, please rm some XXX.arpmacbak <<<<<<<<"+'</font>'
for arpmacbak_line in listdirstr:
print '</p><b>['+'history file: '+arpmacbak_line+']</b></p>'
main(ipormac,history_path+arpmacbak_line)
sys.exit()
if (s_or_his == 'update'):
os.system('python '+ getMacArp_py_path )
print '<font color="green">'+"GetAllArpMac successful ! "+'</font>'