Python3调用Salt API

2021-01-16 13:15

阅读:674

标签: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


评论


亲,登录后才可以留言!