]> pere.pagekite.me Git - homepage.git/blob - blog/data/2012-12-06-bofhd-scripting.txt
71d316d5fa6dc103e905fb5ce2350dfc7789c8a1
[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 was providing
10 an <a href="http://en.wikipedia.org/wiki/XML-RPC">XML-RPC</a> API, but
11 never spent time to try to figure out how to use it, as we always use
12 the bofh command line client at work. Until today. Here are a few
13 notes on how to use it with Python.</p>
14
15 <p>I started by looking at the source of the Java
16 <a href="http://cerebrum.svn.sourceforge.net/viewvc/cerebrum/trunk/cerebrum/clients/jbofh/">bofh
17 client</a>, to figure out how it connected to the API server. I also
18 googled for python examples on how to use XML-RPC, and found
19 <a href="http://tldp.org/HOWTO/XML-RPC-HOWTO/xmlrpc-howto-python.html">a
20 simple example in</a> the XML-RPC howto.</p>
21
22 <p>This simple example code show how to connect, get the list of
23 commands (as a JSON dump), and how to get the information about the
24 user currently logged in:</p>
25
26 <blockquote><pre>
27 #!/usr/bin/env python
28 import getpass
29 import xmlrpclib
30 server_url = 'https://cerebrum-uio.uio.no:8000';
31 username = getpass.getuser()
32 password = getpass.getpass()
33 server = xmlrpclib.Server(server_url);
34 #print server.get_commands(sessionid)
35 sessionid = server.login(username, password)
36 print server.run_command(sessionid, "user_info", username)
37 result = server.logout(sessionid)
38 print result
39 </pre></blockquote>
40
41 <p>Armed with this knowledge I can now move forward and script the DNS
42 and DHCP updates I wanted to do.</p>