Salt-api调用模块saltapi.py
2016-02-15 16:59
369 查看
本模块用于调用Salt-api功能,内容如下
#!/usr/bin/python # -*- coding: utf-8 -*- import pycurl import StringIO #登录salt-api,获取token def 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')
相关文章推荐
- Leetcode(2) -Contains Duplicate
- 递归:汉诺塔
- Hoeffding不等式
- N层楼梯上楼方式总数
- TODO 阅读volley源码 尝试进行volley源码解析
- React Native入门——组件构成及生命周期简介
- web存储
- 离线缓存
- Android 5.0 Camera (5): take picture
- Android 5.0 Camera (6): record video
- 发现引力波
- <[你在荒废时间的时候别人都在拼命!]>
- PHP批量清除bom头
- PHP-FPM CRASH DiagnosticReports
- Java多线程编程总结
- 分数化小数
- 第45讲:Scala中Context Bounds代码实战及其在Spark中的应用源码解析
- nginx的平滑升级
- oracle使用exp导出并备份上传shell
- Style 减少代码重复