Python
Die folgenden Beispiele wurden mit Python 3 getestet. Um sich bei Pathfinder Server zu authentifizieren, mĂĽssen Sie einen API-SchlĂĽssel verwenden.
Verbindungstest mit PFS (authentifizierte und nicht authentifizierte Endpunkte)
import json
from types import SimpleNamespace
from urllib.error import HTTPError
import requests
import urllib3
baseUrl = "https://localhost:8087/api/v1.0/" # <- address of your Pathfinder-Server
apiKey = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" # <- put your API-key here
endpointStatus = "info/status"
endpointPing = "info/status/ping"
# get a deserialized result from PFS
def pfs_get(endpoint: str):
try:
get_result = requests.get(baseUrl + endpoint,
verify=False, headers={'Authorization': 'Bearer ' + apiKey})
except HTTPError as http_error:
print(f"HTTP error occured: {http_error}")
except Exception as exc:
print(f"Exception occured: {exc}")
else:
if get_result.status_code == 200:
result = json.loads(
get_result.text, object_hook=lambda d: SimpleNamespace(**d))
return result
elif get_result.status_code == 401:
print("Authentication error (401)")
return
else:
print(f"An error occured. Status code {get_result.status_code}")
return
def get_success(object):
if object:
return "OK"
else:
return "Failed"
def main():
# suppresses warning about self signed certificate, don't use in production
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
print("Call without authorization...")
print(get_success(pfs_get(endpointStatus)))
print("Call with authorization...")
print(get_success(pfs_get(endpointPing)))
return 0
if __name__ == "__main__":
main()
Â
Beispielausgabe:
Call without authorization...
OK
Call with authorization...
OK
Anzeige der Netzwege zu einem Port
import json
from types import SimpleNamespace
from urllib.error import HTTPError
import requests
import urllib3
baseUrl = "https://localhost:8087/api/v1.0/" # <- address of your Pathfinder-Server
apiKey = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" # <- put your API-key here
endpointNetworkPaths = "infrastructure/ports/{portId}/networkpath"
# get a deserialized result from PFS
def pfs_get(endpoint: str):
try:
get_result = requests.get(baseUrl + endpoint,
verify=False, headers={'Authorization': 'Bearer ' + apiKey})
except HTTPError as http_error:
print(f"HTTP error occured: {http_error}")
except Exception as exc:
print(f"Exception occured: {exc}")
else:
if get_result.status_code == 200:
result = json.loads(
get_result.text, object_hook=lambda d: SimpleNamespace(**d))
return result
elif get_result.status_code == 401:
print("Authentication error (401)")
return
else:
print(f"An error occured. Status code {get_result.status_code}")
return
# output details of a nwp
def print_nwp_info(nwp):
print(f"Id: {nwp.NetworkPathId}")
print(f"Name: {nwp.NetworkPathName}")
print(f"Start-Location: {nwp.StartComponentInfo.LocationPath}")
print(f"Start-Device: {nwp.StartComponentInfo.ComponentPath}")
print(f"Start-Port: {nwp.StartPortName} #{nwp.StartPortNumber}")
print(f"End-Location: {nwp.EndComponentInfo.LocationPath}")
print(f"End-Device: {nwp.EndComponentInfo.ComponentPath}")
print(f"End-Port: {nwp.EndPortName} #{nwp.EndPortNumber}")
print(f"End-Room: {nwp.EndComponentInfo.RoomName}")
def main():
# suppresses warning about self signed certificate, don't use in production
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
portId = 26375
networkPaths = pfs_get(endpointNetworkPaths.format(portId=portId))
if (networkPaths):
nwpCount = len(networkPaths.Items)
if nwpCount > 0:
print(f"Number of network paths: {nwpCount}")
for nwp in networkPaths.Items:
print_nwp_info(nwp)
return 0
else:
print(f"No network path found for port {portId}")
return 0
return -1
if __name__ == "__main__":
main()
Â
Beispielausgabe:
Number of network paths: 1
Id: 149
Name: NWP Port 17 <-> LAN
Start-Location: Universität>Rechenzentrum>EG-EG>EVT-0001
Start-Device: RACK-01>SW-Nortel ERS 4548 GT-PWR-10
Start-Port: Port 17 #17
End-Location: Universität>Rechenzentrum>EG-EG>0010-0010
End-Device: PC-121
End-Port: LAN #1
End-Room: 0010