]> pere.pagekite.me Git - homepage.git/blob - blog/data/2012-12-06-bofhd-scripting.txt
Generated.
[homepage.git] / blog / data / 2012-12-06-bofhd-scripting.txt
1 Title: Scripting the Cerebrum/bofhd user administration system using XML-RPC
2 Tags: english, sysadmin
3 Date: 2012-12-06 10:30
4
5 <p>Where I work at the <a href="http://www.uio.no/">University of
6 Oslo</a>, we use the
7 <a href="http://sourceforge.net/projects/cerebrum/">Cerebrum user
8 administration system</a> to maintain users, groups, DNS, DHCP, etc.
9 I've known since the system was written that the server is providing
10 an <a href="http://en.wikipedia.org/wiki/XML-RPC">XML-RPC</a> API, but
11 I have never spent time to try to figure out how to use it, as we
12 always use the bofh command line client at work. Until today. I want
13 to script the updating of DNS and DHCP to make it easier to set up
14 virtual machines. Here are a few notes on how to use it with
15 Python.</p>
16
17 <p>I started by looking at the source of the Java
18 <a href="http://cerebrum.svn.sourceforge.net/viewvc/cerebrum/trunk/cerebrum/clients/jbofh/">bofh
19 client</a>, to figure out how it connected to the API server. I also
20 googled for python examples on how to use XML-RPC, and found
21 <a href="http://tldp.org/HOWTO/XML-RPC-HOWTO/xmlrpc-howto-python.html">a
22 simple example in</a> the XML-RPC howto.</p>
23
24 <p>This simple example code show how to connect, get the list of
25 commands (as a JSON dump), and how to get the information about the
26 user currently logged in:</p>
27
28 <blockquote><pre>
29 #!/usr/bin/env python
30 import getpass
31 import xmlrpclib
32 server_url = 'https://cerebrum-uio.uio.no:8000';
33 username = getpass.getuser()
34 password = getpass.getpass()
35 server = xmlrpclib.Server(server_url);
36 #print server.get_commands(sessionid)
37 sessionid = server.login(username, password)
38 print server.run_command(sessionid, "user_info", username)
39 result = server.logout(sessionid)
40 print result
41 </pre></blockquote>
42
43 <p>Armed with this knowledge I can now move forward and script the DNS
44 and DHCP updates I wanted to do.</p>