-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
108 lines (94 loc) · 3.69 KB
/
run.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
108
"""cisco-dnac-healthcheck-netconf Console Script.
Copyright (c) 2020 Cisco and/or its affiliates.
This software is licensed to you under the terms of the Cisco Sample
Code License, Version 1.1 (the "License"). You may obtain a copy of the
License at
https://developer.cisco.com/docs/licenses
All use of the material herein must be in accordance with the terms of
the License. All rights not expressly granted by the License are
reserved. Unless required by applicable law or agreed to separately in
writing, software distributed under the License is distributed on an "AS
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied.
"""
__author__ = "Robert Csapo"
__email__ = "[email protected]"
__version__ = "1.0"
__copyright__ = "Copyright (c) 2020 Cisco and/or its affiliates."
__license__ = "Cisco Sample Code License, Version 1.1"
from ncclient import manager
from ncclient.transport import AuthenticationError, SessionCloseError
import xmltodict
import logging
import argparse
import sys
def healthcheck(config):
result = {}
try:
with manager.connect(
host=config["host"],
port=830,
username=config["username"],
password=config["password"],
hostkey_verify=False
) as m:
device = """
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<hostname>
</hostname>
<version>
</version>
</native>
"""
data = m.get(("subtree", device))
except AuthenticationError as e:
logging.error("ncclient: {}".format(e))
result["success"] = False
return result
except SessionCloseError as e:
logging.error("ncclient: {}".format(e))
result["success"] = False
return result
except Exception as e:
logging.error("ncclient: {}".format(e))
result["success"] = False
return result
data = str(data)
parse = xmltodict.parse(data.encode())
result["hostname"] = parse["rpc-reply"]["data"]["native"]["hostname"]
result["version"] = parse["rpc-reply"]["data"]["native"]["version"]
result["success"] = True
return result
if __name__ == "__main__":
config = {}
if len(sys.argv) == 1:
print("Cisco DNA Center Healthcheck Netconf Prompt\n")
config["host"] = input("host: ")
config["username"] = input("username: ")
config["password"] = input("password: ")
else:
print("args")
parser = argparse.ArgumentParser(
description="Cisco DNA Center Healthcheck Netconf",
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument("-host", type=str, required=True,
help="Host (device)")
parser.add_argument("-username", type=str, required=True,
help="Username")
parser.add_argument("-password", type=str, required=True,
help="Password")
args = parser.parse_args()
config["host"] = args.host
config["username"] = args.username
config["password"] = args.password
logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S')
result = healthcheck(config)
if result["success"] is True:
logging.info("SUCCESS: Hostname: {} Version: {}".format(
result["hostname"],
result["version"]
))