티스토리 뷰
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
#!/usr/bin/python
import os
import re
import sys
import getopt
import time
class snmpwalk:
def __init__(self, switchIP):
self.snmpwalk = "/usr/bin/snmpwalk"
self.appOption = "-v1"
self.comOption = "-c public"
self.switchIP = switchIP
self.snmpwalkRetry = 0
def getMacToOID(self, mac):
return reduce(lambda x,y:x+'.'+str(int(y,16)), mac.replace(' ', ':').replace('-', ':').split(':'), '')
def getSNMPInfo(self, imb):
cmd = "%s %s %s %s %s" % (self.snmpwalk, self.appOption, self.comOption, self.switchIP, imb)
#print cmd
result = os.popen(cmd).read().strip()
if result.strip() == "":
for i in range(self.snmpwalkRetry):
if result.strip() == "":
time.sleep(1)
result = os.popen(cmd).read().strip()
#print result
return result
def getPort(self, mac):
port = None
mac_oid = self.getMacToOID(mac)
#print "mac_oid=", mac_oid
imb = "1.3.6.1.2.1.17.4.3.1.2%s" % (mac_oid) # dot1dTpFdbPort
info = self.getSNMPInfo(imb)
if info != '' and info.count(" = INTEGER: ") == 1:
port = info.split(" = INTEGER: ")[-1]
#print "port=", port
return port
def getIfindexFromIP(self, ip):
ifindex = None
imb = "1.3.6.1.2.1.4.20.1.2.%s" % (ip) # ipAdEntIfIndex
info = self.getSNMPInfo(imb)
if info != '' and info.count(" = INTEGER: ") == 1:
ifindex = info.split(" = INTEGER: ")[-1]
if ifindex == None:
imb = "1.3.6.1.2.1.3.1.1.1" # atIfIndex
info = self.getSNMPInfo(imb)
if info:
regex = re.compile(".*:(?:atIfindex|1\.3\.6\.1\.2\.1\.3\.1\.1\.1)\.(?:[0-9\.]+)\.%s\s=\s(?:INTEGER):(\d+)" % (ip))
ifindexs = regex.findall(info)
if len(ifindexs) == 1:
ifindex = ifindexs[0]
elif len(ifindexs) > 1:
raise Exception, info
if ifindex == None:
imb = "1.3.6.1.2.1.4.35.1.4" # ipNetToPhysicalPhysAddress
info = self.getSNMPInfo(imb)
if info:
regex = re.compile(".*:(?:ipNetToPhysicalPhysAddress|1\.3\.6\.1\.2\.1\.4\.35\.1\.4)\.([0-9]+)\.ipv4\.%s\s=\s(?:STRING):\s(?:[0-9a-f:]+)?" % (ip))
ifindexs = regex.findall(info)
if len(ifindexs) == 1:
ifindex = ifindexs[0]
elif len(ifindexs) > 1:
raise Exception, info
if ifindex == None:
imb = "1.3.6.1.2.1.4.22.1.2" # ipNetToMediaPhysAddress
info = self.getSNMPInfo(imb)
if info:
regex = re.compile(".*:(?:ipNetToMediaPhysAddress|1\.3\.6\.1\.2\.1\.4\.22\.1\.2)\.([0-9]+)\.ipv4\.%s\s=\s(?:STRING):\s(?:[0-9a-f:]+)?" % (ip))
ifindexs = regex.findall(info)
if len(ifindexs) == 1:
ifindex = ifindexs[0]
elif len(ifindexs) >1:
raise Exception, info
return ifindex
def getIfindexFromMac(self, mac, vendor):
ifindex = None
port = self.getPort(mac)
if port != None:
imb = "1.3.6.1.2.1.17.1.4.1.2.%s" % (port) # dot1dBasePortIfIndex
info = self.getSNMPInfo(imb)
if info != '' and info.count(" = INTEGER: ") == 1:
ifindex = info.split(" = INTEGER: ")[-1]
if vendor == "JUNIFER":
imb = "1.3.6.1.2.1.2.2.1.2.%s" % (ifindex) # ifDescr
info = self.getSNMPInfo(imb)
if info.count(" = STRING: ") == 1:
logicalPort = info.split(" = STRING: ")[-1]
physicalPort = logicalPort.rsplit(".0", 1)[0]
imb = "1.3.6.1.2.1.2.2.1.2"
info = self.getSNMPInfo(imb)
for value in info.split("\n"):
temp = value.split(" = STRING: ")
port = temp[-1]
if port == physicalPort:
ifindex = temp[0].split(".")[-1]
break
else:
raise Exception, info
if ifindex == None:
mac_oid = self.getMacToOID(mac)
imb = "mib-2.17.7.1.2.2.1.2.1%s" % (mac_oid)
info = self.getSNMPInfo(imb)
if info != '' and info.count(" = INTEGER: ") == 1:
ifindex = info.split(" = INTEGER: ")[-1]
if ifindex == None:
imb = "1.3.6.1.2.1.3.1.1.2" # atPhysAddress
info = self.getSNMPInfo(imb)
if info:
regex_str = "[:\-\s]".join(mac.replace(' ', ':').replace('-', ':').split(':'))
regex = re.compile(".*:(?:atPhysAddress|1\.3\.6\.1\.2\.1\.3\.1\.1\.2)\.([0-9\.]+)\s=\s(?:Hex-STRING):\s%s" % (regex_str), re.I)
oids = regex.findall(info)
if len(oids) == 1:
oid = oids[0]
imb = "1.3.6.1.2.1.3.1.1.1.%s" % (oid) # atIfIndex
info = self.getSNMPInfo(imb)
if info != '' and info.count(" = INTEGER: ") == 1:
ifindex = info.split(" = INTEGER: ")[-1]
else:
raise Exception, info
elif len(oids) > 1:
raise Exception, info
if ifindex == None:
imb = "1.3.6.1.2.1.4.35.1.4" # ipNetToPhysicalPhysAddress
info = self.getSNMPInfo(imb)
if info:
regex_str = "[:\-\s]".join(map(lambda x: str("%x" % int(x,16)), mac.replace(' ', ':').replace('-', ':').split(':')))
regex = re.compile(".*:(?:ipNetToPhysicalPhysAddress|1\.3\.6\.1\.2\.1\.4\.35\.1\.4)\.([0-9]+)\..*\s=\s(?:STRING):\s%s" % (regex_str))
ifindexs = regex.findall(info)
if len(ifindexs) == 1:
ifindex = ifindexs[0]
elif len(ifindexs) > 1:
raise Exception, info
if ifindex == None:
imb = "1.3.6.1.2.1.4.22.1.2" # ipNetToMediaPhysAddress
info = self.getSNMPInfo(imb)
if info:
regex_str = "[:\-\s]".join(map(lambda x: str("%x" % int(x,16)), mac.replace(' ', ':').replace('-', ':').split(':')))
regex = re.compile(".*:(?:ipNetToMediaPhysAddress|1\.3\.6\.1\.2\.1\.4\.22\.1\.2)\.([0-9]+)\..*\s=\s(?:STRING):\s%s" % (regex_str))
ifindexs = regex.findall(info)
if len(ifindexs) == 1:
ifindex = ifindexs[0]
elif len(ifindexs) > 1:
raise Exception, info
return ifindex
def getMacFromIfindex(self, ifindex):
mac = None
imb = "1.3.6.1.2.1.2.2.1.6.%s" % (ifindex) # ifPhysAddress
info = self.getSNMPInfo(imb)
if info != '' and info.count(" = STRING: ") == 1:
mac = info.split(" = STRING: ")[-1]
return mac
def getIfHCInOctets(self, ifindex):
octets = None
imb = "1.3.6.1.2.1.31.1.1.1.6.%s" % (ifindex) # ifHCInOctets
info = self.getSNMPInfo(imb)
if info != '' and info.cout(" = Counter") == 1:
octets = info.split(": ")[-1]
if octets != None:
octets = int(octets)
return octets
def getIfInOctets(self, ifindex):
octets = None
imb = "1.3.6.1.2.1.2.2.1.10.%s" % (ifindex) # ifInOctets
info = self.getSNMPInfo(imb)
if info != '' and info.cout(" = Counter") == 1:
octets = info.split(": ")[-1]
if octets != None:
octets = int(octets)
return octets
def getIfHCOutOctets(self, ifindex):
octets = None
imb = "1.3.6.1.2.1.31.1.1.1.10.%s" % (ifindex) # ifHCOutOctets
info = self.getSNMPInfo(imb)
if info != '' and info.cout(" = Counter") == 1:
octets = info.split(": ")[-1]
if octets != None:
octets = int(octets)
return octets
def getIfOutOctets(self, ifindex):
octets = None
imb = "1.3.6.1.2.1.2.2.1.16.%s" % (ifindex) # ifOutOctets
info = self.getSNMPInfo(imb)
if info != '' and info.cout(" = Counter") == 1:
octets = info.split(": ")[-1]
if octets != None:
octets = int(octets)
return octets
def getIfIndex(self, ifindex):
imb = "1.3.6.1.2.1.2.2.1.1.%s" % (ifindex) # ifIndex
info = self.getSNMPInfo(imb)
if info != '' and info.count(" = INTEGER: ") == 1:
ifindex = info.split(" = INTEGER: ")[-1]
return ifindex
def getSysVendor(self):
imb = "sysDescr"
info = str(self.getSNMPInfo(imb)).upper()
#print "desc=", info
return self.getModelByVendor(info)
def getModelByVendor(self, model):
info = model.upper()
if info.count("JUNIFER") or info.count("EX2200") or info.count("EX3200") or info.count("EX3300") or info.count("EX4200"):
return "JUNIFER"
elif info.count("HITACHI") or info.count("AX3630"):
return "HITACHI"
elif info.count("FORCE10") or info.count("S25P"):
return "FORCE10"
elif info.count("DASAN") or info.count("V2324G") or info.count("V2624G") or info.count("V2524G") \
or info.count("V5424G") or info.count("V5524XG") or info.count("V6524G"):
return "DASAN"
elif info.count("NETGEAR") or info.count("7212") or info.count("7224") or info.count("7248") or info.count("750T"):
return "NETGEAR"
elif info.count("EXTREME"):
return "EXTREME"
elif info.count("HUAWEI"):
return "HUAWEI"
else:
return "UNKNOWN"
def __del__(self):
pass
def main():
usage = "Usage : %s -s SWITCH_IP [-m SERVER_MAC_ADDRESS |-i SERVER_IP_ADDRESS]" % (sys.argv[0])
switch_ip = ""
mac_address = ""
ip_address = ""
options, args = getopt.getopt(sys.argv[1:], 'hs:m:i:')
for op, p in options:
if op == '-s':
switch_ip = p
if op == '-m':
mac_address = p
if op == '-i':
ip_address = p
if op == '-h':
print usage
os._exit(1)
if switch_ip == "" :
print usage
os._exit(1)
if mac_address == "" and ip_address == "":
print usage
os._exit(1)
sw = snmpwalk(switch_ip)
if mac_address :
vendor = sw.getSysVendor()
ifindex = sw.getIfindexFromMac(mac_address, vendor)
elif ip_address:
ifindex = sw.getIfindexFromIP(ip_address)
#print "Vendor : %s" % (vendor)
#print "Server MAC : %s" % (mac_address.upper())
print "Ifindex : %s" % (ifindex)
if __name__=="__main__" :
main()
|
cs |
'프로그래밍' 카테고리의 다른 글
Bash 10진수를 2진수로 변환 (0) | 2020.03.06 |
---|---|
Python 시간 변환 (0) | 2020.03.06 |
Python traceroute 구현 (0) | 2020.03.05 |
Python 여러가지 싱글톤(singleton) 구현방법 (0) | 2020.03.02 |
Python 매직 메소드와 연산자 오버로딩 (0) | 2020.02.22 |
댓글
warpmemory
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
페이지
TAG
- error
- 외부링크
- monitoring
- 예제
- Module
- deview
- configuration
- PowerShell
- 이슈처리
- command
- mysql
- RESTful
- check
- Web
- code
- File
- 번역
- limits
- engineering
- MariaDB
- 명령어
- 코드
- Python
- example
- httpd
- Windows
- Linux
- Ansible
- client
- apache
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함