Salt-api调用模块saltapi.py

来源:互联网 发布:java初学者源代码 编辑:程序博客网 时间:2024/06/06 03:47

本模块用于调用Salt-api功能,内容如下

#!/usr/bin/python# -*- coding: utf-8 -*- import pycurlimport StringIO #登录salt-api,获取tokendef api_login():    global token    url='https://192.168.90.62:8000/login'    ch=pycurl.Curl()    #创建一个pycurl对象的方法    ch.setopt(ch.URL, url)     #设置要访问的url    info = StringIO.StringIO()         ch.setopt(ch.WRITEFUNCTION, info.write)    ch.setopt(ch.POST, True)    #如果是https就要开启这两行    ch.setopt(ch.SSL_VERIFYPEER, 0)    ch.setopt(ch.SSL_VERIFYHOST, 2)    ch.setopt(ch.HTTPHEADER, ['Accept: application/x-yaml'])    ch.setopt(ch.POSTFIELDS, 'username=saltapi&password=111111&eauth=pam')    #要包头信息    #ch.setopt(ch.HEADER, True)    #不要包头信息    ch.setopt(ch.HEADER,False)    ch.perform()    html = info.getvalue()    #提取token    token = html.split("\n")[-3].replace("\n", '')    token = token.split(' ')[3]    info.close()    ch.close() def api_exec(target, fun, expr_form='', arg='', arg_num=0):    global token    url='https://192.168.90.62:8000/'    ch=pycurl.Curl()    ch.setopt(ch.URL, url)    info = StringIO.StringIO()    ch.setopt(ch.WRITEFUNCTION, info.write)    ch.setopt(ch.POST, True)    ch.setopt(ch.SSL_VERIFYPEER, 0)    ch.setopt(ch.SSL_VERIFYHOST, 2)    ch.setopt(ch.HTTPHEADER, ['Accept: application/json', "X-Auth-Token: %s" %(token)])    if arg_num == 0:        ch.setopt(ch.POSTFIELDS, "client=local&tgt=%s&fun=%s" %(target, fun))    elif arg_num == 1:        ch.setopt(ch.POSTFIELDS, "client=local&tgt=%s&fun=%s&arg=%s" %(target, fun, arg))        elif arg_num == 2:        ch.setopt(ch.POSTFIELDS, "client=local&tgt=%s&expr_form=%s&fun=%s&arg=%s" %(target, expr_form, fun, arg))    else:        pass    ch.setopt(ch.HEADER,False)    ch.perform()    html = info.getvalue()    info.close()    ch.close()    return html #测试时用的,做为模块使用时请注释下面两行api_login()print api_exec('*.61', 'test.ping')


0 0
原创粉丝点击