iPython notebook + fabric to create a personal commando.io
Written by Yannik Messerli - 1 October 2014
Commando is a nice service that let you store shell scripts and run then on different servers. It is extremely handy. There is plenty of scripts that I need to run multiple times each day. For example I need to know if a service is running or I need to restart a service. I would spare a lot of time also by storing all my small scripts in a safe place from which I can run them. But Commando requires you to be strict with your workflow and I’m always hacking a bit. Therefore, I was looking for a solution much more flexible keeping the main idea of Commando.
Here is my solution: the nice iPython notebook that let me write python scripts (and more interestingly also notes) alongside Fabric that will allow me to fire commands through SSH. You’ll need python, pip, virtualenv and nohup to follow these steps.
Let’s start by creating a folder for our project and a separated environment:
mkdir commando cd commando virtualenv ENV source ENV/bin/activate
Then, we will create a little file to contain the required frameworks:
Paste the following bundles:
ipython fabric pyzmq jinja2 tornado
Then, run this to install them:
pip install -r requirements.txt
We will now create the iPython server. The following steps are heavily inspired by this page. First of all, we will create a
sha1 password. In a python shell, type:
In : from IPython.lib import passwd In : passwd() Enter password: Verify password: Out: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed’
Then, copy the
sha1 hash. We will use it later. For now, we will create a self-signed SSL certificate to secure connections to our server. This is rather important since we will expose ipython to the world. We don’t want someone accessing our scripts and mess with our servers. Using openssl, type in a shell:
openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
Done. You should have a file
mycert.pem in your folder. We will create the python server configuration in the following steps. To generate the default ones, type:
ipython profile create nbserver
Then, edit the created file
ipython_config.py. It should be located somewhere in
~/.ipython/profile_nbserver. Add the following configurations, just after
c = get_config():
c.IPKernelApp.pylab = 'inline' # if you want plotting support always c.NotebookApp.certfile = u'mycert.pem' c.NotebookApp.ip = '*' c.NotebookApp.open_browser = False c.NotebookApp.password = u'sha1:bcd259ccf...[your hashed password here]' c.NotebookApp.port = 9999
You can start the ipython server using:
nohup ipython notebook --profile=nbserver --certfile=mycert.pem &
If everything went alright, you should be able to access it to
https://you-domain-or-ip:9999/ at this point. Good?
So, type your password and create a new notebook. Finally, We will import fabric and run our first command to a server we own. In the notebook, type:
from fabric.api import * env.host = "0.0.0.0" # Replace with your host name or IP env.user = "user" # Replace with your username env.password = "user" # Your password. You can also use keys. Checkout the fabric doc run("echo 'Hello!'")
If you run this cell, you should see a ‘Hello!’ in the output, coming from the remote server. Yeah! And now, be creative. This has endless possibilities - all saved in a nice environment.