Fabric is a Python library and command-line tool that lets you manage remote servers. I’ve found it useful for working with Vagrant boxes. With this fabfile.py, you can execute commands on the Vagrant box by supplying prefacing your commands with vagrant:22222. The port is passed as a parameter to the command:


from __future__ import with_statement
from fabric.api import *
from fabric.contrib.console import confirm

def vagrant(port): 
env.user = 'vagrant'
# Give the port as an arugment -- this changes depending on the VM
env.hosts = ['127.0.0.1:%s' % port]
# use vagrant ssh key // via http://sysadminpy.com/sysadmin/2011/04/30/use-fabric-on-vagrant-instances/ updated to vagrant-ssh
result = local('vagrant ssh-config | grep IdentityFile', capture=True)
env.key_filename = result.split()[1]