Управление через API EcoSwitch Documentation / Управление через API
Управление Устройством через API возможно с помощью:
- Python – скриптовый язык программирования,
- Bash (Bourne-Again SHell) – командный интерпретатор в системах UNIX, в особенности в GNU/Linux.
Через API можно выполнить пять типов действий:
- загрузить конфигурацию (startup и running config), применить и сохранить;
- скачать конфигурации в формате JSON;
- прочитать конфигурацию (readwrite поля) и информацию об устройстве (readonly поля);
- вызвать удалённые процедуры RPC;
- загрузить конфигурацию (running config) и применить.
Перечисленные функции Устройства привязаны к определённым портам:
- Порт 22045: позволяет загрузить конфигурацию целиком в формате JSON, а также применить ее и сохранить (XPATH пути указать нельзя).
Пример для Python:
import subprocess proc = subprocess.Popen(["sshpass", "-p", "admin", "ssh", "-T", "-oStrictHostKeyChecking=no", "-oForwardX11=no", "admin@192.168.255.1", "-p", "22045"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) proc.stdin.write("{\"mng-if:addresses\":[{\"name\":\"default\",\"ip\":\"10.210.9.145\",\"prefix\":24}],\"mng-if:routes\":[{\"name\":\"default\",\"gateway\":\"10.210.9.2\",\"destination\":\"0.0.0.0\",\"prefix\":0}],\"mng-if:hostname\":\"QA22045\"}\n".encode("utf-8")) proc.stdin.flush() print(proc.stdout.read())
Пример для Bash:
$ echo | sshpass -p admin ssh -T -oStrictHostKeyChecking=no admin@10.210.9.145 -p 22045
OK
{"mng-if:addresses":[{"name":"default","ip":"10.210.9.145","prefix":24}],"mng-if:routes":[{"name":"default","gateway":"10.210.9.2","destination":"0.0.0.0","prefix":0}],"mng-if:hostname":"QA22045"}
- Порт 22046: позволяет просмотреть всю конфигурацию в формате JSON, а также конфигурацию отдельного параметра (можно использовать XPATH пути).
Пример для Python (вся конфигурация):
import subprocess proc = subprocess.Popen(["sshpass", "-p", "admin", "ssh", "-T", "-oStrictHostKeyChecking=no", "-oForwardX11=no", "admin@192.168.255.1", "-p", "22046"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) proc.stdin.write("/*\n".encode("utf-8")) proc.stdin.flush() print(proc.stdout.read())
Пример для Python (конфигурация отдельного параметра port):
import subprocess proc = subprocess.Popen(["sshpass", "-p", "admin", "ssh", "-T", "-oStrictHostKeyChecking=no", "-oForwardX11=no", "admin@192.168.255.1", "-p", "22046"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) proc.stdin.write("/port:*\n".encode("utf-8")) proc.stdin.flush() print(proc.stdout.read())
Пример для Bash (вся конфигурация):
$ echo '/*' | sshpass -p admin ssh -T -oStrictHostKeyChecking=no admin@10.210.9.145 -p 22046
OK
{"ntp:timezone":"Europe/London","ecofilter-balancer:nat-unit-queues":10,"ecofilter-balancer:balance-groups":[{"name":"BALANCEGROUP1TEST","filter-group":[{"name":"GROUP1TEST","lan":"p2-1","wan":"p2-2"}],"liveness-profile":"live_loop_or_not","rebalance":"enable"},{"name":"BALANCEGROUP2TEST","filter-group":[{"name":"GROUP2TEST","lan":"p2-4","wan":"p2-3"}],"liveness-profile":"live_loop_or_not","rebalance":"enable"}],"ecofilter-balancer:filters":[{"name":"FILTER1TEST","apply-to-links":["ISP1"],"flows":[{"name":"FLOW1TEST","action":{"balancing-as":"mag-hash","to-balance-group":"BALANCEGROUP1TEST"},"priority":123}]},{"name":"FILTER2TEST","apply-to-links":["ISP2"],"flows":[{"name":"FLOW2TEST","action":{"balancing-as":"mag-hash","to-balance-group":"BALANCEGROUP2TEST"},"priority":123}]}],"ecofilter-balancer:link":[{"name":"ISP1","lan":"p1-1","wan":"p1-3"},{"name":"ISP2","lan":"p1-2","wan":"p1-4"}],"ecofilter-balancer:liveness-profiles":[{"name":"live_loop_or_not","active-pairs":1,"initial-delay":8000,"interval":5000,"probes-down-count":10,"probes-up-count":1}],"port:label":[{"name":"p1-1","lane":1,"mtu":9000,"number":1,"speed":"10G"},{"name":"p1-2","lane":2,"mtu":9000,"number":1,"speed":"10G"},{"name":"p1-3","lane":3,"mtu":9000,"number":1,"speed":"10G"},{"name":"p1-4","lane":4,"mtu":9000,"number":1,"speed":"10G"},{"name":"p2-1","lane":1,"mtu":9000,"number":2,"speed":"10G"},{"name":"p2-2","lane":2,"mtu":9000,"number":2,"speed":"10G"},{"name":"p2-3","lane":3,"mtu":9000,"number":2,"speed":"10G"},{"name":"p2-4","lane":4,"mtu":9000,"number":2,"speed":"10G"}],"mng-if:addresses":[{"name":"default","ip":"10.210.9.145","prefix":24}],"mng-if:routes":[{"name":"default","gateway":"10.210.9.2","destination":"0.0.0.0","prefix":0}],"mng-if:hostname":"QA"}
Пример для Bash (конфигурация отдельного параметра port):
$ echo '/port:*' | sshpass -p admin ssh -T -oStrictHostKeyChecking=no admin@10.210.9.145 -p 22046
OK
{"port:label":[{"name":"p1-1","lane":1,"mtu":9000,"number":1,"speed":"10G"},{"name":"p1-2","lane":2,"mtu":9000,"number":1,"speed":"10G"},{"name":"p1-3","lane":3,"mtu":9000,"number":1,"speed":"10G"},{"name":"p1-4","lane":4,"mtu":9000,"number":1,"speed":"10G"},{"name":"p2-1","lane":1,"mtu":9000,"number":2,"speed":"10G"},{"name":"p2-2","lane":2,"mtu":9000,"number":2,"speed":"10G"},{"name":"p2-3","lane":3,"mtu":9000,"number":2,"speed":"10G"},{"name":"p2-4","lane":4,"mtu":9000,"number":2,"speed":"10G"}]}
- Порт 22047: позволяет просмотреть всю конфигурацию и состояния, конфигурацию отдельного параметра, а также прочитать информацию об Устройстве.
Пример для Python (вся конфигурация):
import subprocess proc = subprocess.Popen(["sshpass", "-p", "admin", "ssh", "-T", "-oStrictHostKeyChecking=no", "-oForwardX11=no", "admin@192.168.255.1", "-p", "22047"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) proc.stdin.write("/*\n".encode("utf-8")) proc.stdin.flush() print(proc.stdout.read())
Пример для Python (конфигурация отдельного параметра port):
import subprocess proc = subprocess.Popen(["sshpass", "-p", "admin", "ssh", "-T", "-oStrictHostKeyChecking=no", "-oForwardX11=no", "admin@192.168.255.1", "-p", "22046"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) proc.stdin.write("/port:*\n".encode("utf-8")) proc.stdin.flush() print(proc.stdout.read())
Пример для Bash (вся конфигурация):
$ echo '/*' | sshpass -p admin ssh -T -oStrictHostKeyChecking=no admin@10.210.9.145 -p 22047
OK
{"rdp-firmware:boot-image":{"current":"A","A":{"active":true,"stable":false,"version":{"major":3,"minor":2,"release":3,"bugfix":0,"revision":3122,"str":"3.2.3.0.3122"},"tries":1},"B":{"active":false,"stable":false,"version":{"major":3,"minor":2,"release":3,"bugfix":0,"revision":3122,"str":"3.2.3.0.3122"},"tries":0},"Factory":{"active":true,"stable":true}},"ntp:timezone":"Europe/London","ecofilter-balancer:nat-unit-queues":10,"ecofilter-balancer:balance-groups":[{"name":"BALANCEGROUP1TEST","filter-group":[{"name":"GROUP1TEST","lan":"p2-1","wan":"p2-2","filter-group-state":"down","statistics":{"keep-alive":{"to-lan":{"time-on-path":"0","time-of-receipt":"4957919465269"},"to-wan":{"time-on-path":"0","time-of-receipt":"4957919475799"}}}}],"liveness-profile":"live_loop_or_not","rebalance":"enable","state":"bypass"},{"name":"BALANCEGROUP2TEST","filter-group":[{"name":"GROUP2TEST","lan":"p2-4","wan":"p2-3","filter-group-state":"down","statistics":{"keep-alive":{"to-lan":{"time-on-path":"0","time-of-receipt":"4957919573139"},"to-wan":{"time-on-path":"0","time-of-receipt":"4957919580094"}}}}],"liveness-profile":"live_loop_or_not","rebalance":"enable","state":"bypass"}],"ecofilter-balancer:filters":[{"name":"FILTER1TEST","apply-to-links":["ISP1"],"flows":[{"name":"FLOW1TEST","action":{"balancing-as":"mag-hash","to-balance-group":"BALANCEGROUP1TEST"},"priority":123,"statistics":{"bytes":"0","packets":"0"}}]},{"name":"FILTER2TEST","apply-to-links":["ISP2"],"flows":[{"name":"FLOW2TEST","action":{"balancing-as":"mag-hash","to-balance-group":"BALANCEGROUP2TEST"},"priority":123,"statistics":{"bytes":"0","packets":"0"}}]}],"ecofilter-balancer:link":[{"name":"ISP1","lan":"p1-1","wan":"p1-3"},{"name":"ISP2","lan":"p1-2","wan":"p1-4"}],"ecofilter-balancer:liveness-profiles":[{"name":"live_loop_or_not","active-pairs":1,"initial-delay":8000,"interval":5000,"probes-down-count":10,"probes-up-count":1}],"port:label":[{"name":"p1-1","lane":1,"mtu":9000,"number":1,"speed":"10G","link-state":"down"},{"name":"p1-2","lane":2,"mtu":9000,"number":1,"speed":"10G","link-state":"down"},{"name":"p1-3","lane":3,"mtu":9000,"number":1,"speed":"10G","link-state":"down"},{"name":"p1-4","lane":4,"mtu":9000,"number":1,"speed":"10G","link-state":"down"},{"name":"p2-1","lane":1,"mtu":9000,"number":2,"speed":"10G","link-state":"down"},{"name":"p2-2","lane":2,"mtu":9000,"number":2,"speed":"10G","link-state":"down"},{"name":"p2-3","lane":3,"mtu":9000,"number":2,"speed":"10G","link-state":"down"},{"name":"p2-4","lane":4,"mtu":9000,"number":2,"speed":"10G","link-state":"down"}],"mng-if:addresses":[{"name":"default","ip":"10.210.9.145","prefix":24}],"mng-if:routes":[{"name":"default","gateway":"10.210.9.2","destination":"0.0.0.0","prefix":0}],"mng-if:hostname":"QA"}
Пример для Bash (конфигурация отдельного параметра port):
$ echo '/port:*' | sshpass -p admin ssh -T -oStrictHostKeyChecking=no admin@10.210.9.145 -p 22047
OK
{"port:label":[{"name":"p1-1","lane":1,"mtu":9000,"number":1,"speed":"10G","link-state":"down"},{"name":"p1-2","lane":2,"mtu":9000,"number":1,"speed":"10G","link-state":"down"},{"name":"p1-3","lane":3,"mtu":9000,"number":1,"speed":"10G","link-state":"down"},{"name":"p1-4","lane":4,"mtu":9000,"number":1,"speed":"10G","link-state":"down"},{"name":"p2-1","lane":1,"mtu":9000,"number":2,"speed":"10G","link-state":"down"},{"name":"p2-2","lane":2,"mtu":9000,"number":2,"speed":"10G","link-state":"down"},{"name":"p2-3","lane":3,"mtu":9000,"number":2,"speed":"10G","link-state":"down"},{"name":"p2-4","lane":4,"mtu":9000,"number":2,"speed":"10G","link-state":"down"}]}
- Порт 22048: позволяет просмотреть список прошивок на устройстве, а также выполнить вызов удалённых процедур (RPC передаётся в формате JSON).
Пример для Python:
import subprocess proc = subprocess.Popen(["sshpass", "-p", "admin", "ssh", "-T", "-oStrictHostKeyChecking=no", "-oForwardX11=no", "admin@192.168.255.1", "-p", "22048"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) proc.stdin.write("{\"hardware-info:show-all\":{}}\n".encode("utf-8")) proc.stdin.flush() print(proc.stdout.read())
Пример для Bash:
$ echo '{"rdp-firmware:list":{}}' | sshpass -p admin ssh -T -oStrictHostKeyChecking=no admin@10.210.9.145 -p 22048
OK
{"rdp-firmware:file":[{"name":"SDNSwitch-ecofilter-balancer-3.2.3.0.3034-develop-48babd1.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3034-develop-48babd1.image"},{"name":"SDNSwitch-ecoddp-3.2.3.0.3034-new_structure_tests_plus_docker_kudachkin-28a69ae.image"},{"name":"SDNSwitch-ecofilter-balancer-3.2.3.0.3034-develop.handmade-48babd1.image"},{"name":"SDNSwitch-ecofilter-balancer-3.2.3.0.3037-develop.handmade-6c341b5.image"},{"name":"SDNSwitch-ecofilter-balancer-3.2.3.0.3038-develop-a9ec731.image"},{"name":"SDNSwitch-ecofilter-balancer-3.2.3.0.3040-develop-dde2367.image"},{"name":"SDNSwitch-ecofilter-balancer-3.2.3.0.3041-develop-1a88025.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3041-develop-1a88025.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3044-develop.handmade-8789648.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3046-develop-b375340.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3047-develop-a473fbb.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3047-merge-request-fix-rpc-7b2d205.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3050-merge-request-cli_load_config-27cb3cf.image"},{"name":"SDNSwitch-ecofilter-balancer-3.2.3.0.3034-new_structure_tests_plus_docker_kudachkin-e655134.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3052-develop.handmade-d97612e.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3052-merge-request-cli_load_config-ed99870.image"},{"name":"SDNSwitch-ecofilter-balancer-3.2.3.0.3054-develop-14c1a24.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3011-develop-7bd4fb9.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.2995-develop-d2f7c05.image"},{"name":"SDNSwitch-ecofilter-balancer-3.2.3.0.3057-develop.handmade-a466212.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3061-develop-205a6c1.image"},{"name":"SDNSwitch-ecofilter-balancer-3.2.3.0.3061-develop-205a6c1.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3001-develop-2414eed.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3062-develop.handmade-b77e77a.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3068-develop.handmade-59f6140.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3070-develop.handmade-7d1289f.image"},{"name":"SDNSwitch-ecofilter-balancer-3.2.3.0.3070-develop.handmade-7d1289f.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3078-develop.handmade-b1a8429.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3083-merge-request-cli_fix_ancestor_parent-625b29a.image"},{"name":"SDNSwitch-ecoddp-3.2.3.0.3083-develop-3e82f8a.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3085-develop-b83b6a4.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3085-develop.handmade-b83b6a4.image"},{"name":"SDNSwitch-ecoddp-3.2.3.0.3086-merge-request-cli_fix_ancestor_parent-753607c.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3088-merge-request-fix-bgp-3abe1ab.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3093-develop.handmade-97729aa.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3096-develop-246cabe.image"},{"name":"SDNSwitch-ecofilter-balancer-3.2.3.0.3096-develop-246cabe.image"},{"name":"SDNSwitch-ecofilter-balancer-3.2.3.0.3093-merge-request-cli_hostname-9430645.image"},{"name":"SDNSwitch-ecoddp-3.2.3.0.3107-develop-d40992c.image"},{"name":"SDNSwitch-ecotap-3.2.3.0.3108-develop-fb0d1ae.image"},{"name":"SDNSwitch-ecotap-3.2.3.0.3109-develop-521572a.image"},{"name":"SDNSwitch-ecotap-3.2.3.0.3113-merge-request-ecotap_lags-67c293f.image"},{"name":"SDNSwitch-ecofilter-balancer-3.2.3.0.3113-develop-78a1f25.image"},{"name":"SDNSwitch-ecotap-3.2.3.0.3116-merge-request-ecotap_lags-4816f0c.image"},{"name":"SDNSwitch-ecofilter-balancer-3.2.3.0.3116-merge-request-fix_hash_l4-3698cd6.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3117-develop-b08f225.image"},{"name":"SDNSwitch-ecohighway-3.2.3.0.3116-develop-06c5da0.image"},{"name":"SDNSwitch-ecofilter-balancer-3.2.3.0.3117-develop-b08f225.image"},{"name":"SDNSwitch-ecofilter-balancer-3.2.3.0.3122-merge-request-fix_hash_l4-a3051c8.image"},{"name":"SDNSwitch-ecofilter-balancer-3.2.3.0.3122-merge-request-fix_hash_l4-b546c74.image"}]}
- Порт 22049: позволяет загрузить конфигурацию целиком в формате JSON, а также применить ее (XPATH пути указать нельзя).
Пример для Python:
import subprocess proc = subprocess.Popen(["sshpass", "-p", "admin", "ssh", "-T", "-oStrictHostKeyChecking=no", "-oForwardX11=no", "admin@192.168.255.1", "-p", "22049"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) proc.stdin.write("{\"mng-if:addresses\":[{\"name\":\"default\",\"ip\":\"10.210.9.145\",\"prefix\":24}],\"mng-if:routes\":[{\"name\":\"default\",\"gateway\":\"10.210.9.2\",\"destination\":\"0.0.0.0\",\"prefix\":0}],\"mng-if:hostname\":\"QA22049\"}\n".encode("utf-8")) proc.stdin.flush() print(proc.stdout.read())
Пример для Bash:
$ echo | sshpass -p admin ssh -T -oStrictHostKeyChecking=no admin@10.210.9.145 -p 22049
OK
{"mng-if:addresses":[{"name":"default","ip":"10.210.9.145","prefix":24}],"mng-if:routes":[{"name":"default","gateway":"10.210.9.2","destination":"0.0.0.0","prefix":0}],"mng-if:hostname":"QA22049"}