uploading source code for chapter 13

This commit is contained in:
muassif 2021-08-02 00:12:53 +04:00 committed by GitHub
parent bc1582a58d
commit b5a44b66fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 428 additions and 0 deletions

View File

@ -0,0 +1,25 @@
from kafka import KafkaConsumer
import json, sys
host_id = '10.52.90.101:9092'
topic_id = 'NSP-EQUIPMENT'
consumer = KafkaConsumer(bootstrap_servers=['10.52.90.101:9092'],
auto_offset_reset='earliest',
consumer_timeout_ms=1000,
api_version=(0, 10, 1))
consumer.subscribe([topic_id])
print(consumer)
try:
for message in consumer:
print(message)
if message is None:
continue
else:
msg = json.loads(message.value)
print(json.dumps(msg, indent=4, sort_keys=True))
except KeyboardInterrupt:
sys.stderr.write('++++++ Aborted by user ++++++++\n')
finally:
consumer.close()

View File

@ -0,0 +1,15 @@
import requests
token = 'VEtOLWFkbWluNjg0NGE2YjQtNjIwMy00NTEwLWI2YzItMjc1MGU1MDFkZmNm'
NSP_KAFKA_API = url = "https://10.52.90.101:8544/nbi-notification/api/v1/notifications/subscriptions"
def get_subscription():
headers = {'Authorization': 'Bearer {}'.format(token) }
response = requests.request("GET", url, data={}, headers=headers, verify=False)
print(response.text)
if __name__ == '__main__':
get_subscription()

View File

@ -0,0 +1,85 @@
#!/usr/bin/env python
import threading, logging, time
import multiprocessing
import sys
from kafka import KafkaConsumer
"""Collect command-line options in a dictionary"""
def getopts(argv):
opts = {} # Empty dictionary to store key-value pairs.
while argv: # While there are arguments left to parse...
if argv[0][0] == '-': # Found a "-name value" pair.
opts[argv[0]] = argv[1] # Add key and value to the dictionary.
argv = argv[1:] # Reduce the argument list by copying it starting from index 1.
return opts
class Consumer(multiprocessing.Process):
host_id = ''
topic_id = ''
def __init__(self, host, topic):
self.host_id = host + ''
self.topic_id = topic
multiprocessing.Process.__init__(self)
self.stop_event = multiprocessing.Event()
def stop(self):
self.stop_event.set()
def run(self):
consumer = KafkaConsumer(bootstrap_servers=self.host_id,
auto_offset_reset='earliest',
consumer_timeout_ms=1000,
api_version=(0, 8, 0))
consumer.subscribe([self.topic_id])
while not self.stop_event.is_set():
for message in consumer:
print(message)
print("---------------------------------------")
if self.stop_event.is_set():
break
consumer.close()
def main():
from sys import argv
myargs = getopts(argv)
HOST_IP=''
TOPIC=''
if '-host' in myargs: # Example usage.
HOST_IP = myargs['-host']
else:
print ('please use as follows python kafka_test_consumer.py -host HostIP -topic TOPIC')
if '-topic' in myargs: # Example usage.
TOPIC = myargs['-topic']
else:
print ('please use as follows python kafka_test_consumer.py -host HostIP -topic TOPIC')
print(myargs)
tasks = [
# Producer(),
Consumer(HOST_IP,TOPIC)
]
for t in tasks:
t.start()
# time.sleep(10)
# for task in tasks:
# task.stop()
for task in tasks:
task.join()
if __name__ == "__main__":
logging.basicConfig(
format='%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s',
level=logging.INFO
)
main()

View File

@ -0,0 +1,31 @@
import requests
token = 'VEtOLWFkbWluMDE5NDdhZWUtMjRjZi00ZjEwLTg5MDItYzBhNTdjOGU2NGE2'
url = "https://10.52.90.101:8544/nbi-notification/api/v1/notifications/subscriptions"
def create_subscription(category):
headers = {'Authorization': 'Bearer {}'.format(token) }
subscription = {
"categories": [
{
"name": "{}".format(category)
}
]
}
#print(subscription)
response = requests.request("POST", url, json=subscription,
headers=headers, verify=False)
print(response.text)
subscriptionId = response.json()["response"]["data"]["subscriptionId"]
topicId = response.json()["response"]["data"]["topicId"]
timeOfSubscription = response.json()["response"]["data"]["timeOfSubscription"]
expiresAt = response.json()["response"]["data"]["expiresAt"]
sub_data = [subscriptionId,topicId,timeOfSubscription,expiresAt]
print(sub_data)
return sub_data
if __name__ == '__main__':
#create_subscription("NSP-EQUIPMENT")
create_subscription("NSP-PACKET-ALL")

View File

@ -0,0 +1,17 @@
from napalm import get_network_driver
import json
def main():
driver = get_network_driver('iosxr')
device = driver('172.16.2.50', 'root', 'rootroot')
try:
device.open()
device.load_merge_candidate(config='interface Lo0 \n description napalm added new desc \n end\n')
print(device.compare_config())
device.commit_config()
finally:
device.close()
if __name__ == '__main__':
main()

View File

@ -0,0 +1,17 @@
from napalm import get_network_driver
import json
def main():
driver = get_network_driver('iosxr')
device = driver('172.16.2.50', 'root', 'rootroot')
try:
device.open()
print(json.dumps(device.get_interfaces_ip(), indent=2))
print(json.dumps(device.get_facts(), indent=2))
finally:
device.close()
if __name__ == '__main__':
main()

View File

@ -0,0 +1,13 @@
from ncclient import manager
with manager.connect(host='172.16.2.50', username='root', password='rootroot', hostkey_verify=False) as m:
#with manager.connect(host='172.16.2.182', username='admin', password='admin', hostkey_verify=False) as m:
capabilities = []
for capability in m.server_capabilities:
capabilities.append(capability)
capabilities = sorted(capabilities)
for cap in capabilities:
print(cap)
result = m.get_config(source="running")
print (result)

View File

@ -0,0 +1,9 @@
<config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
<interface-configurations xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg">
<interface-configuration>
<active>act</active>
<interface-name>{int_name}</interface-name>
<description>{int_desc}</description>
</interface-configuration>
</interface-configurations>
</config>

View File

@ -0,0 +1,14 @@
from ncclient import manager
nc_template = open("config-template.xml").read()
nc_payload = nc_template.\
format(int_name='Loopback0',
int_desc="Configured by NETCONF")
with manager.connect(host='172.16.2.50', username='root',
password='rootroot', hostkey_verify=False) as nc_conn:
netconf_reply = nc_conn.edit_config(nc_payload, target="candidate")
print(netconf_reply)
reply = nc_conn.commit()
print(reply)

View File

@ -0,0 +1,7 @@
from ncclient import manager
with manager.connect(host='172.16.2.50', username='root',
password='rootroot', hostkey_verify=False) as m:
result = m.get_config("running",
filter=('subtree',
'<interfaces xmlns="http://openconfig.net/yang/interfaces"/>'))
print (result)

View File

@ -0,0 +1,16 @@
from ncclient import manager
interface_filter = """
<filter>
<interfaces xmlns="http://openconfig.net/yang/interfaces">
<interface>
<name>{int_name}</name>
</interface>
</interfaces>
</filter>
"""
with manager.connect(host='172.16.2.50', username='root', password='rootroot', hostkey_verify=False) as conn:
filter = interface_filter.format(int_name = "MgmtEth0/RP0/CPU0/0")
result = conn.get_config("running", filter )
print (result)

View File

@ -0,0 +1,20 @@
from netmiko import ConnectHandler
cisco_rtr = {
"device_type": "cisco_ios",
"host": "172.16.2.50",
"username": "root",
"password": "rootroot",
}
def main():
commands = ["int Lo0", "description my custom description", "commit"]
# commands =["logging buffered 100000"]
with ConnectHandler(**cisco_rtr) as net_connect:
output = net_connect.send_config_set(commands)
print(output)
print()
if __name__ == '__main__':
main()

View File

@ -0,0 +1,20 @@
from netmiko import ConnectHandler
cisco_rtr = {
"device_type": "cisco_ios",
"host": "172.16.2.50",
"username": "root",
"password": "rootroot",
}
def main():
command = "show ip int brief"
with ConnectHandler(**cisco_rtr) as net_connect:
print(net_connect.find_prompt())
print(net_connect.enable())
output = net_connect.send_command(command)
print(output)
if __name__ == '__main__':
main()

View File

