-
Notifications
You must be signed in to change notification settings - Fork 9
/
router_info.py
107 lines (93 loc) · 3.48 KB
/
router_info.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
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
from iosxeapi.iosxerestapi import iosxerestapi
import click
import json
class Device(object):
def __init__(self, ip=None, port=None, username=None, password=None):
self.ip = ip
self.port = port
self.username = username
self.password = password
def set_up(self):
return iosxerestapi(host=self.ip, username=self.username, password=self.password, port=self.port)
@click.group()
# ip addresses or dns of devices
@click.option("--ip",help="ip or dns address of device")
# file of ip addresseses or dns of devices
@click.option("--file",help="file ip addresses of devices")
# option for custom port or uses restconf port 443
@click.option("--port", default=443, help="device port, default = 443" )
# prompts user for name/password of device(s)
@click.option("--username",help="device username (default lab username = cisco)", prompt=True, hide_input=False)
@click.option("--password",help="device password (default lab password = cisco)", prompt=True, hide_input=True)
@click.pass_context
def main(ctx, ip, file, port, username, password):
"""Gather and Add IOS XE device information using restconf"""
devices = []
if ip:
device = Device(ip, port, username, password)
devices.append(device)
click.secho("Working....")
else:
try:
with open(file) as f:
device_data = json.load(f)
except (ValueError, IOError, OSError) as err:
print("Could not read the 'devices' file:", err)
for device_info in device_data.values():
ip = device_info['IP']
device = Device(device_info['IP'], port, username, password)
devices.append(device)
click.secho("Working....{}".format(ip))
ctx.obj = devices
@main.command('get_device')
@click.pass_obj
def get_device(devices):
"""Gather Device information"""
for device in devices:
api = device.set_up()
result = api.get_device()
print(result)
click.secho("Task completed")
@main.command('get_bgp')
@click.pass_obj
def get_bgp(devices):
"""Gather BGP information"""
for device in devices:
api = device.set_up()
result = api.get_bgp()
print(result)
click.secho("Task completed")
@main.command('get_interfaces')
@click.pass_obj
def get_interfaces(devices):
"""Gather Interface information"""
for device in devices:
api = device.set_up()
result = api.get_interfaces_oper()
print(result)
click.secho("Task completed")
@main.command('add_drop')
@click.pass_obj
def add_drop(devices):
"""Add ACL to Interface """
for device in devices:
click.secho("Select Interface!")
router_object = device.set_up()
list_interfaces = router_object.get_interfaces_list()
user_interface = click.prompt('Available Interfaces Are:\n' + list_interfaces)
access = router_object.add_access_group(user_interface)
print(access.message)
click.secho("Task completed")
@main.command('delete_drop')
@click.pass_obj
def delete_drop(devices):
"""Remove ACL from Interface """
for device in devices:
click.secho("Select Interface!")
router_object = device.set_up()
list_interfaces = router_object.get_interfaces_list()
user_interface = click.prompt('Available Interfaces Are:\n' + list_interfaces)
delete = router_object.delete_access_group(user_interface)
print(delete.message)
click.secho("Task completed")
main()