-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautomation.py
More file actions
135 lines (103 loc) · 4.34 KB
/
Copy pathautomation.py
File metadata and controls
135 lines (103 loc) · 4.34 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
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
# import warnings
# # Suppress all deprecation warnings
# warnings.simplefilter('ignore', category=DeprecationWarning)
# from netmiko import ConnectHandler
# # Collect the number of switches to configure
# switch_num = int(input("How many switches would you like to configure: "))
# # Collect common credentials
# USER = input('SSH Username: ')
# PASS = input('SSH Password: ')
# # Process each switch
# while switch_num > 0:
# # Collect each switch's IP address
# hostip = input('Switch IP: ')
# # Define the device dictionary
# switch = {
# 'device_type': 'cisco_ios', # Ensure this matches your switch type in EVE-NG
# 'ip': hostip,
# 'username': USER,
# 'password': PASS
# }
# # Establish an SSH connection to the switch
# try:
# myssh = ConnectHandler(**switch)
# hostname = myssh.send_command('show run | include hostname')
# hostname = hostname.split() # Example output: "hostname SwitchName"
# if len(hostname) > 1:
# device = hostname[1]
# print(f'Configuring VLANs on {device}')
# else:
# raise ValueError("Hostname not found in the output.")
# # Configuring VLANs from 2 to 100
# vlan_config = []
# for vlan_id in range(11):
# vlan_config.append(f'vlan {vlan_id}')
# vlan_config.append(f'name VLAN_{vlan_id}')
# # Sending VLAN configuration
# output = myssh.send_config_set(vlan_config)
# print(output)
# print(f'Configured VLANs on {device}')
# except Exception as e:
# print(f'Failed to configure {hostip}: {str(e)}')
# finally:
# # Decrease the counter and ensure to close the SSH session
# switch_num -= 1
# if 'myssh' in locals():
# myssh.disconnect()
# input('Press ENTER To Continue')
# ////////////////////////////////////////////////////////////////////////////////////
import warnings
from netmiko import ConnectHandler
# Suppress all deprecation warnings
warnings.simplefilter('ignore', category=DeprecationWarning)
# Collect the number of switches to configure
switch_num = int(input("How many switches would you like to configure: "))
# Collect common credentials
USER = input('SSH Username: ')
PASS = input('SSH Password: ')
# Process each switch
while switch_num > 0:
# Collect each switch's IP address
hostip = input('Switch IP: ')
# Define the device dictionary
switch = {
'device_type': 'cisco_ios', # Ensure this matches your switch type in EVE-NG
'ip': hostip,
'username': USER,
'password': PASS
}
# Establish an SSH connection to the switch
try:
myssh = ConnectHandler(**switch)
hostname = myssh.send_command('show run | include hostname')
hostname = hostname.split() # Example output: "hostname SwitchName"
if len(hostname) > 1:
device = hostname[1]
print(f'Configuring VLANs on {device}')
else:
raise ValueError("Hostname not found in the output.")
# Ask user for VLAN creation mode
mode = input("Enter 'single' to configure one VLAN or 'range' to configure a range of VLANs: ").strip().lower()
vlan_config = []
if mode == 'single':
vlan_id = input("Enter VLAN ID to create (e.g., 10): ")
vlan_config.append(f'vlan {vlan_id}')
vlan_config.append(f'name VLAN_{vlan_id}')
elif mode == 'range':
start_vlan = int(input("Enter the starting VLAN ID (e.g., 10): "))
end_vlan = int(input("Enter the ending VLAN ID (e.g., 20): "))
for vlan_id in range(start_vlan, end_vlan + 1):
vlan_config.append(f'vlan {vlan_id}')
vlan_config.append(f'name VLAN_{vlan_id}')
# Sending VLAN configuration
output = myssh.send_config_set(vlan_config)
print(output)
print(f'Configured VLANs on {device}')
except Exception as e:
print(f'Failed to configure {hostip}: {str(e)}')
finally:
# Decrease the counter and ensure to close the SSH session
switch_num -= 1
if 'myssh' in locals():
myssh.disconnect()
input('Press ENTER To Continue')