Created
October 3, 2012 00:20
-
-
Save travisdmathis/3824177 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'net/http' | |
require 'rubygems' | |
require 'json' | |
require 'pp' | |
### Server Details | |
@server="198.61.1.1" | |
@api="/zabbix/api_jsonrpc.php" | |
### Login Details | |
@user="user" | |
@pass="pass" | |
@json_auth_data ={ | |
"jsonrpc" => "2.0", | |
"method" => "user.login", | |
"params" => {"user" => @user,"password" => @pass}, | |
"id" => "1 " | |
}.to_json | |
def authenticate | |
req = Net::HTTP::Post.new(@api, initheader = {'Content-Type' =>'application/json'}) | |
req.body = @json_auth_data | |
response = Net::HTTP.new(@server).start {|http| http.request(req) } | |
puts "Response #{response.code} #{response.message}: | |
#{response.body}" | |
end | |
result = authenticate | |
auth = JSON.parse(result) | |
auth_token = auth['result'] | |
pp auth_token | |
##### authenticate response | |
Response 200 OK: | |
{"jsonrpc":"2.0","result":"ca9b9a66466d730b60047aa28b344a26","id":"1 "} | |
#### ERROR | |
/Users/fluffheadsr/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/json/common.rb:148:in `initialize': can't convert nil into String (TypeError) | |
from /Users/fluffheadsr/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/json/common.rb:148:in `new' | |
from /Users/fluffheadsr/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/json/common.rb:148:in `parse' | |
from ./registraion.rb:34:in `<main>' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment