This has really turned into a wild goose chase. Initially my goal when I set out on this project was simply to get Fabric up and running so I could test out some different features on some network gear. It seems like the Python integration in Windows is very different than it is in the Linux world where everything is all bundled up nice and neatly. There are several separate, seemingly unrelated pieces that all need to fit together to get Python and Fabric working correctly in a Windows environment, which can be very perplexing at first, hence my need to write a post so I don’t have to remember all this complexity for next time. I thought I might as well show people how I got this to work instead of picking and choosing different bits of information from the internet.
The following is a list of links that I have found to be helpful in getting everything up and going, flip back to here for the different resources and components:
- Official Python download page
- Official docs for installing and using Fabric
- using this post I was able to get pip installed and set up
- Install and configure virtualenv
- Distribute modules for Windows
- pip modules for Windows
- Pycrypto modules for Windows
- MinGW32 installer
There’s a few steps for getting up and running. For basic Python functionality it should be enough to download and install Python via the basic installer in your Windows environment. Accepting the defaults should be enough. Also, I recommend going with Python 2.7, rather than 3.3 because it has much better backwards compatibility. You will also want to double check to make sure you download the correct version for you OS as well, either 32-bit or 64-bit.
Once you have your Python install up and going you will want to get pip installed. You will use this tool to get Python modules because it aids tremendously with downloading, managing and installing useful Python code.
So to get up and running with pip, first make sure that you have the correctly matched version of Python and the pip installed for your environment. For example the 2.7 pip installer will not work with a 3.3 Python installation. Second, you will need to make sure you have the Distribute package installed in your Python environment as well. This is the tool that will allow pip to work. Once you have these modules installed you will need to switch to the directory where pip is installed (or add it to your ENV path variable). For me it was located in the following location:
So the command to install Fabric would be as follows:
pip.exe install fabric
You would think that’s all you need to get fabric working right? Well it turns out that using this method we do not have the correct version of Pycrypto installed.
So using the link posted above go ahead and get the correct version of Pycrypto downloaded and installed (version 2.1.0). That still doesn’t fix it though! It just gets us to a different error. I used this post and this post as a guide for getting the correct version of Pycrypto installed on the Windows machine.
Okay, so now we should have a fully functioning Python environment with Fabric installed. The only main issue that remains at this point (to my knowledge at least) is that pip still doesn’t work quite right when attempting to install various Python packages. To get that part working you will need MinGW32 installed (reference above for links). But that is basically out of the scope of this post, I will write another post about it if there is any interest or you can ask me if you have issues as always.
The only other piece left then is to get Fabric up and going with our Cisco gear. Take a look at the docs for basic usage on getting acquainted with Fabric, it is fairly straight forward for the most part.
One thing I was not aware of was the way Cisco CLI and devices would behave when using Fabric to control them remotely. I was having issues with Fabric flaking out whenever I went into config mode on a Cisco switch. It turns out that when you enter into config mode you are essentially dropped into a new shell and Fabric doesn’t have a nice way to deal with that. So something like this will bomb out,
def test(): run("conf t", shell=False) run("int 1/0/1", shell=False) run("no shut", shell=False) run("exit", shell=False)
The “conf t” command opens your new shell and the Cisco gear freaks out because it doesn’t know what to do with the next command. I should also mention the shell=False is somewhat unrelated to this issue but it gets around Fabric trying to use bash as its default shell. The workaround? Use the open_shell command in Fabric and escape each command by using \n to escape to a new line. So a sample command using this format would be something like the following,
def test(): open_shell("conf t \n" "ip name-server 18.104.22.168 \n" "exit \n" "exit \n" )
Yeah this is sort of hacky, and I’m not sure if it will be able to do everything I am looking for but hey at least it kind of works. I am currently looking for a more robust and easier way around this limitation so if you have any suggestions let me know.
Credit goes to markmm on reddit for letting me know about this workaround as well as the people who hang out on the #fabric irc channel on freenode.