1

I'm trying to sort out how to use the new3 method of the XMLRPC::Client class. This is what I have:

#!/usr/bin/ruby

require "xmlrpc/client"

params = {
  host: "https://192.168.1.2",
  path: "rpc/api",
  port: "443",
  proxy_host: "",
  proxy_port: "",
  user: "username",
  password: "password",
  use_ssl: "true",
  timeout: 300,
}

session = XMLRPC::Client.new3(params)
session.instance_variable_get(:@http).instance_variable_set(:@verify_mode, OpenSSL::SSL::VERIFY_NONE)

sys_ver = session.call('api.systemVersion')
users = session.call("user.listUsers", session)

If I use the new2 method the script will connect and return a 404 error. However, if I use the new3 it returns

/usr/lib/ruby/1.9.1/net/http.rb:762:in `initialize': Connection refused - connect(2) (Errno::ECONNREFUSED)
    from /usr/lib/ruby/1.9.1/net/http.rb:762:in `open'
    from /usr/lib/ruby/1.9.1/net/http.rb:762:in `block in connect'
    from /usr/lib/ruby/1.9.1/timeout.rb:68:in `timeout'
    from /usr/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
    from /usr/lib/ruby/1.9.1/net/http.rb:762:in `connect'
    from /usr/lib/ruby/1.9.1/net/http.rb:755:in `do_start'
    from /usr/lib/ruby/1.9.1/net/http.rb:750:in `start'
    from /usr/lib/ruby/1.9.1/xmlrpc/client.rb:535:in `do_rpc'
    from /usr/lib/ruby/1.9.1/xmlrpc/client.rb:420:in `call2'
    from /usr/lib/ruby/1.9.1/xmlrpc/client.rb:410:in `call'
    from sat_test.rb:24:in `<main>'

Am I using the new3 method properly?

Also, if I use the new method instead, do I need to set parameter=value for each parameter (proxy_host=, proxy_port=, etc)?

theillien
  • 1,189
  • 3
  • 19
  • 33
  • Check the server side where you have the XMLRPC server. As the message say you can't connect the server. So you have a network, firewall or service problem imho. – YvesR Apr 22 '13 at 15:18
  • I did consider that. I'm able to connect to the server's web UI from the computer I'm running the script from. I've also turned off iptables on the server to ensure that isn't blocking the connection. – theillien Apr 22 '13 at 15:37
  • Additionally, if I use the new2 method sending just a URI I get a 404 error which indicates that it is at least connecting to the server. This is good as a test but not worth looking into for why it sends that error since I need to pass a username and password. – theillien Apr 22 '13 at 15:53
  • I think part of the problem I'm having is that I need to establish a session and then log into it. It doesn't seem that the Satellite server I'm attempting to connect to will allow me to do it all at once. That's the impression I'm getting, anyway. – theillien Apr 22 '13 at 16:16
  • Ultimately, this does seem to be an issue with the server. I've received the 404 error with a Perl script as well. – theillien Apr 22 '13 at 16:53

1 Answers1

2

The problem seemed to be more with my script being coded wrong. This is what I have now:

#!/usr/bin/ruby

require "xmlrpc/client"

params = {
  host: "REDACTED",
  path: "/rpc/api",
  use_ssl: "true",
  user: "REDACTED",
  pass: "REDACTED"
}

client = XMLRPC::Client.new3(params)
client.instance_variable_get(:@http).instance_variable_set(:@verify_mode, OpenSSL::SSL::VERIFY_NONE)

session = client.call('auth.login', params[:user], params[:pass])

sys_ver = session.call('api.systemVersion', session)
users = session.call("user.listUsers", session)
users.each { |uname| puts uname }

I've removed unnecessary arguments in the params hash and made sure everything is properly quoted.

It works and is only leaving me with a 404 error which is a problem with the script. But, at least it connects now and gives me something.

theillien
  • 1,189
  • 3
  • 19
  • 33
  • Anyone who ends up here wondering how to interact with hosted Wordpress.com blogs via XML-RPC, the above snippet can be used to get it working. Wordpress 301-redirect their http to https (at least for some XML RPC requests) so you need to use an SSL enabled client. – kez Jan 06 '15 at 09:52