ultimos cambios

This commit is contained in:
Antonio Torres Míguez
2021-01-11 11:19:10 +01:00
parent a6930a52e0
commit 8456ae9c2a
10577 changed files with 2639327 additions and 0 deletions

0
m05_flask/__init__.py Normal file
View File

0
m05_flask/app.py Normal file
View File

26
m05_flask/inventory.py Normal file
View File

@ -0,0 +1,26 @@
inventory = [
{
"name": "devnet-csr-always-on-sandbox",
"ssh-info": {
"hostname": "ios-xe-mgmt-latest.cisco.com",
"port": 8181,
"credentials": {"username": "developer", "password": "C1sco12345"},
"device_type": "cisco_ios",
},
"netconf-info": {
"port": 10000
},
"restconf-info": {
"port": 9443
}
},
{
"name": "devnet-csr-always-on-sandbox",
"ssh-info": {
"hostname": "sbx-nxos-mgmt.cisco.com",
"port": 8181,
"credentials": {"username": "admin", "password": "Admin_1234!"},
"device_type": "cisco_nxos"
}
},
]

61
m05_flask/netapi.py Normal file
View File

@ -0,0 +1,61 @@
import json
from flask import Flask, request
from m02_files.l_00_inventory import get_inventory
import napalm
app = Flask(__name__)
@app.route("/inventory")
def inventory():
return json.dumps(get_inventory())
@app.route("/interface_counters")
def interface_counters():
device_name = request.args.get("device")
device_type = request.args.get("type")
port = request.args.get("port")
username = request.args.get("username")
password = request.args.get("password")
result, data = get_intf_counters(
device_name=device_name,
device_type=device_type,
credentials={"username": username, "password": password},
port=int(port),
)
if result != "success":
return data, 406
else:
return data, 200
@app.route("/device_status")
def device_status():
return "Hello, Device Status!"
def get_intf_counters(device_name, device_type, port, credentials):
if device_type == "csr":
driver = napalm.get_network_driver("ios")
else:
return "error", "getting counters supported only for CSR devices"
device = driver(
hostname=device_name,
username=credentials["username"],
password=credentials["password"],
optional_args={"port": port},
)
device.open()
counters = device.get_interfaces_counters()
return "success", json.dumps(counters)
if __name__ == "__main__":
app.run()

61
m05_flask/netclient.py Normal file
View File

@ -0,0 +1,61 @@
import requests
import json
import subprocess
import os
import sys
from time import sleep
from signal import signal, SIGINT
# response = requests.get("http://127.0.0.1:5000/inventory")
# if response.status_code != 200:
# print(f"get inventory failed: {response.reason}")
# exit()
#
# print(json.dumps(response.json(), indent=4))
def exit_gracefully(signal_received, frame):
print("\n\nexiting gracefully")
exit(0)
signal(SIGINT, exit_gracefully)
while True:
query_params = {
"device": "ios-xe-mgmt-latest.cisco.com",
"type": "csr",
"port": "8181",
"username": "developer",
"password": "C1sco12345",
}
response = requests.get("http://127.0.0.1:5000/interface_counters", params=query_params)
if response.status_code != 200:
print(f"get interface counters failed: {response.reason}")
exit()
counters = response.json()
print(json.dumps(counters, indent=4))
intf_counters_list = sorted([(k, v) for k, v in counters.items()])
subprocess.call("clear" if os.name == "posix" else "cls")
print("__Name________________ __Rx Packets__ _____Rx Octets__ __Tx Packets__ _____Tx Octets__")
for intf_name, intf_counters in intf_counters_list:
print(
f" {intf_name:<20}"
+ f" {intf_counters['rx_unicast_packets']:>14}"
+ f" {intf_counters['rx_octets']:>16}"
+ f" {intf_counters['tx_unicast_packets']:>14}"
+ f" {intf_counters['tx_octets']:>16}"
)
print("\n\n")
for remaining in range(10, 0, -1):
sys.stdout.write("\r")
sys.stdout.write(f"Refresh: {remaining:2d} seconds remaining.")
sys.stdout.flush()
sleep(1)
print(" ... updating counters ...")

0
m05_flask/rest.py Normal file
View File