Pyeapi是一个Python API框架,它提供了一种简单的方式来构建、部署和管理API

老虎最近在看Arista的一本书叫【arista-warrior-2】大家有兴趣的可以看看。看到他们提供一个python api框架,这里记录下,供大家参考使用

The Python Client for eAPI (pyeapi) is a native Python library wrapper around Arista EOS eAPI. It provides a set of Python language bindings for configuring Arista EOS nodes.

Pyeapi是一个Python API框架,它提供了一种简单的方式来构建、部署和管理API。
Pyeapi基于Python语言,可以帮助开发人员快速构建、测试和部署API。它提供了一组易于使用的工具和库,使开发人员能够轻松地创建、调试和优化API。Pyeapi还支持多种API风格,包括RESTful、SOAP和gRPC等2。
使用Pyeapi,开发人员可以快速构建API,并使用内置的插件和工具进行测试和部署。此外,Pyeapi还支持与其他Python框架和库集成,例如Flask、Django和PyMySQL等1。
总之,Pyeapi是一个功能强大、易于使用的Python API框架,可以帮助开发人员快速构建、测试和部署高质量的API。

安装方式

sudo pip3 install pyeapi
#https://pyeapi.readthedocs.io/en/master/install.html

交换机配置文件

cat ~/.eapi.conf
[connection:Arista1]
host: 192.168.199.158
username: admin
password: arista
transport: https
$ chmod 400 ~/.eapi.conf
$ ls -l ~/.eapi.conf
-r-------- 1 echou echou 94 Jan 27 18:15 /home/echou/.eapi.conf

终端展示

>>> import pyeapi
>>> arista1 = pyeapi.connect_to('Arista1')
>>> import pprint
>>> pprint.pprint(arista1.enable('show hostname'))
[{'command': 'show hostname',
'encoding': 'json',
'result': {'fqdn': 'arista1', 'hostname': 'arista1'}}]
>>> arista1.config('hostname arista1-new')
[{}]
>>> pprint.pprint(arista1.enable('show hostname'))
[{'command': 'show hostname',
 'encoding': 'json',
 'result': {'fqdn': 'arista1-new', 'hostname': 'arista1-new'}}]
>>> arista1.config(['interface ethernet 1/3', 'description my_link']) [{}, 
{}]
>>> pprint.pprint(arista1.enable('show run'))
Traceback (most recent call last):
...
File "/usr/local/lib/python3.5/dist-packages/pyeapi/eapilib.py", line 396, 
in send
raise CommandError(code, msg, command_error=err, output=out) pyeapi.
eapilib.CommandError: Error [1002]: CLI command 2 of 2 'show run' failed: 
invalid command [incomplete token (at token 1: 'run')]
>>>
>>> pprint.pprint(arista1.enable('show running-config interface ethernet 
1/3'))
Traceback (most recent call last):
...
pyeapi.eapilib.CommandError: Error [1002]: CLI command 2 of 2 'show 
running-config interface ethernet 1/3' failed: invalid command [incomplete 
token (at token 2: 'interface')]
>>> import pyeapi
>>> node = pyeapi.connect_to('Arista1')
>>> vlans = node.api('vlans')
>>> type(vlans)
<class 'pyeapi.api.vlans.Vlans'>
>>> dir(vlans)
[...'command_builder', 'config', 'configure', 'configure_interface', 
'configure_vlan', 'create', 'default', 'delete', 'error', 'get', 'get_
block', 'getall', 'items', 'keys', 'node', 'remove_trunk_group', 'set_
name', 'set_state', 'set_trunk_groups', 'values']
>>> vlans.getall()
{'1': {'vlan_id': '1', 'trunk_groups': [], 'state': 'active', 'name': 
'default'}}
>>> vlans.get(1)
{'vlan_id': 1, 'trunk_groups': [], 'state': 'active', 'name': 'default'}
>>> vlans.create(10) True
>>> vlans.getall()
{'1': {'vlan_id': '1', 'trunk_groups': [], 'state': 'active', 'name':
'default'}, '10': {'vlan_id': '10', 'trunk_groups': [], 'state': 'active', 
'name': 'VLAN0010'}}
>>> vlans.set_name(10, 'my_vlan_10') True
#!/usr/bin/env python3
import pyeapi
class my_switch():
  def __init__(self, config_file_location, device):
	 # loads the config file
	 pyeapi.client.load_config(config_file_location)
	 self.node = pyeapi.connect_to(device)
	 self.hostname = self.node.enable('show hostname')[0]['result']
	['hostname']
	 self.running_config = self.node.enable('show running-config')
 def create_vlan(self, vlan_number, vlan_name):
	 vlans = self.node.api('vlans')
	 vlans.create(vlan_number)
	 vlans.set_name(vlan_number, vlan_name)
# start by importing the library
import pyeapi

# create a node object by specifying the node to work with
node = pyeapi.connect_to('veos01')

# send one or more commands to the node
node.enable('show hostname')
[{'command': 'show hostname',
  'encoding': 'json',
  'result': {u'hostname': u'veos01',
             u'fqdn': u'veos01.arista.com'}}]

# Request a specific revision of a command that has been updated
node.enable({'cmd': 'show cvx', 'revision': 2})
[{'command': {'cmd': 'show cvx', 'revision': 2},
  'encoding': 'json',
  'result': {u'clusterMode': False,
             u'controllerUUID': u'',
             u'enabled': False,
             u'heartbeatInterval': 20.0,
             u'heartbeatTimeout': 60.0}}]

# use the config method to send configuration commands
node.config('hostname veos01')
[{}]

# multiple commands can be sent by using a list
# (works for both enable or config)
node.config(['interface Ethernet1', 'description foo'])
[{}, {}]

# return the running or startup configuration from the
# node (output omitted for brevity)
node.running_config

node.startup_config

这个扩展的与Arista EOS 强相关,但是类似的系统应该也是可以的,大家喜欢的话可以研究研究。
更多样例请参照

https://github.com/arista-eosplus/pyeapi/tree/develop/examples

你可能感兴趣的:(python,开发语言)