0

我能够与 f5 框通信并使用一些基本配置文件创建称为(虚拟 = VIP = 虚拟服务器)的单个对象。

工作代码:

from f5.bigip import ManagementRoot
import urllib3
urllib3.disable_warnings()
mgmt = ManagementRoot('13.126.189.103','admin','admin')
ltm = mgmt.tm.ltm
virtuals = mgmt.tm.ltm.virtuals
virtual = mgmt.tm.ltm.virtuals.virtual
virtual1 = mgmt.tm.ltm.virtuals.virtual.create(name='virtual3', description = 'testnew' , destination= '172.31.5.8:80', partition='Common')

我的要求是,我将提供来自 CSV 文件的输入,可能是 100 个 VIP。上述语法必须在导入的 CSV 文件中逐行运行。

from f5.bigip import ManagementRoot
import urllib3
import csv
import sys
import os
urllib3.disable_warnings()
mgmt = ManagementRoot('13.126.189.103','admin','admin')
ltm = mgmt.tm.ltm
virtuals = mgmt.tm.ltm.virtuals
virtual = mgmt.tm.ltm.virtuals.virtual


def configureVirtuals(bigip, virtualFile):
  # These are the fields in the file. The purpose of each field is obvious. See. Input from CSV file called 'virtualFile' and each row has config
details
  # the BIG-IP iControl REST API for more information.
  fieldNames = ["name", "description", "ip", "port"]
  virtualReader = csv.DictReader(virtualFile, fieldnames=fieldNames,
                  delimiter=",")


# Create a virtual server, one per line. it will take input from CSV file by row one by one
  try: 
    for row in virtualReader: 
      myvirtual = mgmt.tm.ltm.virtuals.virtual.create(name=row["name"],
                  description=row["description"], 
                  destination="%s:%s" % (row["ip"], row["port"])

在解析我是 f5 工程师并且是 python 新手时,我收到了意外的 EOF 错误。为批量更改构建应用程序。

有人请帮帮我吗?

4

1 回答 1

0

首先,您忘记了一个右括号。第二个问题是 try 块,它没有except. 如果使用try,则必须捕获异常,否则根本不需要try。所以要么不要使用try

from f5.bigip import ManagementRoot
import urllib3
import csv
import sys
import os
urllib3.disable_warnings()
mgmt = ManagementRoot('13.126.189.103','admin','admin')
ltm = mgmt.tm.ltm
virtuals = mgmt.tm.ltm.virtuals
virtual = mgmt.tm.ltm.virtuals.virtual


def configureVirtuals(bigip, virtualFile):
  # These are the fields in the file. The purpose of each field is obvious. See. Input from CSV file called 'virtualFile' and each row has config details
  # the BIG-IP iControl REST API for more information.
  fieldNames = ["name", "description", "ip", "port"]
  virtualReader = csv.DictReader(virtualFile, fieldnames=fieldNames,
                  delimiter=",")


  # Create a virtual server, one per line. it will take input from CSV file by row one by one
  for row in virtualReader: 
    myvirtual = mgmt.tm.ltm.virtuals.virtual.create(
      name=row["name"],
      description=row["description"], 
      destination="%s:%s" % (row["ip"], row["port"])) # pay attention to brackets

或者如果您知道您期望哪个异常以及如何处理它,则捕获异常

from f5.bigip import ManagementRoot
import urllib3
import csv
import sys
import os
urllib3.disable_warnings()
mgmt = ManagementRoot('13.126.189.103','admin','admin')
ltm = mgmt.tm.ltm
virtuals = mgmt.tm.ltm.virtuals
virtual = mgmt.tm.ltm.virtuals.virtual


def configureVirtuals(bigip, virtualFile):
  # These are the fields in the file. The purpose of each field is obvious. See. Input from CSV file called 'virtualFile' and each row has config details
  # the BIG-IP iControl REST API for more information.
  fieldNames = ["name", "description", "ip", "port"]
  virtualReader = csv.DictReader(virtualFile, fieldnames=fieldNames,
                  delimiter=",")


  # Create a virtual server, one per line. it will take input from CSV file by row one by one

  try:
    for row in virtualReader: 
      myvirtual = mgmt.tm.ltm.virtuals.virtual.create(
        name=row["name"],
        description=row["description"], 
        destination="%s:%s" % (row["ip"], row["port"])) # pay attention to brackets
  except SpecificExceptionType:
    print('Print some instruction or log an error and continue the flow')
于 2017-11-09T11:00:14.247 回答