I've wanted to learn Python for some time already, but the lack of free time that I need to dedicate to it doesn't allow me. Well, thanks to the long Christmas holiday this year, I've managed to get myself some time to actually start learning Python. This post is about Python and FreeBSD Jails. It is the result of my 1 week studying of Python and presents a new Python interface for FreeBSD's jail_get() system call.
The Python interface for jail_get() system call is added to the existing devel/py-freebsd port and the purpose of this Python interface is to allow you to get jail's parameters like the JID, hostname, IP address, etc.
For now the jail_get() Python interface is only available from the Github repository listed below, but hopefully soon enough it will be added to the FreeBSD Ports Collection as well.
Okay, lets see what we can do with the new jail_get() system call interface for Python, time to fire up your Python interpreter!
dnaeon:(~) % python Python 2.7.3 (default, Aug 4 2012, 12:47:37) [GCC 4.2.1 20070831 patched [FreeBSD]] on freebsd9 Type "help", "copyright", "credits" or "license" for more information. >>>
First lets load the needed Python modules:
>>> from freebsd import * >>> from pprint import *
The Python interface for jail_get() takes one integer argument, which is the jail's JID and returns a dictionary with the parameters of the jail. If you want to get the parameters of all your jails then pass 0 (zero) as the argument to jail_get(). Check the example below:
>>> pprint(jail_get(0))
{'3': {'hostname': 'relay.example.org',
'ip4': '10.11.117.4',
'jid': 3,
'path': '/jails/relay'},
'4': {'hostname': 'mysql.example.org',
'ip4': '10.11.117.5',
'jid': 4,
'path': '/jails/mysql'},
'495': {'hostname': 'www.example.org',
'ip4': '10.11.117.7',
'jid': 495,
'path': '/jails/www'},
'496': {'hostname': 'www-proxy.example.org',
'ip4': '10.11.117.6',
'jid': 496,
'path': '/jails/www-proxy'}}
You can see from the above output that we've got a nice dictionary with the parameters of all our jails. Getting the parameters for a single jail works too, check this example:
>>> jail_get(3)
{'path': '/jails/relay', 'jid': 3, 'hostname': 'relay.example.org', 'name': '3', 'ip4': '10.11.117.4'}
Okay, that's all good, but can we do more with it? Sure, we can now write a simple jls(8)-like Python function that displays information about our FreeBSD jails. The script below works in a similar way to jls(8) and displays information about our jails:
#!/usr/bin/env python
import freebsd
def py_jls():
jails = freebsd.jail_get(0)
print '%5s %-15s %-35s %-20s' % ('JID', 'IP Address', 'Hostname', 'Path')
for j in jails:
print '%5s %-15s %-35s %-20s' % \
(jails[j]['jid'], jails[j]['ip4'], jails[j]['hostname'], jails[j]['path'])
py_jls()
And here's the output after running the script:
dnaeon:(~) % python py-jls.py JID IP Address Hostname Path 495 10.11.117.7 www.example.org /jails/www 3 10.11.117.4 relay.example.org /jails/relay 4 10.11.117.5 mysql.example.org /jails/mysql 496 10.11.117.6 www-proxy.example.org /jails/www-proxy
Pretty much the same as what jls(8) does, isn't it? Well, I guess that's for now, but one thing is sure - I will continue with my study on Python, so stay tuned for new and interesting stuff about Python and FreeBSD! :)
Comments
unix-heaven@tim.thechases.com
You could tidy up that Python code by using dictionary string formatting:
jails = freebsd.jail_get(0) headers = dict( jid="JID", ip4="IP Address", hostname="Hostname", path="Path", ) for j in [headers] + jails.values(): print '%(jid)5s %(ip4)-15s %(hostname)-35s %(path)-20s' % jThat said, it looks like a pretty handy library. Thanks!
Re: Python dicts
Hey Tim,
Thanks for the hint!
Regards,
Marin
"I fight for the users!"