(二)Python調用Zabbix api之從入門到放棄——登入並擷取身分識別驗證令牌

來源:互聯網
上載者:User

標籤:沒有   圖片   false   擷取   print   ref   request   com   log   

訪問zabbix api的URL是:

    http://x.x.x.x/zabbix/api_jsonrpc.php

    x.x.x.x可能是你的IP或者網域名稱


訪問流程概覽:

1、首先登入

2、認證成功後zabbix server返回一個token

3、帶著這個token去訪問各種資料,做各種操作

4、完畢!


一、用RESTClient進行登入


在json請求的本文中,具有以下屬性:

  • jsonrpc - API使用的JSON-RPC協議的版本; Zabbix API實現JSON-RPC版本2.0;

  • method - 調用的API方法;

  • params - 將被傳遞給API方法的參數;

  • id - 請求的任意標識符;

  • auth -使用者認證令牌; 因為我們還沒有一個,它的設定null。


正確提供憑據後,API返回的響應將包含使用者身分識別驗證令牌(json格式):

{

    "jsonrpc":"2.0",

    "result":"140f4524c02e2731dd74c48d29aa5ce8",  #這個就是token

    "id":1

}


二、使用Python進行登入

# -*- coding:utf-8 -*-import urllib2import jsonurl = 'http://x.x.x.x/zabbix/api_jsonrpc.php'header = {'Content-Type': 'application/json'}req = json.dumps(    {        "jsonrpc": "2.0",        "method": "user.login",        "params": {            "user": "Admin",            "password": "你的密碼"        },        "id": 0,    })def auth():    r = urllib2.Request(url=url, headers=header, data=req)    response = urllib2.urlopen(r)    token = json.loads(response.read())    print(token)if __name__ == '__main__':    auth()

得到的響應:

(二)Python調用Zabbix api之從入門到放棄——登入並擷取身分識別驗證令牌

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.