-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkAuto-Dec-vs-Imp.py
67 lines (43 loc) · 1.76 KB
/
NetworkAuto-Dec-vs-Imp.py
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
from netmiko import *
#### Example Network Automation: Declarative Vs Imperative ####
####Utilising classes, class attributes and class functions ####
#Declarative Example
class DeclarativeExample():
attr_Interface="gigabitEthernet 0/45"
attr_ip = "2.2.9.9"
attr_subnet = "255.255.255.0"
cisco_C3750X = {
'device_type': 'cisco_ios',
'host': '192.168.2.1',
'username': 'tamedcobra',
'password': 'tanyatamirtame',
'port' : 22, # defaults SSH port 22
'secret': 'tame2011', #
}
net_connect = ConnectHandler(**cisco_C3750X)
net_connect.enable()
def setInterfaceIPDec(self):
command = ['Interface '+self.attr_Interface, 'ip address '+self.attr_ip+" "+ self.attr_subnet]
result = self.net_connect.send_config_set(command)
print("New Ip address set for this interface "+self.attr_ip)
# DecEx = DeclarativeExample()
# DecEx.setInterfaceIPDec()
#Imperative Example
class ImperativeExample:
attr_Interface="gigabitEthernet 0/45"
attr_ip = "10.1.1.1"
attr_subnet = "255.255.255.0"
def CheckInterfaceIP(self):
assignedIP = 'show ip int br | inc GigabitEthernet0/45'
unassigned = 'unassigned'
Checkresult = DeclarativeExample.net_connect.send_command(assignedIP)
if unassigned in Checkresult:
print('This interface has no IP address set, setting IP.....')
print (Checkresult)
ImpEX=DeclarativeExample()
ImpEX.setInterfaceIPDec()
else:
print("sorry the following ip address has been set for this interface")
print (Checkresult)
ImpEx = ImperativeExample()
ImpEx.CheckInterfaceIP()