Skip to content

Instantly share code, notes, and snippets.

@sveetch
Last active July 10, 2016 20:35
Show Gist options
  • Save sveetch/d38c32d011ff591438b3117e450d3bfe to your computer and use it in GitHub Desktop.
Save sveetch/d38c32d011ff591438b3117e450d3bfe to your computer and use it in GitHub Desktop.
Fabric tasks basic sample
# -*- coding: utf-8 -*-
"""
Some ssh script stuff using 'fabric'
My personnal environment use ssh keys with passphrase
(some empty, some other not) to connect to knowed hosts.
Install requires:
* Needed C build toolchain to compile C modules;
* Library libssl-dev (for OpenSSL >= 1.0.1);
* Library libffi-dev;
* Python development headers (commonly libpython-dev);
* Python package fabric >= 1.11.1;
Strange behavior noticed
************************
Using remote task prompt for a password for wrong public key. Seems fabric or
paramiko are taking the first key entry from ~/.ssh/config event it is not
related to requested host.
So i added this to .ssh/config: ::
Host myhost
IdentityFile ~/.ssh/id_dsa
This avoid fabric to search for a key to connect to the host.
If you use only one ssh key to connect to your knowed host, you should not need to do this.
NOTE: Mind about feeling an issue for this to fabric github.
"""
## Uncomment this to get full logs from paramiko, useful to debug problems with
## ssh authent or connection
#import logging
#logging.basicConfig(level=logging.DEBUG)
from fabric.api import *
env.use_ssh_config = True
env.hosts = ['user@myhost']
@task
def hello(name="world"):
"""Dummy task just for printing out"""
print("Hello %s!" % name)
@task
def foo():
"""Just trying a local command"""
local('ls -l')
@task
def bar():
"""Basic remote command on hosts and returned response attributes"""
res = run('ls -l')
print "command:", res.command
print "succeeded:", res.succeeded
print "failed:", res.failed
print "return_code:", res.return_code
@task
def logs():
"""Trying behavior on remote command using sudo"""
sudo('sudo ls /var/log')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment