Benutzer-Werkzeuge

Webseiten-Werkzeuge


restful_api_python
no way to compare when less than two revisions

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.


restful_api_python [2014/06/18 21:17] (aktuell) – angelegt benny
Zeile 1: Zeile 1:
 +====== Erste Schritte mit der RESTful API (Python) ======
  
 +Das folgende Python-Skript soll einen Einblick in die ersten Schritte mit der OmniSwitch RESTful API mittels Python zeigen.
 +
 +Es wird Python benötigt um diese Schritte durchzuführen.
 +  * http://www.python.org/getit/
 +
 +Der interaktive Teil des Skripts fragt vom Anwender einige Informationen ab und erstellt dann automatisch eine Link-Aggregation mit den passenden Parametern.
 +
 +<note>Die API-Referenzbibliothek "consumer.py" werden wir hier in Kürze öffentlich zugänglich machen.</note>
 +
 +===== Beispielimplementierung mittels Python =====
 +
 +<file python omnipy.py>
 +#!/usr/bin/env python
 +#
 +# Benny's AOS Playground :)
 +#
 +from consumer import AOSAPI, AOSConnection
 +
 +def quicklag(interactive='0', debugoption=False):
 +    try:
 +        api = AOSAPI(AOSConnection(
 +            username = 'admin',
 +            password = 'switch',
 +            hostaddress = '192.168.1.4',
 +            #aosheaders = None,
 +            debug = debugoption
 +        ))
 +        api.login()
 +        
 +        if interactive == '0':
 +        
 +            lag = api.put('mib', 'alclnkaggAggTable', {
 +                                                        'mibObject0': 'alclnkaggAggIndex:40000012',
 +                                                        'mibObject1': 'alclnkaggAggName:Linkagg 12',
 +                                                        'mibObject2': 'alclnkaggAggSize:2',
 +                                                        'mibObject3': 'alclnkaggAggLacpType:1',
 +                                                        'mibObject4': 'alclnkaggAggActorAdminKey:12'
 +                                                        })
 +   
 +            lag4 = api.put('mib', 'alclnkaggAggPortTable', {
 +                                                        'mibObject0': 'alclnkaggAggPortIndex:1001',
 +                                                        'mibObject5': 'alclnkaggAggPortLacpType:1',
 +                                                        'mibObject9': 'alclnkaggAggPortActorAdminKey:12',
 +                                                        })
 +    
 +            lag4 = api.put('mib', 'alclnkaggAggPortTable', {
 +                                                        'mibObject0': 'alclnkaggAggPortIndex:1002',
 +                                                        'mibObject5': 'alclnkaggAggPortLacpType:1',
 +                                                        'mibObject9': 'alclnkaggAggPortActorAdminKey:12',
 +                                                        })
 +            pass
 +        else:
 +            print "Willkommen zu QuickLAG v1.0 erstellt fuer den \"KOMSA TecTeaming\"-Event bei Telenetwork"
 +            print "----------------------------------------------------------"
 +            print "Gib die gewuenschte LAG-ID ein: "
 +            try:
 +                lagid = input("QuickLAG-> ")
 +            except SyntaxError:
 +                print "\nSyntaxError -> Exiting..."
 +                #sys.exit(2)
 +            except KeyboardInterrupt:
 +                print "\nKeyboardInterrupt -> Exiting..."
 +                #sys.exit(2)
 +            print "LAG-ID: %s" % lagid    
 +            print "Gib die LAG-Size ein (2, 4, 8): "
 +            try:
 +                lagsize = input("QuickLAG-> ")
 +            except SyntaxError:
 +                print "\nSyntaxError -> Exiting..."
 +                #sys.exit(2)
 +            except KeyboardInterrupt:
 +                print "\nKeyboardInterrupt -> Exiting..."
 +                #sys.exit(2)
 +            print "LAG-Size: %s" % lagsize
 +            print "Gib die Ports, getrennt durch Komma als gPort ein: "
 +            try:
 +                lagports = input("QuickLAG-> ")
 +            except SyntaxError:
 +                print "\nSyntaxError -> Exiting..."
 +                #sys.exit(2)
 +            except KeyboardInterrupt:
 +                print "\nKeyboardInterrupt -> Exiting..."
 +                #sys.exit(2)
 +
 +            for i in lagports:
 +                print "LAG-Ports: %s" % i    
 +            print "-------"
 +            print "Ok, anschnallen bitte ..."
 +            print "-------"
 +            
 +            lag = api.put('mib', 'alclnkaggAggTable', {
 +                                                        'mibObject0': 'alclnkaggAggIndex:'+ str((40000000 + lagid)),
 +                                                        'mibObject1': 'alclnkaggAggName:Linkagg %s' % lagid,
 +                                                        'mibObject2': 'alclnkaggAggSize:'+ str(lagsize),
 +                                                        'mibObject3': 'alclnkaggAggLacpType:1',
 +                                                        'mibObject4': 'alclnkaggAggActorAdminKey:'+str(lagid)
 +                                                        })
 +            if api.success() == True:
 +                print "Linkaggregation %s erfolgreich eingerichtet." % lagid
 +            else:
 +                print "Erstellung der Linkaggregation fehlgeschlagen: ", api.diag(), "-", str(lag['result']['error']['0'])
 +
 +            for i in lagports:
 +                lag4 = api.put('mib', 'alclnkaggAggPortTable', {
 +                                                            'mibObject0': 'alclnkaggAggPortIndex:'+ str(i),
 +                                                            'mibObject5': 'alclnkaggAggPortLacpType:1',
 +                                                            'mibObject9': 'alclnkaggAggPortActorAdminKey:'+ str(lagid),
 +                                                            })
 +                if api.success() == True:
 +                    print "Linkaggregationsport %s erfolgreich eingerichtet." % i
 +                else:
 +                    print "Zuordnung des Ports zur Linkaggregation fehlgeschlagen: ", api.diag(), "-", str(lag4['result']['error']['0'])
 +            print "Alles erledigt, bitte Konfiguration auf Switch pruefen ... :)"         
 +        api.logout()
 +        
 +    except HTTPError, e:
 +        api.logout()
 +        print "Error: " + e.msg
 +        
 +    except KeyboardInterrupt:
 +                print "\nKeyboardInterrupt -> Exiting..."
 +                api.logout()
 +</file>
 +
 +===== Python-Skript in der Anwendung =====
 +
 +Wenn man das Python-Skript nun anwendet, kommen wir zu folgenden Ausgaben/Ergebnissen.
 +
 +  * quicklag(), Erstellt eine Link-Aggregation mit hinterlegten Standardwerten
 +  * quicklag(1), Fragt Informationen vom Nutzer ab und erstellt daraufhin eine Link-Aggregation
 +  * quicklag(1, True), Dies erzeugt zusätzliche Debugging-Meldungen
 +
 +<code>
 +Benjamins-MacBook-Pro:Omni Benny$ python
 +Python 2.7.5 (v2.7.5:ab05e7dd2788, May 13 2013, 13:18:45) 
 +[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
 +Type "help", "copyright", "credits" or "license" for more information.
 +>>> 
 +>>> from omnipy import quicklag
 +>>> quicklag()
 +>>> quicklag(1)
 +Willkommen zu QuickLAG v1.0 erstellt fuer den "KOMSA TecTeaming"-Event bei Telenetwork
 +----------------------------------------------------------
 +Gib die gewuenschte LAG-ID ein: 
 +QuickLAG-> 13
 +LAG-ID: 13
 +Gib die LAG-Size ein (2, 4, 8): 
 +QuickLAG-> 2
 +LAG-Size: 2
 +Gib die Ports, getrennt durch Komma als gPort ein: 
 +QuickLAG-> 1003,1004
 +LAG-Ports: 1003
 +LAG-Ports: 1004
 +-------
 +Ok, anschnallen bitte ...
 +-------
 +Linkaggregation 13 erfolgreich eingerichtet.
 +Linkaggregationsport 1003 erfolgreich eingerichtet.
 +Linkaggregationsport 1004 erfolgreich eingerichtet.
 +Alles erledigt, bitte Konfiguration auf Switch pruefen ... :)
 +>>> quicklag(1)
 +Willkommen zu QuickLAG v1.0 erstellt fuer den "KOMSA TecTeaming"-Event bei Telenetwork
 +----------------------------------------------------------
 +Gib die gewuenschte LAG-ID ein: 
 +QuickLAG-> 13
 +LAG-ID: 13
 +Gib die LAG-Size ein (2, 4, 8): 
 +QuickLAG-> 2
 +LAG-Size: 2
 +Gib die Ports, getrennt durch Komma als gPort ein: 
 +QuickLAG-> 1003,1004
 +LAG-Ports: 1003
 +LAG-Ports: 1004
 +-------
 +Ok, anschnallen bitte ...
 +-------
 +Erstellung der Linkaggregation fehlgeschlagen:  400 - Submission failed : This aggregate already exists 13
 +Zuordnung des Ports zur Linkaggregation fehlgeschlagen:  400 - Submission failed : This aggregable port already exists
 +Zuordnung des Ports zur Linkaggregation fehlgeschlagen:  400 - Submission failed : This aggregable port already exists
 +Alles erledigt, bitte Konfiguration auf Switch pruefen ... :)
 +>>> 
 +</code>
 +
 +===== Resultierende Konfiguration auf dem OmniSwitch =====
 +
 +<code>
 +OmniSwitch-> 
 +Fri Jul 12 13:32:58 : AAA Switch-Warning warning message:
 ++++ WARNING! Switch Access Vulnerability.
 ++++ WARNING! Admin user authentication using default password.
 +
 +-> 
 +-> show configuration snapshot linkagg
 +! Link Aggregate:
 +linkagg lacp agg 12 size 2 admin-state enable 
 +linkagg lacp agg 12 name "Linkagg 12"
 +linkagg lacp agg 12 actor admin-key 12
 +linkagg lacp agg 13 size 2 admin-state enable 
 +linkagg lacp agg 13 name "Linkagg 13"
 +linkagg lacp agg 13 actor admin-key 13
 +linkagg lacp port 1/1 actor admin-key 12
 +linkagg lacp port 1/2 actor admin-key 12
 +linkagg lacp port 1/3 actor admin-key 13
 +linkagg lacp port 1/4 actor admin-key 13
 +</code>
restful_api_python.txt · Zuletzt geändert: 2014/06/18 21:17 von benny

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki