基于togglepoolmember.pl编写F5设备控制模块

为了方便利用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","端口","用户","密码","%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","端口","用户","密码","%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')


你可能感兴趣的:(linux,python,F5)