Python3调用Salt API

 curl调用salt api cmd.run使用方法如下:

curl http://10.10.2.11:8000 \
-H 'Accept: application/x-yaml' \
-H 'X-Auth-Token: 042a226e6'\
-d client=local \
-d tgt='10.10.2.4' \
-d fun='cmd.run' \
-d arg='get-host'


curl http://10.10.2.11:8000 \
-H 'Content-Type: application/json' \
-H 'X-Auth-Token: 042a226e6'\
-d '[{"client": "local", "tgt": "10.10.2.4", "fun": "cmd.run","arg":"get-host" ,"kwarg":{"shell": "powershell"} }]'


curl http://10.10.2.11:8000 \
-H 'Accept: application/x-yaml' \
-H 'X-Auth-Token: 042a226'\
-d client=local \
-d tgt='10.10.2.4' \
-d fun='cmd.run' \
-d arg='get-host' \
-d arg='shell="powershell"'

附:

There is already an option to use JSON in requests to salt-api. This is the best (easiest) way to send complex data structures in the HTTP request. It looks like this:

curl -k https://localhost:8000/run \
[...snip...] \
-H 'Content-Type: application/json' \
-d '[{"client": "local", "tgt": "*", "fun": "test.arg", "kwarg": {"a": 1}}]'

If you would prefer to continue using the urlencoded format instead of JSON there is an odd trick you can use since salt-api and the salt CLI program both use Salt's Python API and any CLI parameters are run through the YAML parser. The syntax is a bit rough but it looks like this:

curl -k https://localhost:8000/run \
[...snip...] \
-d arg='a=1'

 Note that this isn't an intended feature. It's a side effect of how Salt's Python API and the CLI work together. You're much better off sending real JSON if you can.

 

Python3调用salt api方法如下:

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.10.2.4','cmd.run','get-host') # print(t1) # t2 = s.SaltCmdrun2('10.10.2.4','cmd.run','gwmi win32_bios',shell='powershell') # print(t2)

 

#多台主机同时执行cmd.script,使用expr_form=list,脚本参数通过args传递
    def SaltCmd_Multi(self,tgt,fun='cmd.run',expr_form='glob',arg=None,**kwargs):
        #通过json格式提交参数,需要指定Content-Type
        self.headers['Content-Type'] = 'application/json'
        params = [{'client': 'local', 'tgt': tgt, 'fun': fun,'expr_form':expr_form,'arg':arg ,'kwarg':kwargs }]
        print(params)
        #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)
        print(ret)

t2 = s.SaltCmd_Multi('10.10.2.4,10.10.2.5','cmd.script','list','http://10.10.2.6/OService.ps1',args='w32time Query',shell='powershell')
t2 = s.SaltCmd_Multi('10.10.2.4','cmd.script','glob','http://10.10.2.6/OService.ps1',args='w32time Query',shell='powershell')
print(t2)

 

posted on 2020-05-20 18:15  momingliu11  阅读(1206)  评论(0编辑  收藏  举报