为了方便利用python对F5设备进行操作,本文将togglepoolmember.pl对F5设备的控制写成了python模块,源代码例如以下:
#!/usr/bin/python # -*- coding: utf-8 -*- import os import sys import json import subprocess #def f5_status(pool=sys.argv[1],ip=sys.argv[2]): def f5_status(pool,ip): res = subprocess.Popen(["/usr/bin/perl","/scripts/togglepoolmember.pl","F5设备IP","端口","用户","password","%s" %(pool)],stdout=subprocess.PIPE,stderr=subprocess.PIPE,close_fds=True) result = res.stdout.readlines() a = "".join(result[4:-1]).split("%s" %(ip))[-1].split(")")[0].split("_")[-1] e = json.dumps(a) return e def f5_exec(pool,ip,port): res = subprocess.Popen(["/usr/bin/perl","/scripts/togglepoolmember.pl","F5设备IP","端口","用户","password","%s" %(pool),"%s:%s" %(ip,port)],stdout=subprocess.PIPE,stderr=subprocess.PIPE,close_fds=True) result = res.stdout.readlines() d = json.dumps(result) return d #測试时用的,做为模块使用时请凝视以下 print f5_exec('pool名称','pool成员IP','pool成员端口') #print f5_status('pool名称','pool成员IP')