@ -0,0 +1,13 @@
import requests
import json
token = 'VEtOLWFkbWluMWExNWU0MWQtMjk3Mi00YmM1LWIzMmQtMGVmNWNiNDcxOTQy'
payload = {}
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {}'.format(token)
}
print(headers)
url = "https://10.52.90.101:8544/NetworkSupervision/rest/api/v1/networkElements"
resp = requests.request("GET", url, headers=headers, data=payload, verify=False)
print(resp.text)

View File

@ -0,0 +1,21 @@
import requests
import json
token = 'VEtOLWFkbWluYTIyNTIzODktNmZhYy00ZGU2LTg5ZjUtYTEyMDc4ZmQwMTdl'
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {}'.format(token)
}
url1 = "https://10.52.90.90:8443/nfm-p/rest/api/v1/managedobjects/searchWithFilter"
payload1 = json.dumps({
"fullClassName": "equipment.PhysicalPort",
"filterExpression": "siteId = '10.172.172.172' AND portName='1/1/1'",
"resultFilter":[
"objectFullName",
"description"
]
})
response = requests.request("POST", url1, headers=headers, data=payload1, verify=False)
print(response.json()[0]['objectFullName'])
print(response.json()[0]['description'])

View File

@ -0,0 +1,11 @@
import requests
import json
token = 'VEtOLWFkbWluMWExNWU0MWQtMjk3Mi00YmM1LWIzMmQtMGVmNWNiNDcxOTQy'
payload = {}
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {}'.format(token)
}
url = "https://10.52.90.101:8544/NetworkSupervision/rest/api/v1/ports?filter=(name='Port 1/1/1' and neId='10.172.172.172')"
resp = requests.request("GET", url, headers=headers, data=payload, verify=False)
print(resp.text)

View File

@ -0,0 +1,21 @@
from base64 import b64encode
import requests
import json
message = "username" + ':' + "password!"
message_bytes = message.encode('UTF-8')
basic_token = b64encode(message_bytes)
payload = json.dumps({
"grant_type": "client_credentials"
})
# Base token is obtained by computing base64 encoding of username and password
headers = {
'Content-Type': 'application/json',
'Authorization': 'Basic {}'.format(str(basic_token, 'UTF-8'))
}
url = "https://10.52.90.101/rest-gateway/rest/api/v1/auth/token"
resp = requests.request("POST", url, headers=headers,
data=payload, verify=False)
token = resp.json()["access_token"]
print(token)

View File

@ -0,0 +1,6 @@
import requests
payload = {}
headers = {}
url = "https://10.52.90.101/rest-gateway/rest/api/v1/location/services"
resp = requests.request("GET", url, headers=headers, data=payload, verify=False)
print(resp.text)

View File

@ -0,0 +1,6 @@
import requests
payload = {}
headers = {}
url = "https://10.52.90.101/rest-gateway/rest/api/v1/location/services/endpoints?endPoint=/v1/auth/token"
resp = requests.request("GET", url, headers=headers, data=payload, verify=False)
print(resp.text)

View File

@ -0,0 +1,34 @@
import requests
import json
token = 'VEtOLWFkbWluNjg0NGE2YjQtNjIwMy00NTEwLWI2YzItMjc1MGU1MDFkZmNm'
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {}'.format(token)
}
url1 = "https://10.52.90.90:8443/nfm-p/rest/api/v1/managedobjects/searchWithFilter"
payload1 = json.dumps({
"fullClassName": "equipment.PhysicalPort",
"filterExpression": "siteId = '10.172.172.172' AND portName='1/1/1'",
"resultFilter":[
"objectFullName",
"description"
]
})
response = requests.request("POST", url1, headers=headers, data=payload1, verify=False)
port_ofn = response.json()[0]['objectFullName']
payload2 = json.dumps({
"fullClassName": "equipment.PhysicalPort",
"properties": {
"description": "description added by Python program2"
}
})
url2 = "https://10.52.90.90:8443/nfm-p/rest/api/v1/managedobjects/"+port_ofn
response = requests.request("PUT", url2, headers=headers, data=payload2, verify=False)
print(response.text)

View File

@ -0,0 +1,27 @@
import paramiko
ip='172.16.2.50'
port=22
username='root'
password='rootroot'
cmd= 'show ip interface brief \n'
def main():
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, port, username, password)
stdin, stdout, stderr = ssh.exec_command(cmd)
outlines = stdout.readlines()
resp = ''.join(outlines)
print(resp)
finally:
ssh.close()
if __name__ == '__main__':
main()