码迷,mamicode.com
首页 > 编程语言 > 详细

Python3调用Salt API

时间:2020-05-20 18:47:33      阅读:53      评论:0      收藏:0      [点我收藏+]

标签:lib   sel   htm   form   ref   服务   webkit   客户端   cat   

 

import urllib.request,urllib.parse,json

class saltAPI():

    def __init__(self):
        self.url = http://10.10.2.11:8000
        self.data = {username : salt-api,
                     password : salt-api,
                     eauth : pam}
        self.headers = {User-Agent:Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36}
        self.token = self.get_token()
        self.headers[X-Auth-Token] = self.token

    #获取token
    def get_token(self):
        postdata = urllib.parse.urlencode(self.data).encode(utf8)
        content = self.post_request(/login,postdata)
        try:
            token = content[return][0][token]
            return token
        except Exception as ErrorMsg:
            print(ErrorMsg)

    #发送post
    def post_request(self,prefix=/,postdata=None):
        try:
            request = urllib.request.Request(self.url+ prefix,postdata,headers=self.headers)
            reponse = urllib.request.urlopen(request).read()
            content  = json.loads(reponse.decode(utf8))
            return content
        except Exception as ErrorMsg:
            print(ErrorMsg)
            exit()


    #执行远程命令
    def SaltCmd(self,tgt,fun=cmd.run,arg=None):
        #通过urlencode格式提交参数,则会多个arg,dict中不能包含重复key,需要将其转码为bytes后重新拼接
        params = {client:local, tgt:tgt, fun:fun, arg:arg}
        params2 = {arg:shell=powershell}
        obj = urllib.parse.urlencode(params).encode(UTF8)
        obj2 = urllib.parse.urlencode(params2).encode(UTF8)
        obj_all = obj  + b& +obj2
        ret = self.post_request(prefix=/,postdata=obj_all)
        res = ret[return][0]
        return(res[tgt].replace(\r,‘‘))

    #执行远程命令1,功能同上
    def SaltCmdrun(self,tgt,fun=cmd.run,arg=None):
        #通过json格式提交参数,需要指定Content-Type(服务器/客户端提交的数据类型),Accept表示客户端希望接收的类型;这里指定Shell=PowerShell
        self.headers[Content-Type] = application/json
        params = [{client: local, tgt: tgt, fun: fun,arg:arg ,kwarg:{shell: powershell} }]
        #json.loads() transform str to dict;json.dumps() transform dict to str.
        #将params转换为浏览器可识别的bytes类型
        obj = bytes(json.dumps(params),utf8)
        ret = self.post_request(prefix=/,postdata=obj)
        res = ret[return][0]
        return(res[tgt].replace(\r,‘‘))

    #执行远程命令2,功能同上
    def SaltCmdrun2(self,tgt,fun=cmd.run,arg=None,**kwargs):
        #通过json格式提交参数,需要指定Content-Type
        self.headers[Content-Type] = application/json
        params = [{client: local, tgt: tgt, fun: fun,arg:arg ,kwarg:kwargs }]
        #json.loads() transform str to dict;json.dumps() transform dict to str.
        #将params转换为浏览器可识别的bytes类型
        obj = bytes(json.dumps(params),utf8)
        ret = self.post_request(prefix=/,postdata=obj)
        res = ret[return][0]
        return(res[tgt].replace(\r,‘‘))

    #打印Header(含token)
    def test(self):
        print(self.headers)


s = saltAPI()
# s.test()
t1 = s.SaltCmd(10.10.2.4,cmd.run,get-host) print(t1) # t1 = s.SaltCmdrun(‘10.160.25.48‘,‘cmd.run‘,‘get-host‘) # print(t1) # t2 = s.SaltCmdrun2(‘10.160.25.48‘,‘cmd.run‘,‘gwmi win32_bios‘,shell=‘powershell‘) # print(t2)

 

Python3调用Salt API

标签:lib   sel   htm   form   ref   服务   webkit   客户端   cat   

原文地址:https://www.cnblogs.com/dreamer-fish/p/12925226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!