使用Python获取zabbix登录的Sessionid,该ID用于zabbix API数据获取,开发环境python2.7
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import json
import sys
import urllib2
from urllib2importURLError
reload(sys)
sys.setdefaultencoding('utf-8')
#login
class zabbix_login:
def__init__(self):
self.url ='http://192.168.1.3zabbix/api_jsonrpc.php'
self.header = {"Content-Type":"application/json"}
self.hostid = []
self.hostname = []
defuser_login(self):
data = json.dumps({
"jsonrpc":"2.0",
"method":"user.login",
"params": {
"user":"Admin",
"password":"zabbix"
},
"id":0
})
request = urllib2.Request(self.url, data)
forkeyinself.header:
request.add_header(key,self.header[key])
try:
result = urllib2.urlopen(request)
exceptURLErrorase:
print"Please Check URL !",e.code
exceptKeyErrorase:
print"Please Check Password !",e
else:
response = json.loads(result.read())
result.close()
self.authID = response['result']
returnself.authID
if __name__ =="__main__":
t = zabbix_login()
printt.user_login()