Last active
July 10, 2016 20:35
-
-
Save sveetch/d38c32d011ff591438b3117e450d3bfe to your computer and use it in GitHub Desktop.
Fabric tasks basic sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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