-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathstaticip.py
More file actions
91 lines (83 loc) · 3.13 KB
/
Copy pathstaticip.py
File metadata and controls
91 lines (83 loc) · 3.13 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# This script is provided with no support or guarantees.
# You may use it, modify and distribute with no restrictions
# as long as original author is mentioned.
#
# Script is designed for embedded operation within Comware OS on HP 5130EI Switch.
# It purpose is to check if servers with static ip is plugged in the right port on the switch.
# If wrong port is detected, it then automatically send syslog message to inform administrator
# Administrator has to declare ip in the corresponding interface description.
# For example,
# interface GigabitEthernet1/0
# description 192.168.0.13
#
# Usage:
# <Switch1>python staticip.py
#
# You may also run script automatically when link up/down interface
# is recognized by leveraging Comware EAA:
# [Switch1]rtm cli-policy POL1
# [Switch1-rtm-POL1]event syslog priority all msg "link status is up" occurs 1 period 1
# [Switch1-rtm-POL1]action 0 cli python staticip.py
# [Switch1-rtm-POL1]running-time 180
# [Switch1-rtm-POL1]user-role network-admin
# [Switch1-rtm-POL1]commit
#
# Author: Serge BAIKOFF
# Contact: serge.baikoff@hp.com
__author__ = 'SEBAIK'
import comware
import re
import syslog
import socket
ping_opt = '-c 2'
def get_config_port_ip():
# Extract GigabitEthernet and IP port mapping from administrator Description
output = comware.CLI('dis interface GigabitEthernet brief description', False).get_output()
ip = re.compile(r"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)", re.VERBOSE)
int = re.compile(r"GE[1-9]/[0-9]/[0-9]{1,2}", re.I)
tab = []
for i in output:
n = ip.search(i)
m = int.search(i)
if n and m :
tab.append(n.group(0))
m = m.group(0).replace("GE", "GigabitEthernet")
tab.append(m)
else:
continue
# Convert list to dictionnairy
result = dict(tab[i:i+2] for i in range(0, len(tab), 2))
return result
def list_live_ip(ip):
# Ping IP list
for i in ip.keys():
cmd = comware.CLI('ping %s %s' % (ping_opt, i), False)
return 'none'
def arp(ip):
# Return GigabitEthernet interface where ip is detected by checking arp mapping
cmd = comware.CLI('dis arp | inc %s' % ip, False)
output = cmd.get_output()
if len(output) == 1:
return 'none'
else:
a = re.search(ip, output[1])
if a.group(0) == ip:
b = output[1].split()
c = b[3].replace("GE", "GigabitEthernet")
return c
else:
return 'none'
def main():
config = get_config_port_ip()
list_live_ip(config)
for i in config.keys():
if arp(i) == config[i]:
continue
if arp(i) == 'none':
syslog.openlog( 'WARNING', 0, syslog.LOG_LOCAL7 )
syslog.syslog(syslog.LOG_ALERT, '%s %s IS NOT ACTIVE ' % (socket.gethostname(), i))
else:
syslog.openlog( 'WARNING', 0, syslog.LOG_LOCAL4 )
syslog.syslog(syslog.LOG_ALERT, '%s %s IS IN WRONG PORT %s AND SHOULD BE IN %s ' % (socket.gethostname(), i, arp(i),config[i] ))
if __name__ == "__main__":
main()